Hey,
I seem to have run into a little problem with my stencil shadow implementation - namely that the ZFail capping seems to have gone a bit wonky of late.
Basically, the problem is this: I can see the dark cap's silhouette through my shadow.
a bit like this [Apologies for the poor screengrab, its late, i'm tired, and i've been trying to fix this for a good few hours]
Any ideas? The code i'm using to calculate my caps is as follows:
for(unsigned int i=0; i<m_uiNumTriangles; ++i)
{
for(int j=0; j<3; ++j)
{
vertex = pModel->m_KeyFrames[m_uiFrame].Vertices[pModel->m_Indices[i*3+j]];
if(m_bIsFacingLight==true)
{
//Draw Light Cap
_destLight->AddSingle(vec4f(vertex.x,vertex.y,vertex.z,1.0f)*modelview);
}
else
{
//Draw End Cap
_destDark->AddSingle(vec4f( vertex.x-m_RelPoint.x,vertex.y-m_RelPoint.y,vertex.z-m_RelPoint.z, 0.0f)*modelview);
}
}
}
that m_RelPos position is being calculated earlier as follows:
//convert position into object vertex space
//Reverse Object Rotations!
mat4f inverseMatrix;
inverseMatrix = m_Rotation.Inverse();
//Calc Relative Local Position!
m_RelPoint = inverseMatrix*_Point;
m_RelPoint = m_RelPoint-_Position;
that cap draw function adds the vertices it calculates into a couple of buffers elsewhere, which are later rendererd via VB's. The rendering happens as follows:
glPushAttrib(GL_ALL_ATTRIB_BITS);
//Disable Useless Stuff
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
//Set Shade Model
glPolygonMode(GL_BACK,GL_FILL);
glShadeModel(GL_FLAT);
//Disable Writing to Colour Buffers, and set no Depth buffer mask
glColorMask(0, 0, 0, 0);
glDepthMask(GL_FALSE);
//Enable Culling, and set Counter-Clockwise as Front Faces
glEnable(GL_CULL_FACE);
glFrontFace(GL_CW);
//stencil clear value
glClearStencil(1);
//Stencil test always passes, for all mask bits
glStencilFunc(GL_ALWAYS, 1, 0xffffffff);
//Clear Buffer
glClear(GL_STENCIL_BUFFER_BIT);
//Enable Stencil
glEnable(GL_STENCIL_TEST);
//setup culling
glCullFace( GL_FRONT );
glStencilOp(GL_KEEP,GL_INCR,GL_KEEP);
g_Renderer->DrawBufferVec4f( &m_Shadows, GL_QUADS );
g_Renderer->DrawBufferVec4f( &m_ShadowLightCaps, GL_TRIANGLES );
g_Renderer->DrawBufferVec4f( &m_ShadowDarkCaps, GL_TRIANGLES );
glCullFace( GL_BACK );
glStencilOp(GL_KEEP,GL_DECR,GL_KEEP);
g_Renderer->DrawBufferVec4f( &m_Shadows, GL_QUADS );
g_Renderer->DrawBufferVec4f( &m_ShadowLightCaps, GL_TRIANGLES );
g_Renderer->DrawBufferVec4f( &m_ShadowDarkCaps, GL_TRIANGLES );
g_Renderer->ShadowZFailEnd();
that final function just takes care of drawing a blended shadowing quad over the viewport and resets states etc.
So, any ideas? This has been bugging me all day, and i just can't seem to get my head around it.
As always, any help and suggestions would be greatly appreciated.
Cheers,
Ben