Names of `out` variables in a fragment shader

I’m having some problem understanding one line in the most basic (flat) shader example while reading OpenGL SuperBible.

In chapter 6, Listing 6.4 and 6.5 it introduces the following two very basic shaders.

6.4 Vertex Shader:

// Flat Shader
// Vertex Shader
// Richard S. Wright Jr.
// OpenGL SuperBible
#version 130

// Transformation Matrix
uniform mat4    mvpMatrix;

// Incoming per vertex
in vec4 vVertex;

void main(void) 
    { 
    // This is pretty much it, transform the geometry
    gl_Position = mvpMatrix * vVertex;
    }

6.5 Fragment Shader:

// Flat Shader
// Fragment Shader
// Richard S. Wright Jr.
// OpenGL SuperBible
#version 130

// Make geometry solid
uniform vec4 vColorValue;

// Output fragment color
out vec4 vFragColor;


void main(void)
   { 
   [b]gl_FragColor [/b]= vColorValue;
   }

My confusion is that it says vFragColor in the out declaration while saying gl_FragColor in main().

On the other hand, in code from the website, it has been corrected to ‘vFragColor = vColorValue;’ in the main loop.

What my question is that other then being a typo in the book, what is the rule for naming out values of shaders? Do they have to follow specific names?

On OpenGL.org I’ve found that gl_Position is required for the out of the vertex shader. Is there any such thing for the fragment shader? Or it is just that if there is only one output, then it will be the color in the buffer?

What happens when there is more then one out of a fragment shader? How does the GLSL compiler know which one to use in the buffer?

[QUOTE=zsolt.ero;1241050]On the other hand, in code from the website, it has been corrected to ‘vFragColor = vColorValue;’ in the main loop.
[/QUOTE]That is not a loop.

[QUOTE=zsolt.ero;1241050]What my question is that other then being a typo in the book, what is the rule for naming out values of shaders? Do they have to follow specific names?[/QUOTE]Depends on glsl version - for nowadays use whatever you wish.

[QUOTE=zsolt.ero;1241050]On OpenGL.org I’ve found that gl_Position is required for the out of the vertex shader. Is there any such thing for the fragment shader? Or it is just that if there is only one output, then it will be the color in the buffer?
What happens when there is more then one out of a fragment shader? How does the GLSL compiler know which one to use in the buffer?[/QUOTE]Again, depends on glsl version. There used to be some named outputs - but not anymore. You can set the output buffer by “location” layout specifier inside the shader or specify it via relevant gl* functions. If doing neither then (i think) it numbers them in the order they are defined in glsl source.

Check out the reference cards (iirc they all list the names where color tells whether it is deprecated or not): http://www.google.com/search?q=opengl+reference+card&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

Thanks, so I can just use any name as long as I have a single out variable? I’m thinking about using OpenGL 3.3+

Yes. For clarity i would write it out tho (ie. add the location explicitly):

layout(location=0) out vec4 someMeaningfulName;

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.