PDA

View Full Version : glDrawRangeElements and index using



lobbel
06-29-2010, 06:46 AM
Hello,
I have a question according to the use of glDrawRangeElements. I set up a vbo and ibo and uploaded vertex and index data to draw a simple cube. When
I call glDrawRangeElements(GL_TRIANGLES, 0, 2, 3)
then i draws the first triangle. So far so good. But
when I call glDrawRangeElements(GL_TRIANGLES, 3, 5, 3)
also the first triangle is draw instead of the second.
But when I call glDrawRangeElements(GL_TRIANGLE, 0, 5, 6)
it draws both triangles.

I use opengl 3.2 core profile.

So my vertex struct is


struct Vertex
{
float position[3]; // size: 12
float normal[3]; // size: 12
float texcoord[3]; // size: 12
float color[4]; // size: 16
};

Makes a size of 52 bytes on my computer. I set up
8 vertices for the cube from 0,0,0 to 1,1,1. Each face of
the cube consists of 2 triangles. So my indices are


GLuint idx[] = {0,1,2,
0,2,3,
4,5,6,
4,6,7,
0,4,7,
0,7,3,
1,5,6,
1,6,2,
3,2,6,
3,6,7,
0,1,5,
0,5,4
};


I update the vbo and ibo as follows



/* bind buffers buffer */
glBindBufferARB(GL_ARRAY_BUFFER, vertexbufferId);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, indexbufferId);
/* init buffers, allocate memory */
glBufferDataARB(GL_ARRAY_BUFFER, vbSize, NULL, vbUsage);
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER, ibSize, NULL, ibUsage);
/* unbind buffers */
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

/* bind buffer */
glBindBufferARB(GL_ARRAY_BUFFER, vertexbufferId);
/* map pointer */
float* ptr = (float*)glMapBuffer(GL_ARRAY_BUFFER,GL_WRITE_ONLY) ;
if(ptr)
{
/* copy data */
:memcpy(&ptr[offset], vertices, size);
/* unmap buffer */
glUnmapBuffer(GL_ARRAY_BUFFER);
}

/* unbind buffer */
glBindBufferARB(GL_ARRAY_BUFFER, 0);


/* bind buffer */
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexbufferId);
/* mappointer */
GLuint* ptr = (GLuint*)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
if(ptr)
{
/* update index */
::memcpy(&ptr[offset], index, size);
/* unmap buffer */
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
}

/* unbind buffer */
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);


drawing the vbo



glBindBuffer(GL_ARRAY_BUFFER, vertexbufferId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexbufferId);
glDrawRangeElements(GL_TRIANGLES, 3, 5, 3, GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);


So, actually the vertices and indices are there. But I dont
understand why I can draw all vertices but not a specific
range.

What made I wrong ?

regards,
lobbel

lobbel
06-29-2010, 09:08 AM
Solved,
I forgot to set the offset in glDrawRangeElements.
Actually start and end index are only suggestions for
opengl. The actual index adressing is done by

glDrawRangeElements(GL_TRIANGLES, 3, 5, 3, GL_UNSIGNED_INT, sizeof(GLuint) * startIdx);

regards,
lobbel