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

man*_*tta -1 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"
"}"
Run Code Online (Sandbox Code Playgroud)

这些是错误:

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

BDL*_*BDL 5

好吧,错误消息很清楚出了什么问题:

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

无法将vec4分配给浮点数。texture2D返回vec4,因此无法将其分配给浮点高度。解决方案:当您只需要一个通道时,添加一个麻烦的运算符:

float height = texture2D(texture,gl_TexCoord[0].st).r;
Run Code Online (Sandbox Code Playgroud)

除此之外,该着色器不应在任何大于140的glsl版本中进行编译,因为gl_TexCoord在150中已将其删除。texture2Dtexture1D方法都被texture150中的函数所替代。您是否真的用gl指定了glsl版本#version 400