开发者

Using glDrawRangeElements in OpenGL 2.1

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 de开发者_开发百科fined 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 to glDraw... is interpreted as a byte offset into the buffer bound to the GL_ELEMENT_ARRAY_BUFFER binding point (the index buffer), similar to how the GL_ARRAY_BUFFER affects the array argument to glVertexPointer. Therefore the bad access. So replace (const GLvoid*)pindices with 0 in the first glDrawRangeElements call. 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+3 to 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(...)*3 in both glBufferData calls but your arrays actually contain 6 elements.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜