graphicsDeploy index


phase 9:
shadowing

cs40 assignment 9 web page


overview

well... the gist of this assignment was to integrate z-buffering and illumination code with the hierarchical modeling code, but i really already did that, so instead i have added phong shading and shadow rays. hold on tight, its going to be a wild ride.

a little testament to successfully integrated code is the above animation, which depicts a cone and square with green and blue light sources, with the y and z rotations of the cone animated with keyframing. the source code which generated the animation is available here (except that the colors aren't quite the same because i had been fiddling with it...)


phong shading

whereas gouraud shading works by determining, for each triangle drawn, the color at each vertex of the triangle and then interpolating color across the surface of the triangle, phong shading works by determining the surface normal and world coordinates for each vertex and then interpolating surface normals and world coordinates across the surface. For many applications, gouraud shading is acceptable, but in cases where there are significant changes in illumination within a single triangle (eg. sharp specularities or shadow boundaries) you can get undesirable results. and since i wanted to add shadows to my system, i first needed to add phong shading...

the following images demonstrate a few of the pitfalls which can arise with gouraud shading. the images on the top were rendered using phong shading, the images on the bottom with gouraud shading. in the images on the left, a point light source was located at (3.2, 3.0, -3.2), and in the images on the right, the light was at (2.6, 3.0, -2.6). as you can see, on the left the gouraud shading inappropriately diffuses the shadow across the square, and on the right the gouraud shading misses the shadow entirely. thus if the light were animated we would see the shadow flicker when it passed over the square's vertices.




shadow rays

there are lots of ways to implement shadowing in a z-buffer context. i chose to use shadow rays because, as you will see, they allow you to get nice soft shadows, are easily integrated with transparency, and avoid many of the other pitfalls involved in using shadow polygons or shadow volumes.

the basic idea behind shadow rays is that, to determine the amount of light hitting a particular pixel, you send out a 'shadow ray' from the corresponding world coordinates of that pixel in the direction of each light source (with a little jittering to handle our favorite problem of aliasing), and check for intersections with all triangles in the scene which cast shadows. fairly straightforward, but believe me it can be incredibly computationally expensive.

as mentioned, one advantage of using shadow rays is it becomes fairly easy to generate soft shadows. we do this by creating area- or volume- based light sources which emit light from multiple points in space, then send multiple shadow rays to points distributed throughout the area or volume (using uniform jittered sampling). i implemented this by converting area or volume lights into equivalent sets of point lights.

in the following pair of images, the first was done using a single point light, and the second with a circular area light (in this case, broken down into 18 point lights, corresponding to 18 shadow rays, and thus approximately 18 times the computation time...)

the following image demonstrates the combined use of shadows and transparency; the cone is opaque to green light but mostly transparent to blue light, and it is colored mostly green with some blue. the square is colored white. the light is equal parts blue and green. we end up seeing the cone as mostly green, and casting a blue shadow (because it passes blue light). we also see the edge of the square through the cone as blue (again because the cone passes blue light). there is a slight artifact within the cone's shadow which seems to result from shadow rays in that region only intersecting the cone once rather than twice as they should.




graphicsDeploy index