4

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.

Zepee
  • 1,640
  • 3
  • 20
  • 40
  • Could you update your question with the code you're actually using? For example, fix the calls to glVertexPoint with the suggestions in tibur's answer. Moreover, your code initializes a normal buffer ; and then, uses a color buffer. It's hard to say anything whil your code seems to be incomplete. – rotoglup Feb 06 '11 at 22:25
  • Good point, I didn't notice that – Zepee Feb 06 '11 at 22:55

2 Answers2

2

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.

tibur
  • 11,531
  • 2
  • 37
  • 39
  • Yes yes of course, I missed that... but it still doesn't work.. If it helps, when the program crashes it comes up with a 'Access violation reading location 0x0000000' exception (at the glDrawElements line) – Zepee Feb 06 '11 at 15:55
  • EDIT: It seems the problem is with the offset.. It seems to be interpreting it as pointer wil value 0, instead of the offset.. any ideas? – Zepee Feb 06 '11 at 16:01
  • Could you check that `indices!=0`, check also that your indices are all between 0 and `vertices.size()/3` – tibur Feb 06 '11 at 16:20
  • Yes, they are indeed. As I said, it works if I don't use the buffer objects (using the &vertex[0] pointers instead of the offset into the buffer). I don't know, my current gpu is supposedly compatible with VBOs.. it seems the glDrawElements is interpreting the last param as a pointer instead of a buffer offset.. – Zepee Feb 06 '11 at 17:25
  • I had noticed that (int/uint) and went ahead and changed everything to GLfloat and GLuint just in case opengl was making some kind of weird error with the types. It didn't make any difference however.. – Zepee Feb 06 '11 at 18:40
-2

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.

shoosh
  • 76,898
  • 55
  • 205
  • 325
  • 1
    That didn't do it... And from what I read, GL_INDEX_ARRAY seems to be connected to some sort of color indices, and not the indices used by glDrawElements (corect me if I'm wrong). – Zepee Feb 06 '11 at 23:02