manatttta
manatttta

Reputation: 3124

Error compiling fragment shader on GLSL 1.30

what is wrong with the following fragment shader? It compiles OK under GLSL 4.0 but fails on GLSL 1.30.

This is the code:

// 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"
"}"

These are the errors:

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

Upvotes: 1

Views: 2656

Answers (2)

Amadeus
Amadeus

Reputation: 10655

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

The #version​ directive must appear before anything else in a shader, save for whitespace and comments. If a #version​ directive does not appear at the top, then it assumes 1.10, which is almost certainly not what you want.

Upvotes: 3

BDL
BDL

Reputation: 22168

Well, the error messages are very clear about what is wrong:

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

One cannot assign a vec4 to a float. texture2D returns a vec4, so it cannot be assigned to the float height. Solution: Add a swizzle operator when you only need one channel:

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

Beside this, the shader should not compile in any glsl version > 140, since gl_TexCoord was removed in 150. Same goes for the texture2D and texture1D method which got replaced by the texture function in 150. Are you really specifying the glsl version with #version 400?

Upvotes: 7

Related Questions