سؤال

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);
هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top