I upload data to the VBO every loop because they are dynamic (stencil shadows).
I got it to work (with an improvement of about 25% respect to immediate mode) as it follows:
void RenderShadows()
{
glDepthMask(GL_FALSE);
glDepthFunc(GL_LEQUAL);
glEnable(GL_STENCIL_TEST);
glColorMask(0, 0, 0, 0);
glStencilFunc(GL_ALWAYS, 1, 0xffffffff);
// 1st pass
glFrontFace(GL_CCW);
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(dati_ombra), dati_ombra, GL_DYNAMIC_DRAW_ARB); //swiched from STATIC to DYNAMIC
glBindBufferARB(GL_ARRAY_BUFFER_ARB, buffer);
glVertexPointer( 3, GL_FLOAT, 0, (char ) NULL );
glDrawArrays(GL_QUADS, 0, polys_to_be_drawn4);
}
// 2nd pass
glFrontFace(GL_CW);
glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
glDrawArrays(GL_QUADS, 0, polys_to_be_drawn*4);
glDisableClientState( GL_VERTEX_ARRAY );
//draw a shadowing rectangle covering the entire screen
glDisable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glStencilFunc(GL_NOTEQUAL, 0, 0xffffffff);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glPushMatrix();
glLoadIdentity();
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(-0.1f, 0.1f,-0.10f);
glVertex3f(-0.1f,-0.1f,-0.10f);
glVertex3f( 0.1f, 0.1f,-0.10f);
glVertex3f( 0.1f,-0.1f,-0.10f);
glEnd();
glFrontFace(GL_CCW);
glColorMask(1, 1, 1, 1);
}
Probably the code can be still optimized some more, but i’m not very experienced.
Also, i still have a problem with infinite shadow volumes: i cant set them up, i have read also some articles on Gamasutra but did not understand much of it. I’m going to ask this in a separate thread.