Basic Ray Tracer Design

Ameen Akel — Professor Zoë Wood — CPE 471 Final Project

 

 

 

Ray Tracing in a Nutshell

Before programming this project, I had always thought of Ray Tracing as an algorithm that mapped light from a light source or multiple light sources until it hit a viewer’s eye, just as it is realistically.  However, as I began to do more research on how Ray Tracing works, I found that it was quite backwards in comparison to my initial thoughts.  A “ray” starts out at a user’s eye, or viewing position, and points out at a flat plane directly in front of him or her into a scene of objects.  This flat plane functions exactly as the near plane in a perspective viewing volume, or in other words as the screen.  The ray will display the color of the first shape it intersects on its traversal through the virtual scene, subject to lighting, shadows, as well as other effects.  This is basically repeated for every pixel on the screen until a picture is drawn.


Lighting

Every time a ray intersects with a point on an object, its color must be computed with respect to different aspects of the scene it may interact with.  The Phong model is the lighting model that I’ve used in this program to accurately model the different aspects of light (ambient, diffuse, and specular).  Each light model creates a distinct shade of lightness and darkness on the object based on what it is attempting to represent.  That value of lightness or darkness is then factored in with an object’s tendency to react to that type of light and an object’s pigment, or color (depending on the type of light).

Ambient light attempts to represent the global illumination in the world, so it adds a constant light value evenly to an object.  Diffuse shading represents objects that seem to have a rough, or matte, finish instead of a smooth, mirror-like finish.  Specular lighting is one of the more interesting lighting patterns.  Specular lighting is the lighting that creates a halo-shaped pattern on metallic, smooth objects.  Specular light also typically takes on the color of the light and not the color of the object it is interacting with.  These together all make up a good approximation of real lighting.


Shadows

Not much extra programming is required to add the display of shadows in a rendered scene.  Given the original ray from the eye to the object in the scene, only one extra ray must be cast in the direction of each light source.  If that ray reaches an object before reaching a light source, the surface from which the ray originated (and its corresponding pixel on the viewing plane) receives no light contribution from that light source.

Reflection

Specular reflection allows for an object to realistically take on qualities of its surroundings, which is prominent in mirror-like, glass, or metallic objects.  The algorithm for doing so involves the reuse of the same intersection code as before.  The reflection can be implemented by looking from the point on the reflective surface (as noted by the E under the black surface) out at the same angle, with respect to the normal, as the ray intersecting with the surface from the “true eye” (shown as the left-most E).  This recursion happens five times in this implementation of a ray tracer, so objects 5 bounces away still have a small influence on the first object encountered by the eye, assuming that all of the objects in the chain of bounces are also reflective.

 

Ray Tracer Results

 

The Ray Tracer that I impelemented basically can parse most POV-Ray commands, but cannot display all of them.  The current implantation is limited to spheres and planes only.  The camera can be moved around using the LookAt and Location statements in the POV-Ray file.  All objects, including light sources, can be positioned anywhere in the world, but cannot be rotated or scaled in the current implementation.  The Ray Tracer implements all of the features listed in the above paragraphs.  The usage for the program is as follows: the first command line argument is the location and name of the file to be parsed (which should be in POV-Ray format), the second argument should be the width in pixels of the output picture, and the third argument should be the height in pixels of the output picture.  The width to height ratio should be kept to a standard aspect ratio (i.e. 800x600) for the output to look realistic and not squished.  The output file is written to the same location and file name as the input, with a “.ppm” extention.  All output files are in PPM format.

 

 

Screenshots

Testing depth with non-reflective objects

 

Testing reflection: the output differs from POV-Ray’s, but does not look extremely peculiar

 

Testing shadows on spheres and plane

 

Testing different colored lights: the light here is very red, which allows for the red specular highlight.

 

Testing lower positioned lights: the light here is positioned in between the two spheres.