PDA

View Full Version : Question re gl_FragColor



sam_thedancer
03-09-2013, 04:03 AM
Greetings:
I had an understanding that a vertex shader must write gl_Position and a fragment shader must write gl_FragColor. But looking at some recent books (OpenGL 4+) it seems that only the first is true, while the fragment shader's out vec4 variable is automatically taken to define the color coordinates.

Am I getting this right? If so, it seems a bit odd: why doesn't the fragment shader have to write a special variable, while the vertex shader has to?

Thanks in advance for clarification,
Sam

Alfonse Reinheart
03-09-2013, 04:41 AM
Vertex shaders don't have to write to gl_Position either. They only have to write it if they're not:

a: followed by a Geometry (http://www.opengl.org/wiki/Geometry_Shader) or Tessellation (http://www.opengl.org/wiki/Tessellation) shader

b: using transform feedback and then not rendering the primitives

gl_FragColor was removed from GL 3.1 and above. To write to a color buffer, you need to define a user-defined output and set its color number (http://www.opengl.org/wiki/Fragment_Shader#Output_buffers) to a particular color buffer.

The reason for this is that gl_Position has a very specific meaning: it's the clip-space position. The post-shader vertex processing (http://www.opengl.org/wiki/Vertex_Post-Processing) stage will do a number of computations on this value. And there is only one such value per-vertex.

Fragment shaders can write multiple values, with each value going to different color buffers. So the user can (and must) define user-defined outputs, as well as route them properly to the expected color buffer. We still have gl_FragDepth, because there is only one depth buffer.

sam_thedancer
03-09-2013, 07:22 AM
Thanks, Alfonse, that clears it up. I really appreciate the quick response.