GLSL类型不一致

dk1*_*123 3 c++ opengl glsl

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

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;
}
Run Code Online (Sandbox Code Playgroud)

这个片段着色器在我的电脑上完美运行.但是我从人们那里收到一些反馈,他们发现了以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

这似乎暗示了一些类型错误,但我不知道不一致的来源.有人可能解释一下吗?

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

gen*_*ult 5

#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
Run Code Online (Sandbox Code Playgroud)

需要:

gl_FragColor.r = pixel.r * (1.0-time_passed_fraction) + bw_val * time_passed_fraction;
                            ^^^ fix'd
Run Code Online (Sandbox Code Playgroud)