opengl - 在 glBlendFunc() 中使用 GL_SRC1_COLOR

标签 opengl opengl-3 blending color-blending

我正在使用 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和结果。

Texture 1 Texture 2 Result

最佳答案

假设目标颜色为黑色,您的结果就是您所期望的。目前尚不清楚您到底想做什么 glBlendFunc(GL_SRC_COLOR, GL_SRC1_COLOR); 。但它会做的是 what blending always does .

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

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

Dual source blending仅当您需要在两个源颜色和目标颜色之间执行某些操作时,才有用(因为这是您在着色器中无法执行的操作)。如果它可以表示为 (Src0 op Src1) op Dest,那么这就是您应该在着色器中执行的操作。

关于opengl - 在 glBlendFunc() 中使用 GL_SRC1_COLOR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15739256/

相关文章:

algorithm - 如何混合特定数量的两种颜色

processing - P5.js : manually calculate additive blendmode with pixels[]

c++ - 在 OSG 中创建太阳光源

cocoa - 最简单、简约、opengl 3.2 cocoa项目

opengl-3 - 使用着色器时高效的截锥体剔除

python pygame设置颜色透明度

opengl - glsl imageStore,其次是imageLoad,是否连贯?

java - 仅渲染 2D LWJGL 中的线框

c++ - 在 opengl 中绘制一个岛屿的二维轮廓

ios - 围绕对象的OpenGL ES轨道摄像机