Programming Assignment 1

The due date for this project is February 8, 2008 at 11:59pm. This project is worth 15% of your grade.

The goal of this assignment is to implement a basic raytracer that draws ellipsoids in 3D. A color will be defined for each ellipsoid. Your program should accept the following input in the given order:

So, if your executable is raycast and the input file is input, the command that specifies a single light source at 0.0, 0.0, 0.0 would be: raycast input 1 0.0 0.0 0.0

To raytrace a ellipsoid, you will need to intersect rays from the eye with the ellipsoid. Rays originate from the eye point and go through each pixel in your screen (which has a fixed resolution of 512 x 512). The ray is defined by the parametric line equation:

Parametric Line Equation:
x = eyex + vx t
y = eyey + vy t
z = eyez + vz t

In the above equation, (eyex, eyey, eyez) are the eye coordinates, and (vx, vy, vz) is the ray vector. An ellipsoid is defined by an implicit equation as follows:

Implicit ellipsoid Equation:
(x-Cx)2 / a2 + (y-Cy)2 / b2 + (z-Cz)2 / c2 = 1

In the above equation, the vector (Cx, Cy, Cz) is the ellipsoid center. (a, b, c) are the scaling (stretching) of the ellipsoid along principle axes. To compute the point of intersection (x, y, z) between the ray and the ellipsoid, plug the parametric line equations into the implicit ellipsoid equation, and solve for the parameter t. Once you have solved for t, plug t back into the parametric line equation to obtain the intersection point (x, y, z) between a ray and a ellipsoid. Note that there will often be two points of intersection. Choose the point that is closest to the eye (this is simply the point with the smallest t value). Be careful not to choose points behind the eye (with a negative t value).

To shade this intersection point, you will need to calculate the Normal vector for the intersection point. The screen pixel associated with the ray is shaded according to the position of the light source with respect to the intersection point and the Normal vector of the ellipsoid at the intersection point.


Camera and Screen Parameters

Your program must draw a window that is 512 x 512. The viewpoint is from a camera centered at (0,0,0) and facing down the negative z-axis. The viewing angle is 45 degrees centered around the z-axis. The image plane is centered at z = -1. Given the camera center, viewing angle, and location for the image plane, the actual size of the image plane is a square that is 0.83 x 0.83. You will need to scale your rays so that they are being shot from (0,0,0) through the 0.83 x 0.83 image plane and drawn correctly into the 512 x 512 window. Below is a side view of the camera and image plane locations. Note that the image plane spans from -0.414 to 0.414 along the y-axis. This is also true along the x-axis, resulting in an image plane of 0.83 x 0.83 in size centered around the z-axis.


Input Data:

The input data will be contained in one ascii file. One file may contain multiple ellipsoids (maximum 10). The format of the file is:
 

ellipsoid 3
0.0 5.0 -5.0 2.0 1.0 1.0 1.0 0.0 0.0
-5.0 0.0 -7.0 1.0 3.0 1.0 0.0 1.0 0.0
5.0 0.0 -3.0 1.0 1.0 4.0 0.0 0.0 1.0

The example file above defines three ellipsoids centered at the points (0, 5, -5), (-5, 0, -7), and (5, 0, -3) stretched along the x, y, and z axes respectively. The first line in the file gives the number of ellipsoids that are specified in the file. After this, each line contains the coordinates of the ellipsoid center, the stretch values, and the color (three floats for the 3D coordinate of the ellipsoid center, three floats for the scalings, and three floats for the red, green, and blue components of color).


Calculation of Normal Vector at Point of Intersection

The Normal vector for a point on the ellipsoid surface is found by taking the derivative of the implicit ellipsoid equation with respect to x, y, and z. This results in the following equation:

N = {Nx, Ny, Nz} = {  2(x-Cx) / a2,   2(y-Cy) / b2,   2(z-Cz) / c2  }

To obtain a unit Normal vector, each component of the vector must be divided by the magnitude of the vector:

N^ = {N^x, N^y, N^z} = {  Nx,   Ny,   Nz} / || {  Nx,   Ny,   Nz} ||


Shading

In diffuse illumination, the intensity of a surface point depends on the direction of the light source with respect to the surface Normal vector at the the surface point. A point on the surface of the ellipsoid is shaded according to the following equation and diagram:

Pshaded = Pcolor (N^ * L^)

In the above equation,

Note that (N^ * L^) should always fall between 0.0 and 1.0.


Shadows

In implementing shadows, you will need to cast a ray from the point of intersection on the surface to each light source position. If the ray intersects any other ellipsoid before reaching the light source, then the surface point that you are shading will be in shadow with respect to that light source. The shadowing can be implemented by clamping (N^ * L^i) to 0.0 for light source i. Suppose that you only have one light source, then if the point of intersection on the surface is in shadow, Pcolor will essentially be 0.0. If you wish, you can have a minimum color so that Pcolor is at minimum 0.1 or some other small value.


Extra Credit

Multiple Light Sources: 15 points

Modify your ray-caster so that it can accept multiple light sources from the command line (see Implementation Requirements below for command line syntax). In implementing multiple light sources, you will need to modify shading so that it takes into account other light sources as follows. The equation for shading would now be:

Pshaded = Pcolor [(N^ * L^1) + (N^ * L^2) + ... + (N^ * L^n)]

In the above equation,

Shadowing will also have to be tested against each light source. If the ray from the intersection point to a light source i is blocked by another ellipsoid, then the shading contribution from that light source should be clamped to 0.0 (or some small minimum value such as 0.1). In other words, N^ * L^i = 0.0 if the intersection point is in shadow with respect to light source i.

Note that in the above equation, the summation of the dot products [(N^ * L^1) + (N^ * L^2) + ... + (N^ * L^n)] may be more than 1.0. If so, clamp the sum to 1.0 so that the max color is Pcolor.


Sample Output for SPHERES:

Test 1

Test 2


Implementation Requirements:

(Some of these requirements are for ease of grading)

Grade Based On: Submission: Hints and help:



Back to Class Page