gpt4 book ai didi

c++ - 不透明度面的两侧混合

转载 作者:行者123 更新时间:2023-11-28 06:37:25 26 4
gpt4 key购买 nike

我画了两个透明度为 50% 的多边形,但它只在一侧有效(看图片)。

良好混合:

但从另一个角度来看:

蓝色管是透明度为 0% 的轴。

初始化代码如下:

qglClearColor(QColor::fromCmykF(0.39, 0.39, 0.0, 0.0));

glEnable(GL_DEPTH_TEST);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

和绘图:

static const GLfloat P1[3] = { 0.0, -1.0, +2.0 };
static const GLfloat P2[3] = { +1.73205081, -1.0, -1.0 };
static const GLfloat P3[3] = { -1.73205081, -1.0, -1.0 };
static const GLfloat P4[3] = { 0.0, +2.0, 0.0 };

static const GLfloat * const coords[4][3] = {
{ P1, P2, P3 }, { P1, P3, P4 }, { P1, P4, P2 }, { P2, P4, P3 }
};

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -10.0);

glScalef(scaling, scaling, scaling);

glRotatef(rotationX, 1.0, 0.0, 0.0);
glRotatef(rotationY, 0.0, 1.0, 0.0);
glRotatef(rotationZ, 0.0, 0.0, 1.0);

for (int i = 0; i < 2; ++i) {
glLoadName(i);
glBegin(GL_POLYGON);
QColor color = faceColors[i];
color.setAlpha(50);
qglColor(color);
for (int j = 0; j < 3; ++j) {
glVertex3f(coords[i][j][0], coords[i][j][1],
coords[i][j][2]);
}
glEnd();
}

我的目的是绘制和探索像这样的场景
(来源:wblut.com)

最佳答案

问题在于同时使用混合和深度测试进行渲染。当深度测试发现几何体的一部分位于场景中另一部分几何体的后面时,它将被丢弃而不是混合。

要解决这个问题,分两步渲染

glDisable(GL_BLEND);
glDepthMask(GL_TRUE);
// Render solid geometry here

glEnable(GL_BLEND);
glDepthMask(GL_FALSE);
// Render transparent geometry here

在第一步中,像往常一样绘制实体几何体而不进行混合。在不更改深度缓冲区的情况下,在第二遍中绘制透明几何体。仍然启用深度测试以避免在透明部分前面的实体几何体上混合。

关于c++ - 不透明度面的两侧混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26570938/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com