PDA

View Full Version : Optimizing shadows



ioquan
09-20-2002, 01:20 PM
I want to cast a realistic shadow of a model on to my terrain meshes.

I have a very fast method of finding which triangles in the mesh are within a certain radius of my model. This was something I implemented for my collision detection. Briefly, here is how I do it. Each terrain mesh has a hierarchical box around it. The box is divided into four smaller boxes, each of those is divided into four smaller boxes, and so on. For the smallest boxes, I precalculate which triangles are inside them. I have a recursive algorithm which can quickly search through the boxes, and return a list of triangles which are close to the model. Once I have this list, I can loop through them and do a bounding sphere test to weed out all of those which are not within a given radius.

Now I have some triangles, and I'm ready to draw a shadow. I can make a matrix which will squish my model into the plane of one of the triangles, based on a light source.

I thought I would just stencil each of the triangles, then draw the shadow for each of them. The problem is that this is requiring me to draw the model 20-30 times, once for each triangle. Furthermore, some of the triangles which are within the radius wont even have a shadow drawn on them at all, yet I am still having to tell OpenGL to try and draw it.

So, what is the best way to optimize this? Should I do further checks to make sure that a given triangle will in fact recieve a shadow? This would involve testing each of the squish transformed vertices to find if any are within the boundaries of the polygon. Would that be worth it?

Also, is there some way I can cheat and just draw the object once for a few triangles (perhaps if their slope is very close).

Any optimization techniques you may have for this would be appreciated.

jwatte
09-20-2002, 07:50 PM
Render your model all flat gray to a texture, as seen from the light source (sun).

When rendering terrain, project this texture using multi-texturing onto the ground. Or, render all the "colliding" triangles with this texture projected to them, in modulating mode.

This actually works very well with translucent objects, too, where translucent parts are their regular color (rather than flat grey :-). It just doesn't work all that well on the object itself for self-shadowing :-(

ioquan
09-21-2002, 01:09 AM
Ahhh, now that's a good idea. Perhaps not as realistic as the other method, but certainly much, much faster. Thanks, I'll give it a try!