Homework 3

  1. Modify your sphere ray tracer so that the user can fly the camera around the scene, looking at the spheres from any angle. Do this as follows:

    • Cluster all the spheres near the origin (0,0,0).

    • Keep the aim point as (0,0,0).

    • Initially, place the camera view point at (0,0,10). Then, in response to user mouse clicks (I suggest you check for the mouseUp event), rotate the camera position, so that it rotates about the Y axis as the user clicks to the left or right of center of the image, and rotates about the X axis as the user clicks above or below the center of the image. As the view point rotates under user control, it should always remain 10 units away from the origin.

      Hint: Keep two running angles theta and phi for the rotation about the Y axis and X axis, respectively. At each frame, construct a camera transform matrix, as a translation by (0,0,10), followed by rotations of theta and phi about the X and Y axes, respectively. Then use the procedure we learned in class to obtain the inverse of this matrix. Transform each sphere center by that inverse matrix to move all the spheres into view, and render the image.


  2. Shade your spheres using diffuse Gouraud shading (ie: assume that the spheres are all perfect diffuse Lambert surfaces). You will recall that this just requires taking a dot product of the surface normal with the light direction. Use a light source coming from the top,right,forward direction. You can obtain this light direction vector by normalizing the vector (1,1,1).

    The complete procedure to follow at each pixel is:

    1. For this pixel, find the first sphere intersected by the ray, as in the previous assignment (ie: the sphere whose front surface produces the smallest t parameter). If your ray has missed all the spheres, then just color this pixel black, and go on to the next pixel.

    2. Plug t back into the ray equation to reconstruct the (x,y,z) location of this point on the sphere's surface.

    3. Subtract, from this surface point, the location of the sphere's center. The result will be a vector in the direction of the surface normal. Normalize this vector to obtain the surface normal.

    4. Take the dot product between the surface normal and the light direction. If this dot product is less than zero (ie: if the surface is facing away from the light source), then set it to zero. You should now have a value C between 0.0 and 1.0.

    5. Assign a color of 0.2 + 0.8*C to the pixel. The 0.2 provides a simple approximation to "ambient" light.