Question re gl_FragColor

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,

Vertex shaders don’t have to write to [var]gl_Position[/var] either. They only have to write it if they’re not:

a: followed by a Geometry or Tessellation shader

b: using transform feedback and then not rendering the primitives

[var]gl_FragColor[/var] 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 to a particular color buffer.

The reason for this is that [var]gl_Position[/var] has a very specific meaning: it’s the clip-space position. The post-shader vertex 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 [var]gl_FragDepth[/var], because there is only one depth buffer.

Thanks, Alfonse, that clears it up. I really appreciate the quick response.