Sign in to follow this  
Christobal_de_M

Depth pass (z-pass) stencil shadow problem

Recommended Posts

Hello, I try make ease stencil shadow implementation, but I got some problem in shadow silhouette. This silhouette of all objects painting at the far clipping plane (?). What I do wrong ? Here is sample picture http://img1.imgup.eu/shadow_problem.jpg (red color is shadowed area) Than You very much for any ideas.

Share this post


Link to post
Share on other sites
Nothing is wrong, it just works like that. The shadow extends towards infinity, and that area at the far clip-plane will be shadowed. Normally you won't want to paint the shadow, but rather the light, so then it won't be a problem as only lit objects can be shadowed. That is, you draw your stencil shadows, and then draw light only where there is NOT any shadow.
Something like:
1. Draw ambient lit objects
2. Repeat for each light:
2.1 Draw shadows
2.2 Draw light with additive blending where there is no shadow

Share this post


Link to post
Share on other sites
Thanks, I am sorry for my ignorance about this case (and about my poor English language too ,-), BUT. From Your notes is almost clear for me, only step 2.2 is covered into the dark for my sight. (and my wisdom) ;-) OK, I rendering all objects with ambient lighting and then I do for my first light and first object activate stencil buffer like that :

glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glDepthMask(GL_FALSE);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
glColorMask(0, 0, 0, 0);
glStencilFunc(GL_ALWAYS, 1, 0xffffffff);
glEnable(GL_CULL_FACE);


Then I cull back face and increment pass:

glCullFace(GL_BACK);
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);

..and render shadow siluette of object

For front face decrement pass:

glCullFace(GL_FRONT);
glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);

...render siluette


And finaly I rendering shadow as screen aligned box covered whole screen :

glCullFace(GL_BACK);
glColorMask(1, 1, 1, 1);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glStencilFunc(GL_NOTEQUAL, 0, 0xffffffff);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

glPushMatrix();
glLoadIdentity();
glColor4f(1.0, 0.0, 0.0, 1.0);
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(-0.1, 0.1,-0.1);
glVertex3f(-0.1,-0.1,-0.1);
glVertex3f( 0.1, 0.1,-0.1);
glVertex3f( 0.1,-0.1,-0.1);
glEnd();
glPopMatrix();

glDisable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glEnable(GL_LIGHTING);
glDepthMask(GL_TRUE);
glDisable(GL_STENCIL_TEST);


That is all what I do. Please, where is in this case your step 2.2 ? .-)

Share this post


Link to post
Share on other sites
With the method I mentioned you shouldn't draw the shadow. Instead you first draw all objects with the shadow color (say maybe 0.3f gray, lighting disabled).
Then you draw the stencil volume, and then enable blending with (GL_ONE, GL_ONE), and enable lighting and draw the light-color on top, with stencil-func GL_EQUAL. So that instead of making the shadow darker, you start with everything dark, and make the lit parts lighter instead.

Share this post


Link to post
Share on other sites
Thank You again, but I resolve this stencil efect another way yet. I fill whole screen second rectangle nearest to the far clipping plane. My value for far clipping is 1000.0.
...
...and finaly I rendering shadow at screen aligned box covered whole screen + yet another "finished" rectangle :

glCullFace(GL_BACK);
glColorMask(1, 1, 1, 1);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glStencilFunc(GL_NOTEQUAL, 0, 0xffffffff);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

glPushMatrix();
glLoadIdentity();
glColor4f(1.0, 0.0, 0.0, 1.0);
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(-0.1, 0.1,-0.1);
glVertex3f(-0.1,-0.1,-0.1);
glVertex3f( 0.1, 0.1,-0.1);
glVertex3f( 0.1,-0.1,-0.1);
glEnd();
glPopMatrix();

glDisable(GL_BLEND);
glDepthMask(GL_TRUE);
glDisable(GL_STENCIL_TEST);
glEnable(GL_TEXTURE_2D);

glPushMatrix();
glLoadIdentity();
glColor4f(0.7, 0.6, 0.95, 1.0); //or use some sky texture
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(-1000.0, 1000.0,-1000.0);
glVertex3f(-1000.0,-1000.0,-1000.0);
glVertex3f( 1000.0, 1000.0,-1000.0);
glVertex3f( 1000.0,-1000.0,-1000.0);
glEnd();
glPopMatrix();
glEnable(GL_LIGHTING);

Here is result, shadows without stencil siluette at the far clipping plane :

http://img6.imgup.eu/stencil_shadow.jpg

Thank You for Your patience.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this