问题
I am using glBindFragDataLocationIndexed() and writing two colors in fragment shader. Following is my code :
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);
Fragment Shader is :
#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 spec says that :
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).
I have passed GL_SRC_COLOR, GL_SRC1_COLOR as inputs to glBlendFunc(). I am not sure whether the result is correct or not. Please find attached images. Also if I pass GL_ONE,GL_ONE ,only first texture is rendered and no sign of second texture at all. How can I verify that rest of my code is fine (and blending is correctly done) ?
Following are texture 1 , texture 2 and result respectively.
回答1:
Your result is what you would expect, assuming that the destination color is black. It's not clear what exactly you intend glBlendFunc(GL_SRC_COLOR, GL_SRC1_COLOR);
to do. But what it will do is what blending always does.
The source factor (GL_SRC_COLOR
) will be multiplied by the source color (or, to be more precise, the source0 color). The destination factor (GL_SRC1_COLOR
) will be multiplied by the destination color. The results of these multiplications will be added together.
In your case, this means that the source0 color will be multiplied by itself (therefore making the result darker). The destination color will be multiplied by the source1 color; if the destination is zero, you get 0. Therefore, you get the source0 color multiplied by itself as the output.
Dual source blending is only useful (in that it's something you couldn't do in your shader) if you need to perform some operation between the two source colors and the destination color. If it can be expressed as (Src0 op Src1) op Dest
, then it's stuff you should do in your shader.
来源:https://stackoverflow.com/questions/15739256/using-gl-src1-color-in-glblendfunc