//DepthTest is enabled and depthmask is true
DrawTriangle()
{
//Stencil buffer:-
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
glEnable(GL_STENCIL_TEST);
//Mask off the shape:-
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_KEEP,GL_KEEP,GL_REPLACE);
//Draw in the traingle to the stencil buffer:-
glColorMask(0,0,0,0);
glDepthMask(GL_FALSE);
glBegin(GL_TRIANGLES);
glVertex3fv(v1);
glVertex3fv(v2);
glVertex3fv(v3);
glEnd();
glColorMask(1,1,1,1);
glDepthMask(GL_TRUE);
/////////////////////////////////////////////////////////
//Draw into the stenciled area
/////////////////////////////////////////////////////////
glPushMatrix();
glStencilFunc(GL_EQUAL,1,1);
glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);
glEnable(GL_TEXTURE_2D);
drawScene();
glPopMatrix();
//Fill in the depth values:-
glColorMask(0,0,0,0);
glDisable(GL_TEXTURE_2D);
glDepthFunc(GL_ALWAYS);
glBegin(GL_TRIANGLES);
glVertex3fv(v1);
glVertex3fv(v2);
glVertex3fv(v3);
glEnd();
glColorMask(1,1,1,1);
glDisable(GL_STENCIL_TEST);
//Put depth test back
glDepthFunc(GL_LEQUAL);
}
I'm just calling this 4 times with the 4 different triangles.
I'm sure I'm doing something stoooopid here, I assuming stencil buffer limits what is written into depth bufffer.
Please point out any stupidity you see.
Thanks in advance.
if you need more info please ask, I kinda striped the code down a little to keep the post as small as posssible
Edited by - seaman on February 22, 2001 4:31:13 PM
Strange depth and stencil buffer-ness
I have a triangle defined by 3 vertices, v1,v2, and v3 and I am trying to write this into the stencil buffer, draw a scene in its shape and then redraw the original triangles depth vaules over the ones from the scen in the triangle shape.
Now this all works ok for one triangle, the problem is I have a 4 triangles in a tetrahedron shape and I doing this with all of them and they aint depth buffering aginst each other correctly.
February 22, 2001 09:53 PM
I do not have all the details but it seems like you have to look at how the depth buffer is treated depending on the result of the stencil buffer test.
February 23, 2001 07:10 AM
As I see it the following code means that the secen will only draw where the stencil buffer equals 1, and regardless of wethere that fragment passes the depth test or stencil test the stencil buffer remains unchanged.
Then the following code should fill in the deth values for the triangl over the top, in the stencil buffered shape:-
No somewhere this isnt doing what I expect, as I draw the base triangeof my tetrahedron and it shows through one of the side triangles. Which I guess is down to the depth buffer doing something unexpected.
glPushMatrix(); glStencilFunc(GL_EQUAL,1,1); glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);glEnable(GL_TEXTURE_2D);drawScene();glPopMatrix();
Then the following code should fill in the deth values for the triangl over the top, in the stencil buffered shape:-
//Fill in the depth values:-glColorMask(0,0,0,0);glDisable(GL_TEXTURE_2D);glDepthFunc(GL_ALWAYS);glBegin(GL_TRIANGLES);glVertex3fv(v1); glVertex3fv(v2);glVertex3fv(v3);glEnd();glColorMask(1,1,1,1);glDisable(GL_STENCIL_TEST);//Put depth test backglDepthFunc(GL_LEQUAL);
No somewhere this isnt doing what I expect, as I draw the base triangeof my tetrahedron and it shows through one of the side triangles. Which I guess is down to the depth buffer doing something unexpected.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement