我有两个纹理和一个三角形图元(其顶点的颜色由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,颜色仍然不正确
答案 0 :(得分:2)
如果启用纹理,则默认情况下,纹理像素的颜色将乘以当前颜色,因为默认情况下纹理环境模式(if (preg_match('/^([1-9]|[1-2][0-9]|[3][0-1])\-([1-9]|[1][0-2])\-(?:20)([1][8-9]|[2-9][0-9])$/', $date)) {
// Do stuff
}
)为GL_TEXTURE_ENV_MODE
。参见glTexEnv
。
这将导致纹理的纹理像素的颜色与glColor4f
设置的最后一个颜色“混合”。
渲染纹理时,仍会设置三角形的红色,这将导致纹理着色为红色。绿色和蓝色通道完全消失了,并且由于纹理没有红色通道,因此剩下的都是黑色。
要解决您的问题,必须在渲染纹理之前设置白色:
GL_MODULATE
并在渲染三角形之前禁用glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
,但在渲染纹理之前将其启用。
请注意,始终存在纹理绑定,没有“没有纹理”之类的东西,默认纹理对象0也是有效对象。
这将导致纹理查找呈现完全黑色,并与由GL_TEXTURE_2D
设置的红色混合:
glColor4f