OpenGl:模板缓冲区问题(墙+窗)?

发布于 2024-08-31 23:52:30 字数 972 浏览 5 评论 0 原文

我想使用模板缓冲区创建一堵墙,里面有一扇窗户。 我的代码看起来像这样:

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);

但这不起作用,我得到了整个填充墙,里面没有窗户,有什么建议吗?

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??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水中月 2024-09-07 23:52:30

确保您从窗口系统请求带有模板缓冲区的 OpenGL 上下文。除非您特别要求,否则您可能不会默认获得一个。

这是一个使用 示例 ://openglut.sourceforge.net/" rel="nofollow noreferrer">过剩。您需要查看 glutInitDisplayMode() 调用。

这是另一个使用原始 Win32 的示例PIXELFORMATDESCRIPTOR 位是重要的部分。

glXChooseVisual() 可能是您的位置想看看您是否使用 X11。

如果您想要跨平台上下文管理,我建议使用 SDL

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.

季末如歌 2024-09-07 23:52:30

我还没有测试过你的代码,但我认为你应该更改

glStencilFunc(GL_NOTEQUAL, 1, 1);

glStencilFunc(GL_EQUAL, 1, 1);

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

glStencilFunc(GL_NOTEQUAL, 1, 1);

to

glStencilFunc(GL_EQUAL, 1, 1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文