Primary Rays: starting from eye point, going through lower left corner of each pixel on the canvas.
Secondary Rays: When a primary ray hits the geometry, there is a secondary ray starting at the hit point, and going through the light source position.
Yesterday, we were using the secondary ray (we called it the lighting direction) to compute the color on the hit point. Now, an obvious observation is that the point should not be given any color if the secondary ray hit some geometry before it reaches the light source (i.e., t<1 in our hitSphere() function). Such points form the shadow area in the final image.
Now we try to ray trace more than one sphere.
foreach pixel construct the primary ray starting at eye and going through the lower left corner of the pixel. foreach sphere if the primary ray hit it, if this hit pnt has a larger z value (i.e. closer to eye) than that of the current hit pnt. set current hit pnt to this hit pnt. compute the normal direction and light direction at the hit pnt. set color of this pixel to the returned value of the lighting computation.
The above code does not take care of shadow effect. This one does secondary ray hit detection, and should generate shadow effect if there is any.
foreach pixel construct the primary ray starting at eye and going through the lower left corner of the pixel. foreach sphere if the primary ray hit it, if this hit pnt has a larger z value (i.e. closer to eye) than that of the current hit pnt. set current hit pnt to this hit pnt. given the current hit pnt, and the light position, construct a secondary ray. foreach sphere if the secondary ray hit it with a t value in (0,1) the original hit pnt is in shadow this pixel color is set to background, or something like material_col * 0.1; go to the outer loop to trace another primary ray; compute the normal direction and light direction at the hit pnt. set color of this pixel to the returned value of the lighting computation.
As you have already noticed in the above code, you have to make a little change to the hitSphere() function, so that the caller have a way to know the actual t value if there is a hit.
1.3.6