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:

#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:

#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

Ok, so I just found a thread on reddit that fixes this with this brute force line of 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

[QUOTE=OceanJeff40;1289048]Ok, so I just found a thread on reddit that fixes this with this brute force line of 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.
[/QUOTE]
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.

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).

Ah, thank you for that, I appreciate the clarification, I didn’t get that from the opengl specs description of the variable.

Thanks,
Jeff

https://www.khronos.org/opengl/wiki/Primitive#Point_primitives

you need to glEnable(GL_PROGRAM_POINT_SIZE); if you want to set the point’s size in the shader