Sign in to follow this  

GL projection shadows w/ stencil buffer

This topic is 3309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, I've been working implementing projection shadows mainly for projecting multiple faces and I indeed did implement it and so far it looks like its a success however; there is a problem in the projection matrix calculation that I fixed but the problem is that I don't understand why the fix works. I am using the well-known matrix with parameters of a plane in the form of a*x + b*y + c*z + d = 0, a point P on the plane and light position L. Plane normal N is normalized( a, b, c ) and d = -dot( N, P ). Vec4D for planeNormal is ( a, b, c, d ) and lightPos is ( x, y, z, 1 ). Below is the matrix calculation. Renderer::_calculateShadowMatrix( Vec4D lightPos, Vec4D planeNormal ) { float dotPL = dot( planeNormal, lightPos ); shadowMatrix_.x11 = dotPL - lightPos.x * planeNormal.x; shadowMatrix_.x12 = -lightPos.x * planeNormal.y; shadowMatrix_.x13 = -lightPos.x * planeNormal.z; shadowMatrix_.x14 = -lightPos.x * planeNormal.w; shadowMatrix_.x21 = -lightPos.y * planeNormal.x; shadowMatrix_.x22 = dotPL - lightPos.y * planeNormal.y; shadowMatrix_.x23 = -lightPos.y * planeNormal.z; shadowMatrix_.x24 = -lightPos.y * planeNormal.w; shadowMatrix_.x31 = -lightPos.z * planeNormal.x; shadowMatrix_.x32 = -lightPos.z * planeNormal.y; shadowMatrix_.x33 = dotPL - lightPos.z * planeNormal.z; shadowMatrix_.x34 = -lightPos.z * planeNormal.w; shadowMatrix_.x41 = -lightPos.w * planeNormal.x; shadowMatrix_.x42 = -lightPos.w * planeNormal.y; shadowMatrix_.x43 = -lightPos.w * planeNormal.z; shadowMatrix_.x44 = dotPL - lightPos.w * planeNormal.w; } If I directly use it as it is, some shadows slide across the surface and they even move with the camera. Image Hosted by I fixed it by scaling the d of plane with a trial-error value as below which happened to be 0.75: polyNormal.w = -dot( polyNormal, point ) * 0.75f; Image Hosted by And with that it works properly so far. I tested it on several situations and it does work fine. Though I can't really explain why, I suspect that after transforming the normals of a face using the mesh's transformation, a 0 may turn into a very small number close to 0 such as -1.15e16 and I wonder if that may build up to an error. Can anyone shed a light on it? Thanks.

Share this post

Link to post
Share on other sites
Sign in to follow this