在OpenGL中更新模具缓冲区时的问题 [英] Issue when updating stencil buffer in OpenGL

查看:159
本文介绍了在OpenGL中更新模具缓冲区时的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mac上的OpenGL中使用模版测试进行绘图时,我遇到了问题.当我第一次绘制场景时,模具工作正常.我在中间绘制了一个半透明的黑色矩形,启用了对模板缓冲区的写操作,然后在较大的蓝色矩形中禁用了对模板缓冲区的写操作.第一次弹出窗口时,我得到正确的结果,如下所示:

I'm having issues when drawing using the stencil test in OpenGL on mac. When I first draw the scene, the stencil works fine. I draw a semi-transparent black rectangle in the middle, with writing to the stencil buffer enabled, and then a larger blue rectangle with writing to the stencil buffer disabled. I get the right result when the window first pops up, which looks like this:

但是,当我调整窗口大小并再次调用渲染函数时,我得到的结果如下:

However, when I resize the window, and the rendering function gets called again, I get a result which looks like:

有时候,奇怪的白色空间跟随中间的矩形,其他时候,白色在看似随机的排列之间捕捉,但是当您返回到该窗口大小时,仍保留这些排列.我在网上找不到任何解决方案.这是我的渲染函数,在调整窗口大小时会调用该函数:

Sometimes the weird white space follows the middle rectangle, other times the white snaps between seemingly random arrangements, but keeps those arrangements when you go back to that window size. I can find no solution to this online. Here is my render function, which is called any time that the window is resized:

glClearColor(0, 0, 0, 1);
glClearStencil(0x00);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

// Replace data in the stencil buffer with 1s if it passes the test
//  which should be GL_ALWAYS
glStencilFunc(GL_ALWAYS, 1, 0xFF); 
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

// Allow data to be written to the stencil buffer.
glStencilMask(0xFF);

p->fillSquare(1, 1, 1, 0.3, -0.25, 0.25, 0.25, 0.25, -0.25, -0.25, 0.25, -0.25); // Write a semi transparent black rectangle

glStencilMask(0x00); // Disable writing to the stencil buffer.
glStencilFunc(GL_NOTEQUAL, 1, 0xFF); // Only draw if stencil value is 1.

// Draw blue rectangle
p->fillSquare(0.60, 0.60, 0.80, 1, -0.5, 0.5, 0.5, 0.5, -0.5, -0.5, 0.5, -0.5);

glfwSwapBuffers(w);

如果您想知道,p->fillRect()会为矩形的RGBA颜色获取四个浮点数,然后取每个顶点的x和y坐标.

In case you want to know, p->fillRect() takes four floats for RGBA color of the rectangle and then the x and y coordinate of each vertex.

清除模版似乎可能存在某种问题,但我确实不能确定.我确实在OpenGL初始化功能中打开了模板测试. 如果您需要了解我的代码或系统其他方面的信息,请随时发表评论.

It seems like there may be some kind of issue in clearing the stencil, but I really can't be sure. I do have the stencil test turned on in my OpenGL initialization function. If you need to know anything else about the other aspects of my code or system, feel free to comment.

注意:我没有使用OpenGL的深度,因此可以不清除深度缓冲区(我已经对此进行了测试).

Note: I am not using the OpenGL's depth, so it's okay to not be clearing the depth buffer (I've tested this).

推荐答案

清除模版缓冲区确实不起作用(除了第一次调用funtion以外),就像您期望的那样.

The clearing of the stencil buffer does indeed not work (except for the very first time that funtion is called) as you might expect it.

您错过的是glStencilMask也将影响glClear(... | GL_STENCIL_BUFFER_BIT). 在执行清除调用之前,您应该将glStencilMask(0xFF)向上移动一点.

What you missed is that the glStencilMask will also affect glClear(... | GL_STENCIL_BUFFER_BIT). You should move the glStencilMask(0xFF) up a bit, before doing the clear call.

这篇关于在OpenGL中更新模具缓冲区时的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆