Skip to main content
GameDev.net gamedev.net
🔒 Locked

Stencil Buffer: Two shadows = No shadow

Started by sorin_g Apr 10, 2007 at 1:46 PM 2 replies 800+ views
Original Post
sorin_g
sorin_g
Hello everyone! I'm trying to render a scene with 2 cubes and a light source. I'm using the stencil buffer to create shadow volumes... The light is above the 2 cubes and each cube creates a shadow. The problem is that when the 2 shadows fall on the same piece of surface, the scene looks like I have no shadow there: http://geocities.com/gsmcreation/untitled.GIF Could anyone tell me what I'm doing wrong in this code? glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glClearStencil(0); glDisable(GL_LIGHT0); drawScene(); glEnable(GL_STENCIL_TEST); glDepthMask(GL_FALSE); glStencilFunc(GL_ALWAYS, 1, 0xffffffff); glStencilOp(GL_KEEP, GL_KEEP, GL_DECR); glCullFace(GL_BACK); glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); // draw the shadow volumes pass 1 makeShadowVolume(objects[1], light_position0, 15.f,0); makeShadowVolume(objects[0], light_position0, 15.f,0); glStencilOp(GL_KEEP, GL_KEEP, GL_INCR); glCullFace(GL_FRONT); glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); // draw the shadow volumes pass 2: makeShadowVolume(objects[1], light_position0, 15.f,0); makeShadowVolume(objects[0], light_position0, 15.f,0); glDepthMask(GL_TRUE); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glCullFace(GL_BACK); glDepthFunc(GL_LEQUAL); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); // HERE MAY BE THE PROBLEM: glStencilFunc( GL_GEQUAL, 0, 1); glEnable(GL_LIGHT0); drawScene(); glDisable(GL_STENCIL_TEST); glDepthMask(GL_TRUE); glDepthFunc(GL_LESS); Thank you.
Steaua Kernel
AndyEsser
AndyEsser
I believe (but not certain) this is caused by doing a Stencil test on a part of the buffer that's already been tested which results in the value being 0 (as if no shadow was cast on that point). John Carmack (Doom dude) found a way around this but I can't remember it off by heart.

Sorry to not actually be much help. But try googling for Carmack Shadow Volumes
sorin_g
sorin_g
Thank you, Mr Esser and Mr Lengyel. The "WRAP" option solved the problem.
Here is the correct code, in may prove useful to someone:

glDisable(GL_LIGHT0);
drawScene();

glPushAttrib( GL_ALL_ATTRIB_BITS);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glEnable( GL_DEPTH_TEST);
glDepthMask(GL_FALSE);

glEnable( GL_CULL_FACE);
glEnable(GL_STENCIL_TEST);

glCullFace( GL_FRONT);
glStencilFunc( GL_ALWAYS, 0, 0xffffffff);
glStencilOp( GL_KEEP, GL_KEEP, GL_DECR_WRAP);
makeShadowVolume(objects[1], light_position0, 15.f,0);
makeShadowVolume(objects[0], light_position0, 15.f,0);

glCullFace( GL_BACK);
glStencilFunc( GL_ALWAYS, 0, 0xffffffff);
glStencilOp( GL_KEEP, GL_KEEP, GL_INCR_WRAP);
makeShadowVolume(objects[1], light_position0, 15.f,0);
makeShadowVolume(objects[0], light_position0, 15.f,0);

glDisable(GL_CULL_FACE);

glDepthMask(GL_TRUE);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

glCullFace(GL_BACK);
glDepthFunc(GL_LEQUAL);
glStencilFunc(GL_EQUAL, 0, 0xffffffff);
glStencilOp( GL_KEEP, GL_KEEP,GL_KEEP);

glEnable(GL_LIGHT0);
drawScene();

glDisable(GL_STENCIL_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);

makeShadowVolume(objects[1], light_position0, 15.f,1);
makeShadowVolume(objects[0], light_position0, 15.f,1);
Steaua Kernel

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.