【发布时间】:2019-03-12 05:53:10
【问题描述】:
我有两个纹理和一个三角形图元(其顶点的颜色由glColor4f 定义)。
纹理 A 如下所示:
纹理 B 如下所示:
三角形基元 P 如下所示:
当我渲染纹理 A,然后渲染纹理 B 时,我得到了预期的结果,即矩形的中间是透明的。
但是,如果我渲染原始 P,然后是纹理 A,然后是纹理 B,我会得到错误的颜色。 P 不是纯红色,纹理颜色全部变为黑色。
如何解决此问题以保持适当的透明度和颜色。
纹理参数:
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER));
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER));
GLfloat debugColor[] = {1.0f, 0.0f, 1.0f, 1.0f};
GL_CALL(glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, debugColor));
混合功能:
GL_CALL(glEnable(GL_BLEND));
GL_CALL(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
P、A 和 B 的渲染:
if(true)
{
// Render Primitive P
GL_CALL(glBegin(GL_TRIANGLES));
GL_CALL(glColor4f(1.0f, 0.0f, 0.0f, 1.0f));
GL_CALL(glVertex2f(-0.5f, -0.5f));
GL_CALL(glColor4f(1.0f, 0.0f, 0.0f, 1.0f));
GL_CALL(glVertex2f(0.0f, 0.5f));
GL_CALL(glColor4f(1.0f, 0.0f, 0.0f, 1.0f));
GL_CALL(glVertex2f(0.5f, -0.5f));
glEnd();
}
if(true)
{
// Render Texture A
GL_CALL(glBindTexture(GL_TEXTURE_2D, textureId));
GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, imageWidth, imageHeight, 0,
GL_RGBA, GL_UNSIGNED_BYTE, image));
real32 screenPercentage = 0.25f;
GL_CALL(glBegin(GL_TRIANGLES));
// Lower triangle
GL_CALL(glTexCoord2f(0.0f, 0.0f));
GL_CALL(glVertex2f(-screenPercentage, screenPercentage));
GL_CALL(glTexCoord2f(1.0f, 0.0f));
GL_CALL(glVertex2f(screenPercentage, screenPercentage));
GL_CALL(glTexCoord2f(1.0f, 1.0f));
GL_CALL(glVertex2f(screenPercentage, -screenPercentage));
// Upper triangle
GL_CALL(glTexCoord2f(0.0f, 0.0f));
GL_CALL(glVertex2f(-screenPercentage, screenPercentage));
GL_CALL(glTexCoord2f(1.0f, 1.0f));
GL_CALL(glVertex2f(screenPercentage, -screenPercentage));
GL_CALL(glTexCoord2f(0.0f, 1.0f));
GL_CALL(glVertex2f(-screenPercentage, -screenPercentage));
glEnd();
}
if(true)
{
// Render Texture B
GL_CALL(glBindTexture(GL_TEXTURE_2D, textureId));
GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, window[0].offscreenBuffer.width, window[0].offscreenBuffer.height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, window[0].offscreenBuffer.data));
real32 screenPercentage = 1.0f;
GL_CALL(glBegin(GL_TRIANGLES));
// Lower triangle
GL_CALL(glTexCoord2f(0.0f, 0.0f));
GL_CALL(glVertex2f(-screenPercentage, screenPercentage));
GL_CALL(glTexCoord2f(1.0f, 0.0f));
GL_CALL(glVertex2f(screenPercentage, screenPercentage));
GL_CALL(glTexCoord2f(1.0f, 1.0f));
GL_CALL(glVertex2f(screenPercentage, -screenPercentage));
// Upper triangle
GL_CALL(glTexCoord2f(0.0f, 0.0f));
GL_CALL(glVertex2f(-screenPercentage, screenPercentage));
GL_CALL(glTexCoord2f(1.0f, 1.0f));
GL_CALL(glVertex2f(screenPercentage, -screenPercentage));
GL_CALL(glTexCoord2f(0.0f, 1.0f));
GL_CALL(glVertex2f(-screenPercentage, -screenPercentage));
glEnd();
}
编辑:尝试@Rabbid76 的解决方案后:
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
if(true)
{
// Render triangle with all vertices having color 1.0f, 0.0f, 0.0f, 1.0f
}
if(true)
{
// Render texture A with color set to 1.0f, 1.0f, 1.0f, 1.0f and the appropriate texture u,v coordinates
}
if(true)
{
// Render texture B with color set to 1.0f, 1.0f, 1.0f, 1.0f and the appropriate texture u,v coordinates
}
我得到以下结果:
即使三个实体的alpha都是1.0f,空心区域的alpha都是0.0f,颜色仍然不对
【问题讨论】:
标签: opengl rendering alpha texture2d alpha-transparency