Sign in to follow this  
oachler

Stencil buffers and masking out a region

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.glColorMask(0,0,0,0);
				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.glColorMask(1,1,1,1);								// Set Color Mask to TRUE, TRUE, TRUE, TRUE
				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 this post


Link to post
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 this post


Link to post
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_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


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

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