Results 1 to 4 of 4

Thread: Getting color from FFP in standalone frag shader?

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    23

    Getting color from FFP in standalone frag shader?

    Hi everyone, I'm writing a very, very simple fragment shader. This is compiled by itself, so that the FFP can supply its own vertex shader.

    Code :
    void main() {
        vec4 color = gl_ColorSoFar;
        gl_FragData[0] = vec4(color.rgb * color.a, color.a);
        gl_FragData[1] = vec4(1.0);
        //gl_FragColor = color;
    }

    I'm sure you've spotted the error: there's no such thing as gl_ColorSoFar! I have no idea what to put there. I know that in the previous stage of the FFP, the vertex shader spits out some color, how do I grab it and continue with it?

    Thanks!

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Getting color from FFP in standalone frag shader?

    Hi,
    I can give u two solutions for this, the first is the answer (using FFP) and the second is what u should use now (modern opengl 3.0 and above GLSL version 330).
    Old GLSL version using FFP
    The per vertex color attribute comes in gl_Color register in the fragment shader if you assign to gl_FrontColor in the vertex shader. Add this to your vertex shader.
    Code :
    //vertex shader
    void main() {
       gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
       gl_FrontColor = gl_Color;
    }
    Then in the fragment shader, you may use gl_Color attributes as follows.

    Code :
    //Fragment shader
    void main() {
       gl_FragColor = gl_Color;
    }

    Modern GLSL version
    Now u assign the per vertex attributes yourself and also maintain the matrices. So the same shader becomes this.
    Code :
    //vertex shader
    #version 330
    in vec4 vVertex;
    in vec4 vColor;
     
    smooth out vec4 oColor;
    uniform mat4 MVP;
    void main() {
       gl_Position = MVP * vVertex;
       oColor = vColor;
    }
    Code :
    //fragment shader
    #version 330
    smooth in vec4 oColor;
    out vec4 vFragColor;
    void main() {
       vFragColor = oColor;
    }
    Note that you need to maintain the attributes and matrices yourself.

    Hope this helps,
    Mobeen
    Regards,
    Mobeen

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    23

    Re: Getting color from FFP in standalone frag shader?

    Thank you so much! It worked perfectly.

    That stuff is really interesting...

    So it used to be that glVertex() would assign a value to gl_FragCoord, but now we have to make our own function to use instead, that will bind a position to vVertex?

  4. #4
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,673

    Re: Getting color from FFP in standalone frag shader?

    So it used to be that glVertex() would assign a value to gl_FragCoord
    Not directly. glVertex() would be transformed by the current matrices into a clip-space value, which then would be processed by the OpenGL hardware into a window-space coordinate. The interpolation of that coordinate over a triangle's face would result in gl_FragCoord for a particular fragment.

Similar Threads

  1. Export shader as standalone
    By acroscene in forum OpenGL: GLSL
    Replies: 0
    Last Post: 10-10-2018, 12:33 PM
  2. Point Frag Shader: If statement causes inconsistent color output.
    By CaptainSnugglebottom in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 02-21-2018, 11:12 PM
  3. Frag Shader vs BlendFunc
    By imported_CRasterImage in forum OpenGL: GLSL
    Replies: 2
    Last Post: 09-12-2012, 12:15 PM
  4. vert shader - no frag shader
    By supagu in forum OpenGL: GLSL
    Replies: 1
    Last Post: 12-10-2004, 06:10 AM
  5. Why need to have one vertex and one frag shader?
    By Bruce Sinner in forum OpenGL: GLSL
    Replies: 4
    Last Post: 09-02-2004, 06:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean