Skip to content
A side project I did to understand about lighting and shadows.
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.


Type Name Latest commit message Commit time
Failed to load latest commit information.


Raw Content

A side project I learned from Caleb Piercy to understand about lighting and shadows.

As noted, raytracer works by emitting rays the size of (width) x (height) of the photo, reflecting the rays if they hit a surface, and determining the colors of the surface if the rays hit the light source.

In this case, the image shown above is 320x240 (image is magnified for clarity), therefore the raytracer emits a total of 76800 rays onto the frustum. All surfaces that are hit by the 76800 rays emitted from the camera view will reflect accordingly. This is calculated by obtaining the normal vector of the surface, followed by finding the reflected vector of the light using the following algorithm:


Where RayDirection is the ray direction, or a point with a directional vector, and Normal is the surface normal.

Calculating the surface normal requires using the following algorithm:


Where A, B, and C are all points on a 3D plane, and magnitude is the magnitude or length of the cross product.

Once the rays have been reflected and intersects a light source, the rays closest to the light source will have a brighter hue color on the reflected surface, where the initial ray intersection is at.

Rays with multiple reflections will have the Nth intersection color be mapped onto the N-1th intersection.

Rays with two or more intersections (and no reflections) will emit a shadow, or a darker hue color of the surface, starting from the 3rd intersection. There will be no light reflected after the 4th intersection.

The end result will give an illusion of showing objects with a reflective surface and shadows.


Caleb Piercy - Raytracer C++ Tutorial

You can’t perform that action at this time.