简体   繁体   中英

GLSL fragment shader syntax error

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?

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM