opengl模具缓冲区-不太正常 [英] opengl stencil buffer - not quite got it working

查看:346
本文介绍了opengl模具缓冲区-不太正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前正在绘制背景,然后将透明三角形绘制到模板缓冲区中,然后绘制蓝色正方形(未绘制三角形).我希望效果是一个蓝色的正方形,上面有一个三角形的孔,露出背景.但是我得到的只是在其他所有物体上绘制的黑色三角形.我认为即时通讯在正方形上获得了预期的效果,但模具也被应用到了背景上.

currently im drawing a background, then a transparent triangle into the stencil buffer, then a blue square where the triangle isnt being drawn. i was hoping the effect would be a blue square with a trianglular hole exposing the background. but all im getting is a black triangle drawn over everything else. i think im getting the desired effect on the square but the stencil is also being applied to the background.

我的问题是如何调整以下代码以允许背景通过正方形的三角形孔显示?

my question is how can i adjust the following code to allow the background to be displayed through a triangular hole in the square?

static void Draw(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

//big blue square
glColor3ub(0,255,255);
glBegin(GL_POLYGON);
glVertex3i(5, 5, 0);
glVertex3i(-5, 5, 0);
glVertex3i(-5, -5, 0);
glVertex3i(5, -5, 0);
glEnd();

glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

/* transparent triangle */
glColor4ub(0.0f, 0.0f, 0.0f, 0.0f);
glBegin(GL_POLYGON);
glVertex3i(-4, -4, 0);
glVertex3i(4, -4, 0);
glVertex3i(0, 4, 0);
glEnd();

glStencilFunc(GL_EQUAL, 1, 1);
glStencilOp(GL_INCR, GL_KEEP, GL_DECR);

glStencilFunc(GL_EQUAL, 2, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

/* blue square */
glColor3ub(0, 0, 200);
glBegin(GL_POLYGON);
glVertex3i(3, 3, 0);
glVertex3i(-3, 3, 0);
glVertex3i(-3, -3, 0);
glVertex3i(3, -3, 0);
glEnd();

if (doubleBuffer) {
    glutSwapBuffers();
} else {
    glFlush();
}
}

推荐答案

问题一:绘制背景时,请先禁用模板测试,或将模板功能切换为GL_ALWAYS,以使片段通过(我建议将其关闭).然后,您要在以后设置两个不同的模具模版,但是无论哪种都不会做,这是您想要的.所以这是变化:

Problem one: When drawing the background either disable stencil test before, or switch the stencil function to GL_ALWAYS so that the fragments pass (I recomment turning it off). Then you're setting two different stencil modi afterwards, but neither will do, what you intend. So here's the change:

glDisable(GL_STENCIL_TEST);
glStencilMask(0x0);

draw_background();

glEnable(GL_STENCIL_TEST);
glStencilMask(0x1);
glStencilFunc(GL_ALWAYS, 0x1, 0x1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);

draw_trangle();

glStencilFunc(GL_NOTEQUAL, 0x1, 0x1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);

draw_blue_square();

这篇关于opengl模具缓冲区-不太正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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