PDA

View Full Version : gl_FragCoord and gl_PointCoord



OceanJeff40
10-23-2017, 09:37 AM
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 (https://www.youtube.com/channel/UCzx8alrxVELz5h1dfCdkdfg?view_as=subscriber)

OceanJeff40
10-23-2017, 09:48 AM
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

GClements
10-23-2017, 01:15 PM
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.

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.



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

OceanJeff40
10-23-2017, 10:10 PM
Ah, thank you for that, I appreciate the clarification, I didn't get that from the opengl specs description of the variable.

Thanks,
Jeff

john_connor
10-24-2017, 02:21 AM
https://www.khronos.org/opengl/wiki/Primitive#Point_primitives


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