GLSL fragment shader syntax error

本小妞迷上赌 提交于 2019-12-10 19:26:07

问题


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?


回答1:


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




回答2:


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)



来源:https://stackoverflow.com/questions/882527/glsl-fragment-shader-syntax-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!