Based on the information in Questions about glDrawRangeElements() and given the following items:
struct MyVertex
{
GLfloat x, y, z; //Vertex
};
const GLushort pindices[] = { 0, 1, 2, 3, 5, 4 };
struct MyVertex pvertex[6];
//VERTEX 0
pvertex[0].x = 0.0;
pvertex[0].y = 0.0;
pvertex[0].z = 0.0;
//VERTEX 1
pvertex[1].x = 1.0;
pvertex[1].y = 0.0;
pvertex[1].z = 0.0;
//VERTEX 2
pvertex[2].x = 0.0;
pvertex[2].y = 1.0;
pvertex[2].z = 0.0;
//VERTEX 3
pvertex[3].x = 0.0;
pvertex[3].y = 0.0;
pvertex[3].z = 0.0;
//VERTEX 4
pvertex[4].x = 1.0;
pvertex[4].y = 0.0;
pvertex[4].z = 0.0;
//VERTEX 5
pvertex[5].x = 0.0;
pvertex[5].y = -1.0;
pvertex[5].z = 0.0;
and the following initialization:
glGenBuffers(1, &VertexVBOID);
glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
glBufferData(GL_ARRAY_BUFFER, sizeof(struct MyVertex)*3, &pvertex[0].x, GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, sizeof(struct MyVertex), BUFFER_OFFSET(0));
glGenBuffers(1, &IndexVBOID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort)*3, pindices, GL_STATIC_DRAW);
glEnableClientState(GL_VERTEX_ARRAY);
the following calls:
glDrawRangeElements(GL_TRIANGLES, 0, 2, 3, GL_UNSIGNED_SHORT, (const GLvoid*) pindices);
glDrawRangeElements(GL_TRIANGLES, 3, 5, 3, GL_UNSIGNED_SHORT, (const GLvoid*) pindices+3);
should draw two triangles. However, I get an EXE_BAD_ACCESS. If I use pindices[0] in glDrawRangeElements; I get the triangle defined by the first three indices. Using pindices[3] in the second call to glDrawRangeElements doesn’t result in anything being drawn and I don’t get any OpenGl errors. At no time does the second glDrawRangeElements call draw the inverted triangle defined by the final three vertices. I feel like I’m missing something fundamental regarding the use of pointers here, but I haven’t been able to figure it out yet. Thoughts?
Your problems are not directly related to
glDrawRangeElements, but to incorrect use of VBOs.First of all, when using VBOs (or more precise, when binding a VBO to
GL_ELEMENT_ARRAY_BUFFER), the index pointer argument toglDraw...is interpreted as a byte offset into the buffer bound to theGL_ELEMENT_ARRAY_BUFFERbinding point (the index buffer), similar to how theGL_ARRAY_BUFFERaffects the array argument toglVertexPointer. Therefore the bad access. So replace(const GLvoid*)pindiceswith0in the firstglDrawRangeElementscall. The second call is wrong anyway, as you cannot do pointer arithmetic on a void pointer (it shouldn’t even compile, what size should it be increased by? bytes? shorts?). So in the second call use(const GLushort*)0+3to gain a byte offset to the 4th index in the index buffer.Second, your buffers only contain half of the needed data, as you use
sizeof(...)*3in bothglBufferDatacalls but your arrays actually contain 6 elements.