I’m coding a simple car racing game. While trying to add shadows in my scene, by drawing black transparent polygons, i found myself stuck in a strange problem.
When shadows are drawn on horizontal surfaces, when i look at them from far away, i can see through the surfaces as if they were transparent.
void PolyShadow::Draw(){
glColor4f(0,0,0,0.5f);
glEnable (GL_BLEND);
glBlendFunc (GL_DST_COLOR,GL_ONE_MINUS_SRC_ALPHA);
this->drawShadow();
glDisable (GL_BLEND);
shadow_initialized = true;
}
To avoid conflicts i assign to any object projecting shadows a different shadow_offset and make this call before drawing the shadow
glEnable(GL_POLYGON_OFFSET_FILL);
//call to object.drawShadows()
glDisable(GL_POLYGON_OFFSET_FILL);
And
void Object::drawShadows(){
glPolygonOffset(-1.0-shadow_offset,-1.0-shadow_offset);
//Draw shadow
}
I also tried with
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
but nothing changed.
Thanks in advance
When you use blending you need to render back-to-front. So you need to z-order your models or even triangles.
Typically you first render all opaque stuff in arbitrary order, and then blended stuff back-to-front.
Since all shadows have the same color, isn’t that bad for you. Just render the shadows after rendering the geometry.
There are other methods for rendering shadows which look better and don’t have this problem(for example marking the shadowed parts of the geometry in the stencil buffer).
There are some techniques to render complicated alpha-blended scenes (for example “Depth peeling”) which avoid the ordering problem and even work for intersecting geometry, but they are rather expensive.