Home Page

Home Page

Comp 236

Programming Assignment 4:  Ray Tracing

Page 1

Features

  • Simple scene description language (SDL)
  • Adaptive antialiasing
  • Object-oriented design
  • Camera that allows arbitrary position, look at point, and focal length
  • Phong illumination model
  • Shadows and reflections
  • Spheres and planes

Code Specifics

The code is written in C++. It contains a few extras, most notably reflections, adaptive antialiasing, a simple scene description language (SDL), and a clean object-oriented design. The primary components are a new and improved camera class, a scene class, a sphere class, and a plane class. The new camera class cleans up the "one true way" math implementation of the original version from Programming Assignment 1. It also implements the adaptive antialiasing scheme. Pseudocode for this scheme is given below:

    //***
    // At the end of this loop, we will have values for the 
    // four corners of every pixel.
    //***
    For each scan line (plus one extra)
    {
        For each pixel in the current scan line (plus one extra)
        {
            Send ray through lower left corner of pixel
        }
    }

    //***
    // Now, perform adaptive antialiasing.
    //***
    For each scan line
    {
        For each pixel in the current scan line
        {
            Calculate mean val of the four corners
            Calculate the variance
            If low variance (i.e. low contrast)
            {
                Just use the average of the four corners
            }
            Else
            {
                Fire more rays and convolve results with Gaussian
            }
        }
    }

The scene class features an overloaded stream extraction operator for reading in SDL commands (e.g. from a text file). It also features the method for calculating the color value for a particular ray using the Phong illumination model. Pseudocode for this method is given below:

    Init answer to background color.

    For each object in the scene
    {
        Check if object is hit by ray
        Find closest (i.e. smallest t > 0) of all the hits
    }

    If ray hit something
    {
        For each light source
        {
            Check if hit point is in shadow
            If not, calculate diffuse and specular components
            Total diffuse += current diffuse
            Total specular += current specular
        }

        // Reflections
        If recurse level == max recurse level
        {
            Answer = ambient + total diffuse + total specular
        }
        Else
        {
            If object is reflective enough
            {
                Increment recurse level
                Spawn a new ray in direction of perfect reflection
                Answer = ambient + total diffuse + total specular + reflected
            }
            Else
            {
                Answer = ambient + total diffuse + total specular
            }
        }
    }

The sphere and plane classes are derived from the GeomObj (for geometric object) abstract base class. The GeomObj class contains pure virtual functions for two hit methods. The intent is that each derived class will implement a "full up" hit method for calculating the intersection with a ray and storing the t value, hit point, and normal. Each derived class will also implement a "stripped down" hit method that just calculates the intersection (used for shadow rays).

Defining an abstract base class and then deriving classes for specific geometric objects (e.g. sphere, plane, polygonal mesh) allows the scene class (which holds the object list) to make calls to the hit methods through pointers to the abstract base class. The correct hit method is called through the magic of polymorphism.

I also wrote a simple Matlab script to generate SDL for the one cubic meter volume containing 100 spheres with random position, size, color, and surface shading properties. The script also places five point light sources at random locations outside the volume. The results are written to scene.txt in the current Matlab directory.

Email Jason Stewart