Yola
Yola

Reputation: 19019

Draw array of points with OpenGL

Why in this program four points drawn by glVertex3f rendered in pointed positions, but instead of four array points rendered only one in the center of the window?

#include <GL/glew.h>
#include <GL/glut.h>

#include <iostream>
#include <vector>

using namespace std;

struct point3 {
    GLfloat x, y, z;
};

vector<point3> Vertices(4);

void display(void)
{
    glClear (GL_COLOR_BUFFER_BIT);
    GLuint abuffer;
    glGenVertexArrays(1, &abuffer);
    glBindVertexArray(abuffer);
    GLuint buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    Vertices = {{0.3f, 0.3f, 0.5f}, {0.6f, 0.6f, 0.5f}, {0.6f, 0.3f, 0.5f}, {0.3f, 0.6f, 0.5f}};
    glBufferData(GL_ARRAY_BUFFER, Vertices.size() * sizeof(Vertices[0]), &Vertices[0], GL_STATIC_DRAW);
    glVertexPointer(3, GL_FLOAT, 0, &Vertices[0]);
    glDrawArrays(GL_POINTS, 0, Vertices.size());
    glFlush();
    glBegin(GL_POINTS);
    glVertex3f(0.25f, 0.25f, 0.5f);
    glVertex3f(0.25f, 0.75f, 0.5f);
    glVertex3f(0.75f, 0.75f, 0.5f);
    glVertex3f(0.75f, 0.25f, 0.5f);
    glEnd();    
    glFlush();
} 

void init (void)
{
    glClearColor (0.0, 0.0, 0.0, 0.0);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

} 

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("simple OpenGL example");
    glutDisplayFunc(display);
    GLenum err = glewInit();
    if (GLEW_OK != err) {
        cerr << "Error: " << glewGetErrorString(err) << endl;
    }
    cout << "Status: Using GLEW" << glewGetString(GLEW_VERSION) << endl;    
    init();
    glutMainLoop();
}

Upvotes: 2

Views: 8113

Answers (1)

Kos
Kos

Reputation: 72221

You haven't enabled the attribute to actually use the array. You do that by:

  • (OpenGL 2) glEnableClientState(GL_VERTEX_ARRAY) (or GL_COLOR_ARRAY, etc)
  • (OpenGL 3) glEnableVertexArray(attributeId)

Without that, what you point to with glVertexPointer (GL2) or glVertexAttribPointer (GL3) won't be actually used.

Upvotes: 2

Related Questions