VBO with glDrawRangeElements

archidea
Unregistered
 
Post: #1
Hi,
I am facing for the first time the VBO chapter.
I can quite draw my vertex array with
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
glDrawArrays(GL_TRIANGLES, 0, totTriangles);

but I can't understand how to work with indexes and
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer);
glDrawRangeElements(GL_TRIANGLE_STRIP, 0, totIndex, totIndex,
GL_UNSIGNED_INT, NULL);

Specifically I don't know how to calculate the value of totIndex, and whether and how to fill the m_indexBuffer array. Any help would be appreciated. Thanks.
Quote this message in a reply
Member
Posts: 144
Joined: 2004.07
Post: #2
Ah, I remember this question from the Mac-OpenGL mailing list, let's see how much I can help.

Here's some quick sample code:
Code:
    GLuint vboIndices; // the vbo indices reference index/value
std::vector<unsigned short> indices; // the indices in an STL vector (you can use a plain old C array here, or malloc, whatever)

// fill in the indices array with the indices you'll be rendering with, these are the same indices you'd use in say immediate mode (glBegin(), glEnd()) to render the geometry out

glGenBuffersARB(1, &vboIndices); // create a reference value for this VBO object
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vboIndices); // bind it for use
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, indices.size()*sizeof(unsigned short), &(indices.front()), GL_STATIC_DRAW_ARB);  // place the indices data in the buffer for later use.  For this example I'm using GL_STATIC_DRAW_ARB as I don't need to change the index data later, you've got other drawing options though.

// and now onto the rendering code
// enable and set the vertex/normal/color/texcoord/etc data

glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vboIndices);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT, 0);  // GL_TRIANGLES is just an example, to use glDrawRangeElements() you'd do somehing like glDrawElements(GL_TRIANGLES, 0, 100, indices.size(), GL_UNSIGNED_SHORT, 0)

So as you can see 'm_indexBuffer' (and in my case vboIndices) is simply a reference number used by OpenGL and your application to keep track of the different buffer objects, it doesn't actually store any index information. That's all handled in 'indices' in my example, which I pass to the GPU using glBufferDataARB().

Buffer objects and VBOs are a little confusing at first, but after a little bit of time with them you'll realize how great and logical they really are.

EDIT: urg, I don't like how small the code window is, is there a way I can change the vertical height of it? I'd recommend for now copy and pasting the code from that block into a text editor.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Terribly odd display when using shader and glDrawRangeElements on a Powerbook G4 applmak 4 3,218 Mar 15, 2007 06:13 AM
Last Post: applmak