cutting holes

I am searching for a way to cut holes in triangles I have already drawn in order to avoid the hassle of replacing the triangle with the triangles surrounding the hole and implementing the undo operation when I want to take the hole away.

The application arises because I want a tunnel diving underground. Unfortunately the surface of the land remains inside the tunnel visible to a viewer travelling along the tunnel.

One possibility is to project the tunnel walls onto a texture which is used to overwrite the intersection. This is undo-able but is not true perspective inside the tunnel.

All this would be easy if the inside faces of the tunnel always showed in preference to the land surface but my understanding of GL_BLEND is that this can only be done on a plane not in the viewing plane.

Draw the tunnel ‘hole’ to the stencil buffer. You can do this using coplnar polygons or something almost coplanar but very slightly protruding.

Next stencil test a depth clear polygon (something very distant) to the region you just rendered with a GL_ALWAYS depth test.

Finally render the tunnel interior and distant terrain normally.

Remember to clear the stencil buffer between frames.

…now try that with lighting.
:slight_smile: only joking.

Hello,

yes thanks this seems to be what I want but the pixels are only intermittently transparent within the hole!

Under certain circumstances almost the whole hole is there with irregular areas not ‘hole’.
It changes as the viewing angle changes.

I have tried all combinations of
//glDisable(GL_TEXTURE_2D);
//glBindTexture(GL_TEXTURE_2D, texture);
and nothing works.

Stranglely, it seems to not work at all if I drop the DepthFunc.

I have:

int InitGL()
{
glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LESS);

glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glPointSize(NORM_WIDTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glPolygonMode( GL_BACK, GL_FILL );
glPolygonMode( GL_FRONT, GL_FILL );

}

DrawGLScene()
{ /* draw scene underneath */
glPushMatrix();

glTranslatef(0.0, 0.4, 0.0);

//glDisable(GL_TEXTURE_2D);
glColorMask(0,0,0,0);
glEnable(GL_STENCIL_TEST);
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
glStencilMask(GL_TRUE);
glStencilFunc(GL_NEVER, 1, 1);
glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP);

glColor3f( 1.0f, 1.0f, 1.0f );

//glBindTexture(GL_TEXTURE_2D, texture);

glBegin(GL_TRIANGLES);
glVertex3f(0.5, 0.0, 0.5);
glVertex3f(-0.5,0.0, 0.5);
glVertex3f(-0.5,0.0, -0.5);
glVertex3f(0.5, 0.0, 0.5);
glVertex3f(0.5, 0.0, -0.5);
glVertex3f(-0.5,0.0, -0.5);
glEnd();

glStencilMask(GL_TRUE);
glStencilFunc(GL_EQUAL, 0, 1);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);
glClear(GL_STENCIL_BUFFER_BIT);

glColor3f( 1.0f, 1.0f, 1.0f );
glBindTexture(GL_TEXTURE_2D, 0);
//glEnable(GL_TEXTURE_2D);

glBegin(GL_TRIANGLES);
glVertex3f(-3.0f,0.0f,-3.0f);
glVertex3f( 3.0f,0.0f,-3.0f);
glVertex3f( 3.0f,0.0f, 3.0f);
glVertex3f(-3.0f,0.0f,-3.0f);
glVertex3f(-3.0f,0.0f, 3.0f);
glVertex3f( 3.0f,0.0f, 3.0f);
glEnd();

return 1;
}

Sounds like you are zfighting.

You need to offset the coplanar stencil write.

glPolygonOffset should do it.

Thank you that worked :eek: