Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Names of `out` variables in a fragment shader

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2012
    Posts
    10

    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:

    Code :
    // 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:

    Code :
    // 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?

  2. #2
    Intern Contributor
    Join Date
    Jul 2006
    Posts
    70
    Quote Originally Posted by zsolt.ero View Post
    On the other hand, in code from the website, it has been corrected to 'vFragColor = vColorValue;' in the main loop.
    That is not a loop.

    Quote Originally Posted by zsolt.ero View Post
    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?
    Depends on glsl version - for nowadays use whatever you wish.

    Quote Originally Posted by zsolt.ero View Post
    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?
    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=openg...ient=firefox-a

  3. #3
    Junior Member Newbie
    Join Date
    Jul 2012
    Posts
    10
    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+

  4. #4
    Intern Contributor
    Join Date
    Jul 2006
    Posts
    70
    Yes. For clarity i would write it out tho (ie. add the location explicitly):

    layout(location=0) out vec4 someMeaningfulName;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •