2015-11-07 56 views
2

我目前使用下面的片段着色器的基本黑白效果: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;

+5

GLSL的IIRC旧版本并没有做隐含INT-漂浮的转换;你可能不得不使用'float(myinteger)'来明确地转换它们。 –

回答

5

在你的shader开头的缺失#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 
+0

谢谢 - 你也碰巧知道如果1.0f也可以,或者GLSL只支持1.0吗? – dk123

+1

@ dk123:['#version 110']中没有'f'后缀(https://www.opengl.org/registry/doc/GLSLangSpec.Full.1.10.59.pdf)。 – genpfault