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 5 of 5

Thread: gl_FragCoord and gl_PointCoord

  1. #1
    Intern Contributor
    Join Date
    Dec 2016
    Location
    Ocean Shores, WA USA
    Posts
    66

    gl_FragCoord and gl_PointCoord

    Hey all,

    I'm new to OpenGL, and to this forum as well, and I appreciate everyone's kind support, this is a great community!

    Anyways, I wanted to ask about these two built in input variables in the fragment shaders: gl_FragCoord and gl_PointCoord.

    I have tried to play around with both of them several times, and I don't get anything from either of them. Here's my current fragment shader, and where gl_PointCoord is, I've tried using gl_FragCoord, just to get a vertical stripe on the points (which are size 35 in vertex shader, and they do properly display on-screen as 35x35 squares...!).

    Vertex shader:

    Code :
    #version 330 core
    layout (location = 0) in vec2 position;
    layout (location = 1) in vec3 color;
     
    out vec3 ourColor;
     
    void main()
    {
        gl_PointSize = 35.0f;
        gl_Position = vec4(position.x, position.y, 0.0f, 1.0f); 
        ourColor = color;
    }

    Fragment shader:

    Code :
    #version 330 core
    in vec3 ourColor;
     
    out vec4 color;
     
    void main()
    {
        if ( gl_FragCoord.x > 0.4 && gl_FragCoord.x < 0.6 )
        {
            color = vec4(0.0f,0.0f,1.0f,1.0f);
        } else {
            color = vec4(ourColor, 1.0f);
        }
    }

    I'm not trying to do anything meaningful, just wanting to experiment and get a feel for these variables, but they are proving to be quite the buggers to even work with them.... any ideas?

    So if I use gl_PointCoord in the place of gl_FragCoord above, shouldn't that put a vertical stripe of blue on the squares themselves? and using the gl_FragCoord as I listed above, shouldn't that make a vertical column of blue squares like an overlay on the screen as the squares pass through that column on the screen? They are animated and bouncing around the screen, as I am using my fireworks demo for this small experimental project.

    Thanks in advance,
    Jeff
    My YouTube channel for OpenGL Basics Tutorials

  2. #2
    Intern Contributor
    Join Date
    Dec 2016
    Location
    Ocean Shores, WA USA
    Posts
    66
    Ok, so I just found a thread on reddit that fixes this with this brute force line of code:

    Code :
    glEnable(0x8861);

    This is the old enum constant for GL_POINT_SPRITE apparently, which is no longer supported? Anyways, it enables gl_PointCoord functionality.

    Um, that's crazy.

    Any ideas on the gl_FragCoord functionality.... of course, I will continue scouring the internet for answers.....

    Jeff

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,477
    Quote Originally Posted by OceanJeff40 View Post
    Ok, so I just found a thread on reddit that fixes this with this brute force line of code:

    Code :
    glEnable(0x8861);

    This is the old enum constant for GL_POINT_SPRITE apparently, which is no longer supported? Anyways, it enables gl_PointCoord functionality.

    Um, that's crazy.
    GL_POINT_SPRITE is effectively forced on in the core profile (which doesn't support non-sprite points). In the compatibility profile, point sprites are an option (and are disabled by default).

    If your headers don't define GL_POINT_SPRITE, then they don't support the compatibility profile. But if that glEnable() is required, your program is using a compatibility profile.

    Quote Originally Posted by OceanJeff40 View Post
    Any ideas on the gl_FragCoord functionality
    gl_PointCoord is normalised (coordinates range from 0.0 to 1.0, so 1.0 is the width of the point), while gl_FragCoord is in pixels (coordinates range from 0.0 to the width/height of the window, and 1.0 is the width/height of a single pixel).

  4. #4
    Intern Contributor
    Join Date
    Dec 2016
    Location
    Ocean Shores, WA USA
    Posts
    66
    Ah, thank you for that, I appreciate the clarification, I didn't get that from the opengl specs description of the variable.

    Thanks,
    Jeff

  5. #5
    Member Regular Contributor
    Join Date
    May 2016
    Posts
    435
    https://www.khronos.org/opengl/wiki/...int_primitives

    Quote Originally Posted by GL wiki
    Points are rasterized as screen-aligned squares of a given window-space size. The size can be given in two methods: by the last active vertex processing shader stage or by the context's state. To set the point size from a shader, enable the glEnable with argument (GL_PROGRAM_POINT_SIZE) to set the point size from the program. If GL_PROGRAM_POINT_SIZE is enabled, then the point size comes from the output variable float gl_PointSize. If it is disabled, the point size is constant for all points in a primitive, and is set by the glPointSize function.
    you need to glEnable(GL_PROGRAM_POINT_SIZE); if you want to set the point's size in the shader

Posting Permissions

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