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.