I can only get shadow volumes to work like this. When the camera goes inside the volume, the shadow disappears. I have read several papers on “Carmack’s Reverse” but it results in no shadows when I use all the settings other people suggest. Please show me what to replace in this code to make it work right.
The object brush I draw at the end is an extruded shape that contains the entire shadow volume mesh. This will cover the entire area of the shadow on screen, so I use it instead of covering the whole screen with a quad.
'Draw shadows
glclear GL_STENCIL_BUFFER_BIT
glDisable GL_LIGHTING
glDepthMask GL_FALSE
glEnable GL_STENCIL_TEST
glColorMask GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE
glStencilFunc GL_ALWAYS,1,255
'First Pass. Increase Stencil Value In The Shadow
glFrontFace GL_CCW
glStencilOp GL_KEEP,GL_KEEP,GL_INCR
lightlink.shadowvolume.draw()
'Second Pass. Decrease Stencil Value In The Shadow
glFrontFace GL_CW
glStencilOp GL_KEEP,GL_KEEP,GL_DECR
lightlink.shadowvolume.draw()
'Final pass
glFrontFace GL_CCW
glColorMask GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE
glColor4f 0.0,0.0,0.0,a
glStencilFunc GL_NOTEQUAL,0,255
glStencilOp GL_KEEP,GL_KEEP,GL_KEEP
gldisable GL_DEPTH_TEST
'Draw object brush
glmatrixmode GL_MODELVIEW
glpushmatrix()
glloadidentity()
glRotatef camera.roll,0,0,1
glRotatef camera.pitch,1,0,0
glRotatef -camera.yaw,0,1,0
glTranslatef -camera.x,-camera.y,camera.z
glScalef 1,1,-1
lightlink.brushvolume.build()
For face:tface=EachIn lightlink.brushvolume.faces
If pointplanedistance(camera.x,camera.y,camera.z,face.nx,face.ny,face.nz,face.d)<0.0
glenableclientstate GL_VERTEX_ARRAY
glVertexPointer 3,GL_FLOAT,0,BankBuf(face.rendersurface.vertexarray)
glDrawElements GL_POLYGON,face.rendersurface.indicearray.size()/2,GL_UNSIGNED_SHORT,BankBuf(face.rendersurface.indicearray)
EndIf
Next
glmatrixmode GL_MODELVIEW
glpopmatrix()
'Restore Settings
glenable GL_DEPTH_TEST
gldisable GL_STENCIL_TEST
gldepthmask GL_TRUE
glcolor4f 1,1,1,1