gl_PointCoord anyone?

Hey all,

I’m working on a new series, experimenting with Point Sprites.

I’ve got this line in my code to enable point sizes in the shader:

glEnable(GL_PROGRAM_POINT_SIZE);

I’ve also got this line in the vertex shader:

gl_PointSize = 30.0f;

And I’m using the gl_PointCoord.x and gl_PointCoord.y in the fragment shader, and I can’t get it to do anything.

I’ve tried discarding point greater than a certain value… nothing. Less than a certain value… nothing.

Any ideas on making something? Anything? with gl_PointCoord?? I just want to play around, literally anything would be great, even something simple (which is what I thought I was trying).

Alright, thanks,

Jeff

Do you understand when gl_PointCoord is undefined?

Maybe that’s the part I’m missing. When is it undefined? I haven’t seen any documentation on this, and I’ve googled a bunch…

Any help would be appreciated, especially if you have an example of how to work with this built in variable.

Thanks,

Jeff

Read the documentation, specifically the GLSL specification that defines gl_PointCoord:

Next question: do you understand how to enable point sprites?

Point Sprites are enabled by default, there is no setting to enable point sprites:

My intellisense shows just this:

GL_POINT_SIZE
GL_POINT_SIZE_GRANULARITY
GL_POINT_SIZE_RANGE
GL_POINT_SPRITE_COORD_ORIGIN

there is no constant for GL_POINT_SPRITE

So, nobody has even a small example of manipulating this built in variable?

I’m not surprised… since the example from the book that I’m studying didn’t work, maybe the feature is deprecated.

I hope not,
Jeff

This is the video series that I was going to do, I’ve done the first video, the rest was going to be tinkering with this built-in variable, but I don’t think it’s working…

and I can’t find any sample code on making it work either… but I will…I must… for I am a … hobby programmer.

https://www.youtube.com/watch?v=73XoxNQ4WNc&t=3s

Jeff

That’s true in a core profile GL context, or in ES2, ES3, or WebGL.
But it’s not true in a compatibility profile GL context, or in ES1.

So, what context profile are you using? And do your included headers match your context profile?

It took me three minutes to write and test a complete, minimal, working example using gl_PointCoord:


//
// gl_PointCoord
//
// cc sprite.c -framework OpenGL -framework GLUT -Wno-deprecated-declarations -o sprite && ./sprite
//

#include <GLUT/glut.h>
#include <stdlib.h>

GLint prog;

void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_POINTS);
        glVertex2f(0, 0);
    glEnd();

    glutSwapBuffers();
    glutPostRedisplay();
}

void init(void)
{
    glPointSize(32);    
    glEnable(GL_POINT_SPRITE);

    const GLchar *src =
    "#version 120
"
    "void main() {
"
    "    gl_FragColor = vec4(gl_PointCoord, 0.0, 1.0);
"
    "}";    

    prog = glCreateProgram();
    GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fs, 1, &src, NULL);
    glCompileShader(fs);    
    glAttachShader(prog, fs);
    glLinkProgram(prog);
    glDeleteShader(fs);    
        
    glUseProgram(prog);
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayString("double rgba");
    glutInitWindowSize(480, 320);
    glutCreateWindow("Sprite");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    
    init();
    glutMainLoop();
    return 0;
}

This uses GLUT and a compatibility profile context. On OSX (GL2.1, Nvidia, Intel, and Software renderers), it produces a black window with one 32x32 sprite in the center. The sprite shows gl_PointCoord as a black->red gradient from left->right, and black->green gradient from top->bottom.

The only interesting part of that code is glEnable(GL_POINT_SPRITE). Commenting that line out results in “undefined” behavior; the sprite changes to all-black on Nvidia and software renderers, and is unchanged on Intel.

Anybody else have any other examples of using this? I still haven’t gotten this to work for me.

Links to examples would be great too.

Thanks in advance,

Jeff

P.S. Perhaps other profiles? etc.

It’s been 5 years, but I know I’ve seen this used in an NVidia example, possibly in their GPU Computing SDK.

I may have an example at home. In the interim, check out the point sprite shaders in the OpenGL Programming Guide (9th edition) examples: