0

After learning about VBOs, a friend told me to try using VAOs for linking cube indices with the vertices. I followed about every tutorial I could find with no avail. It looks like the buffers are binding correctly and everything works up until I try to draw them.

This is how I generate the VAO -

glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);

glEnableVertexAttribArray(0);

glGenBuffers(1, &cubeIndex);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIndex);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(GLuint), cubeIndices, GL_STATIC_DRAW); 
glVertexAttribPointer(0, 3, GL_UNSIGNED_INT, GL_FALSE, sizeof(GLuint)*3, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

glEnableVertexAttribArray(1);

glGenBuffers(1, &cubeVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeVBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 21 * sizeof(GLfloat), cubeVertPoints, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*3, 0);

glBindVertexArray(0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

Rendering -

glEnableClientState(GL_VERTEX_ARRAY);
glBindVertexArray(VAO);

glDrawArrays(GL_TRIANGLES, 0, size);//Runtime exception here if anything larger that '2' is provided?

glBindVertexArray(0);
tcoy
  • 169
  • 1
  • 2
  • 11

1 Answers1

1

Your code does not make sense. You never bind anything to GL_ARRAY_BUFFER, so your glVertexAttribPointer() calls will point to invalid VBOs - explaining the crash when you try to draw that thing.

I'm not sure what you're trying to do here. It looks like cubeIndex is supposed to be the element array for indexed rendering. But you seem to try to use the indices as vertex attributes. But you should use that as GL_EMEMENT_ARRAY_BUFFER (as you already do) in conjunction with glDrawElements() (what you don't do). Your cubeVBO should be a GL_ARRAY_BUFFER, and you should use that as a vertex attribute.

The sizes of your array also seem strange. 36 Uints do make sense as index buffer when drawing a cube with 6 sides and 2 triangles each. But 21 floats for the vertex data with 3 components per vector are just 7 vertices - strange for a cube.

Please have a look at the Vertex Specification and Vertex Rendering pages in the OpenGL wiki, or look into some tutorials - the code you posted so far makes me think that you didn't grasp the underlying concepts.

derhass
  • 38,787
  • 2
  • 42
  • 61
  • Do I need to be calling glEnableVertexAttribArray(1); at all? – tcoy Feb 16 '14 at 21:25
  • @tcoy: Depends on what attrib 1 is. In your case, you seem to need just one attribute, which one typically would bind to index 0. – derhass Feb 16 '14 at 21:28
  • I read up on the wiki articles and the OpenGL reference pages, and I fixed up my code. Whenever I call glEnableVertexAttribArray() it causes the runtime error, however when I remove it the program runs but doesn't draw anything – tcoy Feb 16 '14 at 21:30
  • @tcoy: Well. If you don't enable any array, your VBOs are not really used. If it crashes when you enable it, your attrib pointers are not correctly set up. It is hard to tell what might be going on without seeing the actual code... – derhass Feb 16 '14 at 21:59
  • Thanks. Well it turns out, I was missing two commas in my vertex array /facepalm. I have it somewhat working now, it resembles a cube however my indices are incorrect. – tcoy Feb 16 '14 at 23:05