开发者

OpenGL: problem with vertex indices buffer

开发者 https://www.devze.com 2023-02-09 01:55 出处:网络
I just started using VBOs, and everything seems to be fine except for the vertex indices buffer. If I call glDrawElements after enabling the indices buffer I get an access violation error (can\'t find

I just started using VBOs, and everything seems to be fine except for the vertex indices buffer. If I call glDrawElements after enabling the indices buffer I get an access violation error (can't find the indices) and if I simply call it with a pointer to the beginning of the indices array in memory it works..

//DOESN'T WORK
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
glDrawElements(GL_TRIANGLES, stripIndices.size(), GL_UNSIGNED_INT开发者_开发百科, 0);

//WORKS
glDrawElements(GL_TRIANGLES, stripIndices.size(), GL_UNSIGNED_INT, &stripIndices[0]);

I think I am doing everything right when setting it up, but still I'll post some code:

glGenBuffers(1,&vtxBuffer);
glGenBuffers(1,&nrmBuffer);
glGenBuffers(1,&clrBuffer);
glGenBuffers(1,&indices);

glBindBuffer(GL_ARRAY_BUFFER, vtxBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*vertices.size(), 
    &vertices[0], GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, nrmBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*normals.size(),
    &normals[0], GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, clrBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*colors.size(),
    &colors[0], GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*stripIndices.size(),
    &stripIndices[0], GL_STATIC_DRAW);

And to draw I do:

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);

glPushMatrix();
    glRotatef(25.f,0.f,1.f,0.f);
    s->draw();
glPopMatrix();

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);

The s->draw() line calls:

glBindBuffer(GL_ARRAY_BUFFER, vtxBuffer);
glVertexPointer(3,GL_FLOAT,0,&vertices[0]);

glBindBuffer(GL_ARRAY_BUFFER, clrBuffer);
glNormalPointer(GL_FLOAT,0,&colors[0]);

glBindBuffer(GL_ARRAY_BUFFER, clrBuffer);
glColorPointer(3,GL_FLOAT,0,&clrVtx[0]);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
glDrawElements(GL_TRIANGLES, stripIndices.size(), GL_UNSIGNED_INT, &stripIndices[0]);

(note: in all pointer/drawElements calls, instead of the &..[0] pointers at the end I would like to to use the vertex buffer subscript, but I can't).

Which is where the problem arises. I don't get it. I generate the buffer object, fill it with the indices data, but when it comes to drawing it doesn't seem seem to be finding it. Anyone has any idea on how to solve this issue?

Thanks

EDIT: It seems to me that the compiler is interpreting the offset '0' into the buffer object as a pointer to location '0' in memory which throws the access violation error.


You should try:

glBindBuffer(GL_ARRAY_BUFFER, vtxBuffer);
glVertexPointer(3,GL_FLOAT,0,0L);

glBindBuffer(GL_ARRAY_BUFFER, clrBuffer);
glColorPointer(3,GL_FLOAT,0,0L);

When a buffer is bound, the last argument of gl*Pointer calls is an offset on the GPU buffer, and not a memory address.

EDIT

Your indices seem to be of type int (looking at your glBufferData), but you use them as unsigned in your glDrawElements.


You're probably missing a call to

glEnableClientState(GL_INDEX_ARRAY)

...

glDisableClientState(GL_INDEX_ARRAY)

This allows gl to know the indices are coming from an object and not a direct pointer.
God I hate these functions.

0

精彩评论

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