OpenGl: stencil buffer problem (Wall + Window)?

I want to create a wall with a window inside using a stencil buffer. My code is as follows:

glEnable(GL_STENCIL_TEST);
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
glStencilMask(1);
glStencilFunc(GL_ALWAYS, 1, 1);

glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glDisable(GL_DEPTH_TEST);   
glColor3f(1,1,1);

//Window
glBegin(GL_POLYGON);
glVertex3d(-5,0,-20);
glVertex3d(-5,0,40);
glVertex3d(-20,0,40);
glVertex3d(-20,0,-20);
glEnd();

glEnable(GL_DEPTH_TEST);
glStencilFunc(GL_NOTEQUAL, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

glEnable(GL_CLIP_PLANE0);                       
glClipPlane(GL_CLIP_PLANE0, eqr);   
glBindTexture(GL_TEXTURE_2D,texture[1]);
glBegin(GL_POLYGON);
glNormal3f(0.0f,-1.0,0.0f);

//Wall
glTexCoord2f(0.0f, 0.0f);glVertex3d(20,0,-20);
glTexCoord2f(1.0f, 0.0f);glVertex3d(20,0,40);
glTexCoord2f(1.0f, 0.0f);glVertex3d(-20,0,40);
glTexCoord2f(1.0f, 0.0f);glVertex3d(-20,0,-20);

glEnd();
glDisable(GL_STENCIL_TEST);

But this will not work, I got the entire fill wall without a window in it, any suggestions

+1
source share
2 answers

Make sure you request an OpenGL context with a stencil buffer from your window system. You cannot get it by default unless you ask for it.

Here is an example using GLUT . You want to watch the challenge glutInitDisplayMode().

Here is another example using the original Win32. Bit PIXELFORMATDESCRIPTORis an important part.

glXChooseVisual(), , , X11.

SDL, , .

+1

, ,

glStencilFunc(GL_NOTEQUAL, 1, 1);

to

glStencilFunc(GL_EQUAL, 1, 1);
+1

Source: https://habr.com/ru/post/1745183/


All Articles