GLSL类型不一致

时间:2015-11-07 04:24:24

标签: 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;
}

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

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;

1 个答案:

答案 0 :(得分: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

需要:

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