如何用另一个四边形遮盖 OpenGL 四边形
How to mask OpenGL quad with another quad
我正在尝试显示一个四边形,但只有当它位于我知道其位置的另一个四边形上方时。我考虑过使用该四边形作为另一个四边形的掩码,但我不确定该怎么做(我已经找到 this post 谈论掩码,但在我的情况下我没有有遮罩纹理;我只知道要遮罩区域的 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);
我怎样才能做到无论我之前画的是什么,它只会遮住前一个四边形?
如果您想将渲染限制在一个矩形区域,您可以使用 Scissor Test。
必须启用剪刀测试(GL_SCISSOR_TEST
) a nd th rectangular area can be set by glScissor
。例如:
glEnable(GL_SCISSOR_TEST);
glScissor(x, w, width, height);
我正在尝试显示一个四边形,但只有当它位于我知道其位置的另一个四边形上方时。我考虑过使用该四边形作为另一个四边形的掩码,但我不确定该怎么做(我已经找到 this post 谈论掩码,但在我的情况下我没有有遮罩纹理;我只知道要遮罩区域的 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);
我怎样才能做到无论我之前画的是什么,它只会遮住前一个四边形?
如果您想将渲染限制在一个矩形区域,您可以使用 Scissor Test。
必须启用剪刀测试(GL_SCISSOR_TEST
) a nd th rectangular area can be set by glScissor
。例如:
glEnable(GL_SCISSOR_TEST);
glScissor(x, w, width, height);