HI all,
I ‘ve difficulty with understanding the logic of stencil buffer, the code bellow is supposed to carve out solid sphere from octagon. But what I got as result is hole in the octagon w/o depth perception, even if I look(rotate) from back of octagon, sphere is still visible, where it’s supposed to be hided by octagon geometry.
I’ve also provided my inline comments my way interpretation of stencil buffer.
Any comments will be appreciated.
Void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glEnable(GL_LIGHTING);
glPushMatrix();
glCullFace(GL_BACK); // DISCARD THE BACK FACE
glStencilFunc(GL_LESS,1,1);//FRAGMENTS COMING FROM SPHERE WILL PASS THE STENCIL TEST
glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP); // PASSING FRAGMENTS WILL MODIFY THE BUFFER CONTENTS WITH 1'S
glPushMatrix();
glTranslatef(0.5,0,0);
glColor3f(0,0,1);
glutSolidSphere(0.6,16,16); // DRAWING METHOD SPHERE
glPopMatrix();
// SO FAR, I'LL GET SOMETHING LIKE A MASK(OF SPHERE) IN STENCIL BUFFER
//glDepthFunc(GL_ALWAYS);
glColor3f(1,0,0);
glStencilFunc(GL_EQUAL,0,1);//LET ONLY THE 0's PASS THE STENCIL TEST
glutSolidOctahedron();
glCullFace(GL_FRONT);
glColor3f(0,0,1);
glStencilFunc( GL_NOTEQUAL,1,1);//STENCIL TEST THE SPHERE TO GET THE OCTAHEDRON INNER-FACE FILLED
glPushMatrix();
glTranslatef(0.5,0,0);
glutSolidSphere(0.6,16,16); // DRAWING METHOD SPHERE
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void init(void)
{
……………………
glClearStencil(0); // ISN'T THAT EQUAL WITH FILLING THE STENCIL BUFFER WITH 0'S ??
glEnable(GL_STENCIL_TEST); // ENABLE STENCIL TEST
glEnable(GL_CULL_FACE); // ENABLE FACE CULLING
glEnable(GL_DEPTH_TEST);
glStencilFunc(GL_ALWAYS, 0, 1 ); // SET THE TEST COMPARISION FUNC TO FILL WITH 0'S
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); // FILL UP THE STENCIL BUFFER WITH 0'S
}