PDA

View Full Version : Stencil Buffer Not Masking



Swiftless
09-16-2005, 02:43 AM
Hi, I am learning to use the stencil buffer, but no matter what tutorial I follow I always have the problem of the stencil buffer not masking. So things that I draw that are not supposed to go outside the mask, do.

My Display Code is as:


glClearStencil(0);
glClearDepth(1.0f);
glClearColor (0.0,0.0,0.0,0.5);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glLoadIdentity();

glTranslatef(0,0,-5);

glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);

glBegin(GL_QUADS);
glVertex3f(-1, 0, -7);
glVertex3f(1, 0, -7);
glVertex3f(1, -2, 0);
glVertex3f(-1, -2, 0);
glEnd();

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

glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

glPushMatrix();
glScalef(1.0f, -1.0f, 1.0f);
glTranslatef(0,2,-1);
glRotatef(rot,0,1,0);
square();
glPopMatrix();

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glColor4f(1,1,1,0.4);
glBindTexture(GL_TEXTURE_2D,texture[1]);
base();

glDisable(GL_BLEND);
glDisable(GL_STENCIL_TEST);

glPushMatrix();
glTranslatef(0,1,-1);
glRotatef(rot,0,1,0);
square();
glPopMatrix();
glutSwapBuffers();
rot++;
Any yet it draws my square(the one drawn in the stencil buffer) outside the supposed mask which is the same dimensions as base() if that means anything.

Thanks

Relic
09-16-2005, 04:25 AM
Check glGetIntegerv(GL_STENCIL_BITS, &stencilBits).
If it returns 0, you have failed to select a pixelformat with stencil bits.

Swiftless
09-16-2005, 09:35 PM
Ok, tried it and it does return 0. So how do I select a pixel format with stencil bits? (I am using GLUT)

Lol don't worry. I just realised looking at my code, that I was setting the stencil buffer in the display modes. Sorry for the trouble.