在GLSL 1.30上编译片段着色器时出错

时间:2016-07-06 12:41:42

标签: c++ opengl glsl

以下片段着色器有什么问题?它在GLSL 4.0下编译好,但在GLSL 1.30上失败。

这是代码:

// Fragment Shader
"uniform sampler2D texture;\n"
"uniform sampler1D cmap;\n"
"uniform float minZ;\n"
"uniform float maxZ;\n"
"\n"
"void main() {\n"
"    float height = texture2D(texture,gl_TexCoord[0].st);\n"
"    float lum = (height-minZ)/(maxZ-minZ);\n"
"    if (lum > 1.0) lum = 1.0;\n"
"    else if (lum < 0.0) lum = 0.0;\n"
"    gl_FragColor = texture1D(cmap, lum);\n"
"}"

这些是错误:

FRAGMENT glCompileShader "" FAILED
FRAGMENT Shader "" infolog:
0:7(2): error: initializer of type vec4 cannot be assigned to variable of type float
0:8(2): error: initializer of type vec4 cannot be assigned to variable of type float
0:9(6): error: operands to relational operators must be scalar and numeric
0:9(6): error: if-statement condition must be scalar boolean
0:9(17): error: value of type float cannot be assigned to variable of type vec4
0:10(11): error: operands to relational operators must be scalar and numeric

2 个答案:

答案 0 :(得分:4)

嗯,错误信息非常清楚错误:

0:7(2): error: initializer of type vec4 cannot be assigned to variable of type float
----
float height = texture2D(texture,gl_TexCoord[0].st);

无法将vec4分配给浮点数。 texture2D返回vec4,因此无法将其指定给float高度。 解决方案:当您只需要一个频道时添加一个混音运算符:

float height = texture2D(texture,gl_TexCoord[0].st).r;

除此之外,着色器不应该在任何glsl版本中编译&gt; 140,因为在{150}中删除了gl_TexCoord。同样适用于texture2Dtexture1D方法,它被150中的texture函数替换。您是否真的指定了glsl版本与#version 400

答案 1 :(得分:3)

You need to declare the version that you want to compile. As explained in Core Language GLSL @ opengl.org

  

除了空格和注释外,#version指令必须出现在着色器中的任何其他内容之前。如果#version指令没有出现在顶部,那么它假定为1.10,这几乎肯定不是你想要的。