c++ - GLSL 类型不一致

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

最佳答案

着色器开头缺少的 #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/

相关文章:

c++ - 简单的光线追踪器,漫反射着色问题c++

c++ - 从源代码中嵌入和读取图像

c++ - 使用现代 OpenGL 渲染我的第一个三角形

javascript - 如何在 webgl 着色器中使用 console.log?

c++ - QThread,在线程上创建 GUI 小部件元素

java - 丢失的比特去哪儿了?

opengl - 如何解释 GL_DEBUG_OUTPUT 消息?

c++ - 纹理图像显示不正确 - OpenGL

opengl - 如何在片段着色器中平铺部分纹理

unity-game-engine - 着色器中的 3D 纹理模拟(子像素相关)