Hello! You can find there my implementation of ray tracing for simple geometry objects. It currently supports phong lighting, shadows, reflections, refractions, objects: spheres and planes.
The basic ray tracing model is as follows. A camera is placed in the scene and rays are cast from the camera's position to points on an imaginary image plane. A ray is used to determine what light would be going towards the camera at that point and direction. In our case each ray represents a pixel in the output image and the resulting color of the ray determines the color output for the pixel.
For each ray we find the closest point of intersection with objects, it is this object that the camera will see at this point
Diffuse lighting is determined by computing the intensity of the light at a point on the sphere. If the angle is close to the normal at that point then the intensity will be increased. The intensity determines how much of the object's color to contribute.
Shadows are incorporated into lighting. To determine if a light source should contribute to the lighting at an intersection point a shadow ray is cast from the intersection point to the light source. If there is an intersection before the light source then this point is in the shadow of that light source.
Specular lighting is calculated by computing a reflection ray by reflecting the light vector about the normal at the intersection point. The view ray is compared to the reflection ray to determine how much specular lighting to contribute. The more parallel the vectors are the more specular lighting will be added.
Reflections are performed by casting rays originating from the intersection point directed along the reflection vector. A portion of the reflected ray's color will be contributed to the original intersection point based on how reflective the surface is. Fortunately this is fairly easy given the a recursive approach for casting rays. There is an arbitrary limit on how many reflections a ray can perform before stopping to improve performance and eliminate potential infinite loops.
Reflections are performed by casting rays originating from the intersection point directed along the reflection vector. A portion of the reflected ray's color will be contributed to the original intersection point based on how reflective the surface is. Fortunately this is fairly easy given the a recursive approach for casting rays. There is an arbitrary limit on how many reflections a ray can perform before stopping to improve performance and eliminate potential infinite loops.
Each object has its indices of refraction and reflection, which allows you to create different materials for objects.
You can make your scene from the proposed set of objects(light source, sphere, and plane) and materials(base of 20 variants in file materials.cpp
) using a convenient interface to add objects by specifying their coordinates. Also, you can add objects by calling their constructor and adding them into MainScene
using operator <<
.
You can easily add a new type of object by making a new class, which inhirets shade
class. For everything to work, it is enough to write the functions for obtaining the normal at a point and finding the point of intersection of the object with the ray(see sphere.h
file as example)