• 15
• 15
• 11
• 9
• 10

# Stencil buffers and masking out a region

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

## Recommended Posts

Hello, I would like to draw a scene with a circular cutout in the middle that has the same scene rotated 90 degrees. To do this, i'm trying to use the stencil buffer. My attempt isn't working. Instead of getting a scene drawn within a scene, i just get everything drawn twice, at 90 degree angles to each other (a very confusing sight) I think some part of my understanding of the stencil buffer may not be correct. Here is annotated code that does not work:
So, I set out to fill the stencil:
Gl.glDisable(Gl.GL_DEPTH_TEST);
Gl.glStencilFunc(Gl.GL_ALWAYS, 1, 1);
Gl.glStencilOp(Gl.GL_REPLACE, Gl.GL_REPLACE, Gl.GL_REPLACE);

Then i draw the circle in the middle of the scene using a triangle fan:
Utilities.Ogl.drawSolidCircle(1);

Then i set up to draw the interior circle:
Gl.glEnable(Gl.GL_DEPTH_TEST);
Gl.glStencilOp(Gl.GL_KEEP, Gl.GL_KEEP, Gl.GL_KEEP);
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
Utilities.Ogl.drawWireCircle(1);

Gl.glPushMatrix();
Gl.glTranslatef( TransXYZ[0], TransXYZ[1], -TransXYZ[2] );

Gl.glScalef( Convert.ToSingle(4.0F / (scale * Width) ), Convert.ToSingle(4.0F / (scale * Height)) , 1 );

Gl.glStencilFunc(Gl.GL_EQUAL, 1, 1);

Then I draw the scene
drawMap();

Gl.glPopMatrix();

Gl.glRotatef(90F,0,0,1);

/* Translate the scene */
Gl.glTranslatef( TransXYZ[0], TransXYZ[1], -TransXYZ[2] );

/* scale the scene:						*/
Gl.glScalef( Convert.ToSingle(4.0F / (scale * Height)), Convert.ToSingle(4.0F / (scale * Width) ) , 1 );

I set up to draw the outer part of the circle
Gl.glStencilFunc(Gl.GL_NOTEQUAL, 1, 1);

and i draw the scene again.
drawMap();


##### Share on other sites
right, i've had a think about this, now the first value of glStencilOp() is applied whenever the stencil test fails, however in your glStencilFunc() call you've got it set to GL_ALWAYS, which if i'm reading this right means it will always pass the test, so try changing that to GL_NEVER

Edit: heh, turns out i'm tired and havent played with the stencil buffer enuff, for future reference this NeHe tutorial covers the same kinda masking effect

##### Share on other sites
Quote:
 Original post by _the_phantom_right, i've had a think about this, now the first value of glStencilOp() is applied whenever the stencil test fails, however in your glStencilFunc() call you've got it set to GL_ALWAYS, which if i'm reading this right means it will always pass the test, so try changing that to GL_NEVEREdit: heh, turns out i'm tired and havent played with the stencil buffer enuff, for future reference this NeHe tutorial covers the same kinda masking effect

Thanks for the suggestion; it didn't work. I have a feeling theres some detail outside of this code i'm missing