OpenGl: Stencil buffer problem (Wall + Window)?

旧街凉风 提交于 2020-01-06 02:53:06

问题


I want to create a wall with a window inside, using stencil buffer. My code looks like this:

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 that doesn't work out, I got the whole fill wall without the window in it, any suggestions??


回答1:


Make sure you request an OpenGL context with a stencil buffer from your windowing system. You may not get one by default unless you specifically ask for it.

Here's an example using GLUT. You'll want to look at the glutInitDisplayMode() call.

Here's another example using raw Win32. The PIXELFORMATDESCRIPTOR bit is the important part.

glXChooseVisual() is probably where you want to look if you're using X11.

I'd recommend SDL if you want cross-platform context management.




回答2:


I haven't tested your code, but I think that you should change

glStencilFunc(GL_NOTEQUAL, 1, 1);

to

glStencilFunc(GL_EQUAL, 1, 1);


来源:https://stackoverflow.com/questions/2823584/opengl-stencil-buffer-problem-wall-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!