c++ - 谁能帮我弄清楚我的着色器哪里出了问题?

标签 c++ opengl

我在弄清楚如何让我的片段着色器正确加载 3 个图像时遇到问题。 目标是:加载三个纹理图像 IMAGE1、IMAGE2 和 IMAGE3。 IMAGE3 应该是黑白图像。使用 IMAGE3 作为过滤器。对于任何给定的像素P,如果IMAGE3中对应的纹理像素颜色为白色,则使用IMAGE1对像素P进行纹理映射。如果MAGE3中对应的像素颜色为黑色,则使用IMAGE2对像素P进行纹理映射。如果对应的像素颜色在 MAGE3 中既不是黑色也不是白色,然后使用 IMAGE1 和 IMAGE2 的混合来纹理贴图像素 P。

现在我可以让它在 IMAGE3 的白色阴影区域中显示 IMAGE1,但我很难让 IMAGE2 在 IMAGE3 的黑色区域中显示而不与 HEXAGON 中的 IMAGE1 重叠。任何帮助将不胜感激。

#version 330

in vec2 textureCoord;

uniform sampler2D textureMap0;
uniform sampler2D textureMap1; 
uniform sampler2D textureMap2; 

out vec4 fragColor;

void main() {

// retrieve color from each texture
    vec4 textureColor1 = texture2D(textureMap0, textureCoord);
    vec4 textureColor2 = texture2D(textureMap1, textureCoord);
    vec4 textureColor3 = texture2D(textureMap2, textureCoord);
    //vec4 finalColor = textureColor1 + textureColor2 + textureColor3;

// Combine the two texture colors
// Depending on the texture colors, you may multiply, add, 
// or mix the two colors. 
#if __VERSION__ >= 130
    if ((textureColor1.r == 0.0f) && (textureColor1.g == 0.0f) 
        && (textureColor1.b == 0.0f)) {
        textureColor1.r = 1.0f;
    }

    //fragColor = mix(fragColor,finalColor,0.5);
    fragColor = textureColor1 * textureColor3 ;//* textureColor3;
#else
    gl_FragColor = textureColor1 * textureColor2;// * textureColor3;
#endif

}

最佳答案

我认为这应该按照您的描述进行:

vec4 textureColor4 = vec4(vec3(1.0, 1.0, 1.0) - textureColor3.rgb, 1.0)

//...

fragColor = textureColor3 * textureColor1 + textureColor 4 * textureColor2;

这本质上是一个线性插值。

关于c++ - 谁能帮我弄清楚我的着色器哪里出了问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27406962/

相关文章:

java - 如何获得适用于 LWJGL 的 ByteBuffer?

c++ - OpenGl 无法正确呈现 .obj 文件

opengl - Shader - 简单的 SSS 光照问题

c++ - glRotatef 移动轴

c++ - 为什么在 64 位系统上 float 不是 double?

c++ - 在对项目调用 next()/previous() 时,迭代器预计会有不同的行为

c++ - 在编译时通过 constexpr 或模板函数获取多维 std::array 的大小

c++ - 句子生成器

c++ - opengl c++ 中的边界填充算法

c++ - 如何在持续集成中包含 google benchmark