3
votes

the following simple fragment shader code fails, leaving me with an uninformative message in the log: ERROR: 0:1: 'gl_Color' : syntax error syntax error

void main()
{
  vec4 myOutputColor(gl_Color);
  gl_FragColor = myOutputColor;
}

while the following one works:

void main()
{
  glFragColor = gl_Color;
}

This boggles my mind, as in Lighthouse3D's tutorial gl_Color is said to be a vec4. Why can't I assign it to another vec4?

2
Is the error message really authentic? Is it normal/expected for it to miss the column/line number, and include the "syntax error" text twice? - unwind
yes, the error message is authentic (I have omitted an underscore in the examples, hence the edit). I don't really know what's normal/expected with GLSL info logs; in most of the cases the location of the error wasn't displayed, just the problematic variable / function name. (The environment is OS X, xcode, OpenGL 2.0 with an ATI driver v1.5) - zyndor
bumpbump* Hey slacker! Did it work? ;) - ralphtheninja
sure did. I was hesitant whose answer shall I accept because both work just fine, but I ended up with yours for brevity / human-readability. - zyndor
The second answer is just as correct really, but there isn't any need to use the vec4() constructor, since both are of the same type. If you had lets say a (r,g,b,w) tuple you could write: vec4 myOutputColor = vec4(r, g, b, w); - ralphtheninja

2 Answers

7
votes

Try normal assignment. Like this:

void main()
{
  vec4 myOutputColor = gl_Color;
  gl_FragColor = myOutputColor;
}

Edit:

The second answer is just as correct really, but there isn't any need to use the vec4() constructor, since both are of the same type. If you had lets say a (r,g,b,w) tuple you could write:

vec4 myOutputColor = vec4(r, g, b, w);

or

// assuming myRgbColor is a vec3
vec4 myOutputColor = vec4(myRgbColor, w);

etc

4
votes

Aparrently you should use slightly different syntax

(see OpenGL Shading Language Specification )

vec4 myOutputColor = vec4(gl_Color);
gl_FragColor = myOutputColor;

this unlike your sample compiles fine on my mashine (Windows, Nvidia card)