1

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
2
  • 1
    "It compiles OK under GLSL 4.0 but fails on GLSL 3.0." There is no GLSL 3.0. There is a GLSL ES 3.00, but that's different. Commented Jul 6, 2016 at 13:39
  • 1
    There is no GLSL 3.5 anywhere. Commented Jul 6, 2016 at 15:10

2 Answers 2

7

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?

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.