2

我正在使用 glBindFragDataLocationIndexed() 并在片段着色器中编写两种颜色。以下是我的代码:

glBindFragDataLocationIndexed(shader_data.psId, 0, 0, "Frag_Out_Color0");
glBindFragDataLocationIndexed(shader_data.psId, 0, 1, "Frag_Out_Color1");

glActiveTexture ( GL_TEXTURE0 );
LoadTexture(texture1);    
glBindTexture ( GL_TEXTURE_2D, tex_id1);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA,width, height, 0, GL_RGBA,GL_UNSIGNED_BYTE, texture1_data);

glActiveTexture ( GL_TEXTURE1 );
LoadTexture(texture2);
glBindTexture ( GL_TEXTURE_2D, tex_id2);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA,width, height, 0, GL_RGBA,GL_UNSIGNED_BYTE, texture2_data);


glEnable(GL_BLEND);
glBlendFunc(GL_SRC_COLOR, GL_SRC1_COLOR);

glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

片段着色器是:

#version 150
in vec2 texcoord;

uniform sampler2D basetexture1;
uniform sampler2D basetexture2;

out vec4 Frag_Out_Color0;
out vec4 Frag_Out_Color1;

void main(void)
{
    Frag_Out_Color0 = texture2D(basetexture1, texcoord);

    Frag_Out_Color1 = texture2D(basetexture2, texcoord);

}

OGL3.3 规范说:

Data written to the first of these outputs becomes the first source
color input to the blender (corresponding to SRC_COLOR and SRC_ALPHA). Data
written to the second of these outputs generates the second source color input to
the blender (corresponding to SRC1_COLOR and SRC1_ALPHA).

我已将 GL_SRC_COLOR、GL_SRC1_COLOR 作为输入传递给 glBlendFunc()。我不确定结果是否正确。请查找附件图片。此外,如果我通过 GL_ONE,GL_ONE ,则仅渲染第一个纹理,根本没有第二个纹理的迹象。如何验证我的其余代码是否正常(并且混合是否正确完成)?

以下分别是纹理 1、纹理 2 和结果。

纹理 1 纹理 2 结果

4

1 回答 1

3

假设目标颜色为黑色,您的结果就是您所期望的。目前尚不清楚您到底打算glBlendFunc(GL_SRC_COLOR, GL_SRC1_COLOR);做什么。但它会做的就是混合总是的。

源因子 ( GL_SRC_COLOR) 将乘以源颜色(或者,更准确地说,是 source0 颜色)。目标因子 ( GL_SRC1_COLOR) 将乘以目标颜色。这些乘法的结果将相加。

在您的情况下,这意味着 source0 颜色将自行相乘因此使结果更暗)。目标颜色将乘以 source1 颜色;如果目标为零,则为 0。因此,您将 source0 颜色乘以自身作为输出。

只有当您需要在两种源颜色和目标颜色之间执行一些操作时,双源混合才有(因为这是您无法在着色器中执行的操作)。如果它可以表示为,那么这就是你应该在着色器中做的事情。(Src0 op Src1) op Dest

于 2013-04-01T09:06:55.537 回答