首页 > 解决方案 > 如何用另一个四边形掩盖 OpenGL 四边形

问题描述

我正在尝试显示一个四边形,但只有当它位于我知道其位置的另一个四边形上方时。我考虑过使用那个四边形作为另一个四边形的遮罩,但我不确定如何去做(我已经找到了这篇关于遮罩的帖子,但是在我的情况下,我没有遮罩纹理;我只知道遮罩区域的 X、Y、宽度和高度)。我找到的当前解决方案是使用glBlendFunc,它只有在我不渲染任何东西的情况下才有效,以后不会出现这种情况。

glBlendFunc(GL_ONE, GL_ZERO);
// draw the background quad, that is acting as the mask...
glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_SRC_ALPHA, GL_ZERO);
// draw the background quad again, this time it will act as a mask...
glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
// draw the quads that will be masked...
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // this is the blend func used for the rest of the rendering

在绘制每一帧之前,我还有一个清屏功能:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0,0,0,0);

我怎样才能做到这一点,无论我在此之前绘制的是什么,它只会掩盖前一个四边形?

标签: openglmask

解决方案


如果要将渲染限制为矩形区域,则可以使用Scissor Test
必须启用剪刀测试 ( GL_SCISSOR_TEST),并且可以通过 设置矩形区域glScissor。例如:

glEnable(GL_SCISSOR_TEST);
glScissor(x, w, width, height);   

推荐阅读