我在弄清楚如何让我的片段着色器正确加载 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/