15

Is there any way to vary the point size when drawing lots of points? I know there's the glPointSize(float), but is there a way to do it in a 'batch' or array?

I would like the points to have different sizes based on an attribute of the data. Such as each point having x, y, z, and a size attribute. I'm using frame buffers right now in java.

Could I possibly use vertex shaders for this?

5 Answers 5

21

You can use point sprite: enable it using glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); and then you can use gl_PointSize attribute in your vertex program.

Vertex shader example taken from an OpenGL discussion thread:

void main() {
    gl_FrontColor=gl_Color;
    gl_PointSize = gl_Normal.x;
    gl_Position = ftransform();
} 
3
  • Thanks. Is there a way to set the point size using vertex attributes instead of using 'gl_pointSize = gl_Normal.x'?
    – max
    Jan 4, 2011 at 21:48
  • I guess that you can add your own float attribute to that shader and use glVertexAttrib1f or glVertexAttribPointer to set it. You might look at nehe.gamedev.net/data/articles/article.asp?article=21.
    – tibur
    Jan 4, 2011 at 22:06
  • Ok. Thanks. Are vertex attributes the standard way of passing extra data into a shader?
    – max
    Jan 4, 2011 at 23:32
3

This was what I ever did,

 //reset 
 glLoadIdentity();                                   

 //set size to 1 for a group of points
 glPointSize(1);                                     

 //group #1 starts here     
 glBegin(GL_POINTS);                                 

    //color of group #1 is white 
    glColor3f(1,1,1);                                

    for(int a=0; a<x; a++)
        for(int b=0; b<y; b++)
                    glVertex3f(a/953.,-b/413.,0.);   //location of points
 glEnd();


 //set size to 5 for another group of points
 glPointSize(5);  

 //group #2 starts here     
 glBegin(GL_POINTS);

    //color of group #2 is red 
    glColor3f(1,0,0);
    for(unsigned long int a=0; a<jd; a++)
    {
           glVertex3f(data[a].i,data[a].j,0);
    }
 glEnd();
2

I believe it's with glPointSize(GLfloat size)

Source:

http://www.talisman.org/opengl-1.1/Reference/glPointSize.html

1
  • 2
    As far as I understand, max wants to vary the size of the points but send them in only one batch.
    – tibur
    Jan 4, 2011 at 20:21
1

With GL_POINT_DISTANCE_ATTENUATION (GL14?) you can set the coefficients for a function (I think it's quadratic) for computing point size from Z distance.

1
  • 1
    It sounds like the OP wants to have a per-point size setting independent of distance.
    – genpfault
    Jan 4, 2011 at 21:07
-1

Increasing and decreasing the point size affects more than one pixel, but shaders are meant to be run only once per pixel. It will fail, because once the shader program has been run for a particular pixel, can the change only affect the following pixels, but not the previous pixels.

Shader programs are run on many shader units simultaneously and for many pixels in parallel making it impossible to do what you are trying to do. The limitation will be that one can set the pixel size with a shader program, but it will keep its size for all pixels the shader program gets run.

You could try to sort your point data by size and group points of the same size into one array and draw each array with a different point size.

Or you could try doing it with an indirection where you first render the different point sizes into a texture and in a second pass render the pixels by accessing the texture data and using it to test if a pixel should be rendered (or not).

1
  • There is much wrong here. In the GL, the shaders are never run once per pixel. Vertex shaders are executed once per (input) vertex, fragment shader once (or even multiple times) per fragment, geometry shaders once per input primitive and so on. And the GL does support rasterization of points with a variable point size, so that for one primitive with one vertex many fragments might be generated.
    – derhass
    Jul 21, 2014 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.