user1022795
user1022795

Reputation:

OpenGL array of points

I have the following code to draw an array of points but it only draws one point in the center. How can I draw an array of 2D points using OpenGL?

GLint NumberOfPoints = 10;
GLfloat x[2],y[2];

glBegin( GL_POINTS );

for ( int i = 0; i < NumberOfPoints; ++i )
{
    glVertex2f( x[i], y[i] );

}
glEnd();

Upvotes: 2

Views: 13516

Answers (4)

genpfault
genpfault

Reputation: 52166

Requires GLUT for window and context management:

#include <GL/glut.h>
#include <vector>
#include <cstdlib>

struct Point
{
    float x, y;
    unsigned char r, g, b, a;
};
std::vector< Point > points;

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-50, 50, -50, 50, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // draw
    glColor3ub( 255, 255, 255 );
    glEnableClientState( GL_VERTEX_ARRAY );
    glEnableClientState( GL_COLOR_ARRAY );
    glVertexPointer( 2, GL_FLOAT, sizeof(Point), &points[0].x );
    glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(Point), &points[0].r );
    glPointSize( 3.0 );
    glDrawArrays( GL_POINTS, 0, points.size() );
    glDisableClientState( GL_VERTEX_ARRAY );
    glDisableClientState( GL_COLOR_ARRAY );

    glFlush();
    glutSwapBuffers();
}

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

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

    glutInitWindowSize(640,480);
    glutCreateWindow("Random Points");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

     // populate points
    for( size_t i = 0; i < 1000; ++i )
    {
        Point pt;
        pt.x = -50 + (rand() % 100);
        pt.y = -50 + (rand() % 100);
        pt.r = rand() % 255;
        pt.g = rand() % 255;
        pt.b = rand() % 255;
        pt.a = 255;
        points.push_back(pt);
    }    

    glutMainLoop();
    return 0;
}

Screenshot

Upvotes: 10

starrify
starrify

Reputation: 14781

Your code just works. Fill the array x and y with randomized values and this would draw random points.

The problem may be you can't 'see' the points you draw. That's obvious since:

  1. By default the color of the points is black( 0, 0, 0, in rgb), and you may want to set it to some other value using glColor3f or such functions.

  2. You've drawn too few points and each point is too small(actually only 1 pixel size on your screen). You may want to draw circles instead or draw thousand or more pixels and check again.

By the way, please format your question and let the code displayed normally.


Edited:

See my comment above. If you didn't set up a valid OpenGL context or you just didn't know how, check this http://openglbook.com/the-book/chapter-1-getting-started/ and get started with your first working OpenGL program.

Upvotes: 0

Josh1billion
Josh1billion

Reputation: 14927

Where are you setting the values for x[0], x[1], y[0], and y[1]?

If it's only drawing one point in the center, it sounds like the values are set to 0 for all four of those variables. Be sure to initialize their values before you reference them in your call to gVertex2f().

Upvotes: 3

NickLH
NickLH

Reputation: 2645

Do you define what x[i] and y[i] are? Otherwise they will be set to 0 automatically (hence the centering). Also, creating the arrays with two elements but accessing 10 elements is very bad since you are accessing memory locations that you do not have control over.

You should do something like :

GLint NumberOfPoints = 10;
GLfloat x[10],y[10];

for(int i = 0; i < NumberOfPoints; i++){
    x[i] = y[i] = (GLfloat) i;
}

glBegin( GL_POINTS );

for ( int i = 0; i < NumberOfPoints; ++i )
{
    glVertex2f( x[i], y[i] );

}
glEnd();

Upvotes: 1

Related Questions