我目前正在使用以下片段着色器来实现基本的黑白效果:

uniform sampler2D texture;
uniform float time_passed_fraction;

//gl_Color: in the fragment shader, the interpolated color passed from the vertex shader

void main()
{
    // lookup the pixel in the texture
    vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);
    vec3 texel = pixel .rgb;

    gl_FragColor = pixel;
    float bw_val = max(texel.r,max(texel.g,texel.b));

    gl_FragColor.r = pixel.r * (1-time_passed_fraction) + bw_val * time_passed_fraction;
    gl_FragColor.g = pixel.g * (1-time_passed_fraction) + bw_val * time_passed_fraction;
    gl_FragColor.b = pixel.b * (1-time_passed_fraction) + bw_val * time_passed_fraction;
    gl_FragColor.a = pixel.a;

    // multiply it by the color
    //gl_FragColor = gl_Color * pixel;
}

这个片段着色器在我的电脑上完美运行。但是我从人们那里得到了一些反馈,他们收到了以下错误:
ERROR: 0:17: '-' :  wrong operand types  no operation '-' exists that takes a left-hand operand of type 'const int' and a right operand of type 'uniform float' (or there is no acceptable conversion)
ERROR: 0:17: '*' :  wrong operand types  no operation '*' exists that takes a left-hand operand of type 'float' and a right operand of type 'const int' (or there is no acceptable conversion)
ERROR: 0:18: '-' :  wrong operand types  no operation '-' exists that takes a left-hand operand of type 'const int' and a right operand of type 'uniform float' (or there is no acceptable conversion)
ERROR: 0:18: '*' :  wrong operand types  no operation '*' exists that takes a left-hand operand of type 'float' and a right operand of type 'const int' (or there is no acceptable conversion)
ERROR: 0:19: '-' :  wrong operand types  no operation '-' exists that takes a left-hand operand of type 'const int' and a right operand of type 'uniform float' (or there is no acceptable conversion)

这似乎暗示了某些类型错误,但我不明白不一致的来源。有人可以解释一下吗?

注意:第17行是这一行)gl_FragColor.r = pixel.r * (1-time_passed_fraction) + bw_val * time_passed_fraction;

最佳答案

着色器开头缺少的 #version 指令意味着 #version 110,正如三十二上校指出的那样,它不支持隐式 int -> float 转换。

请注意 typeof(1) == int != float == typeof(1.0)

所以你的台词是这样的:

gl_FragColor.r = pixel.r * (1-time_passed_fraction) + bw_val * time_passed_fraction;
                            ^ nope

需要:
gl_FragColor.r = pixel.r * (1.0-time_passed_fraction) + bw_val * time_passed_fraction;
                            ^^^ fix'd

关于c++ - GLSL 类型不一致,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33579110/

10-11 19:39