State in OpenGL

Posted by newprogrammer on Stack Overflow See other posts from Stack Overflow or by newprogrammer
Published on 2012-06-23T03:00:55Z Indexed on 2012/06/23 3:16 UTC
Read the original article Hit count: 134

Filed under:

This is some simple code that draws to the screen.

GLuint vbo;

glGenBuffers(1, &vbo);

glUseProgram(myProgram);

glBindBuffer(GL_ARRAY_BUFFER, vbo);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);

//Fill up my VBO with vertex data
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexes), &vertexes, GL_STATIC_DRAW);

/*Draw to the screen*/

This works fine. However, I tried changing the order of some GL calls like so:

GLuint vbo;

glGenBuffers(1, &vbo);

glUseProgram(myProgram);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);

//Now comes after the setting of the vertex attributes.
glBindBuffer(GL_ARRAY_BUFFER, vbo);

//Fill up my VBO with vertex data
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexes), &vertexes, GL_STATIC_DRAW);

/*Draw to the screen*/

This crashes my program. Why does there need to be a VBO bound to GL_ARRAY_BUFFER while I'm just setting up vertex attributes? To me, what glVertexAttribPointer does is just set up the format of vertexes that OpenGL will eventually use to draw things. It is not specific to any VBO. Thus, if multiple VBOs wanted to use the same vertex format, you would not need to format the vertexes in the VBO again.

© Stack Overflow or respective owner

Related posts about opengl