PDA

View Full Version : Problem with stencilling



Chaldrax
07-19-2011, 12:01 PM
I'm trying to use stencilling to control where my reflected object is visible like I've seen in several tutorials, but I can't seem to prevent the reflection from showing up at all. Here's the relevant code snippet:


glClear(GL_STENCIL_BUFFER_BIT);

//disable drawing
glDisable(GL_DEPTH_TEST);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

//enable stencilling
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glStencilFunc(GL_ALWAYS, 1, 1);

//would draw stencil for reflective surface here

//reenable drawing
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glEnable(GL_DEPTH_TEST);

//set up stencil
glStencilFunc(GL_EQUAL, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

draw_reflection();

//disable stencilling
glDisable(GL_STENCIL_TEST);
From what I understand of stencilling, since I haven't specified any area to have a stencil value of 1, the object from draw_reflection shouldn't be drawn at all. When I run the code, the reflected object shows up unchanged. What am I not understanding here?

Y-tension
07-20-2011, 03:43 AM
Try glClearStencil(0) before glClear(GL_STENCIL_BUFFER_BIT).

Chaldrax
07-20-2011, 04:00 AM
Thanks for the tip, but that didn't change anything.

ZbuffeR
07-20-2011, 04:24 AM
Do you actually have a stencil buffer ?
You should ask for it at context creation (like double buffering, depth buffer, etc) otherwise the driver may or may not create one for you.
Example for glut :
glutInitDisplayMode(GLUT_DOUBLE|GLUT_STENCIL|GLUT_ DEPTH|GLUT_ALPHA);

Then once the gl window is created, you can verify how many stencil bits you actually got :

int stencilBits=-1;
glGetIntegerv(GL_STENCIL_BITS, &stencilBits);

If stencilBits is 0, then you effectively have no stencil buffer available.

Chaldrax
07-20-2011, 05:32 AM
Yep, that did it. Thanks for the help. Embarrassing to have missed something so simple. >.<