Ray tracing is one of the many processes that is used in computer graphics to render images to the screen. The main idea of ray tracing is that you shoot out a ray towards your scene and then recursively trace that ray back to a light source. This allows the for the creation of more realistic images because you are able to determine the actual color that our eyes would see.
The first step that I took in my project was casting a ray and outputting the color of a hit shape. This involved looping through my pixels and for each pixel, cast a ray. I then check if that ray intersected any of my objects. If it did, I would output my shape's material color.
Spheres were the shape that I chose to use for my project. The Sphere class contains a center point, radius, and material. Initially, to be sure that my intersection method was working and that rays were being cast properly, I rendered the three spheres, as shown below.
For diffuse lighting, I calculated the lambert term and multiplied it with the square of the the sphere's color. To calculate the lambert term, I take to dot of the normal of intersection and the light ray's direction vector. The image below shows diffuse lighting on the spheres.
The calculate reflection, I create a new ray that originates from the point of intersection and whose direction is mirrored across the normal of intersection. The image below shows us diffuse shading and reflection.
To calculate Blinn-Phong shading, I first calculate the view projection value by taking the dot of the view ray and and the normal of intersection. I then calculate the Blinn vector by subtracting the view ray direction from the light ray direction. Then through a couple more calculations, I calculate the Blinn term and then multiply it by the current reflection factor and itself to the power from the current material. Then to get the color, I multiply the Blinn term, the current material's specular value, and the current light's color. The image below shows us Blinn-Phong.
The final addition to my project was to add anti-aliasing and exposure. To add anti-aliasing I divide each pixel into fourths and then compute the color for each of the fourths. I then added exposure to create a better looking image. Below, is my final image that I create for my project.
To run the program, from the command line, type ./a.out "outfile".tga. This process will then create a TGA file called "outfile".tga.
Ray tracing was a lot harder than I planned. Even though I only did the basic sphere, there are still many other shapes and techniques that I want to implement. A list of additions that I would like to add after this class are listed below:
There is a great tutorial on ray tracing at CoderMind.
There is another excellent tutorial on ray tracing theory at DevMaster.
Scratchapixel also has a good example on basic ray tracing.