OpenGL: vertex index buffer problem

I just started using VBOs and everything seems fine except for the vertex index buffer. If I call glDrawElements after turning on the index buffer, I get an access violation error (cannot find indexes), and if I just call it with a pointer to the beginning of the index array in memory, it will work.

//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 I configure it, but still I will send the 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 I will draw:

 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 string s-> draw () 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 calls to the / drawElements pointer, instead of the & .. [0] pointers at the end, I would like to use the vertex buffer index, but I cannot).

This is where the problem arises. I do not understand. I generate a buffer object, populate it with index data, but when it comes to drawing, it doesn't seem to find it. Does anyone know how to solve this problem?

thanks

EDIT: It seems to me that the compiler interprets the offset '0' in the buffer object as a pointer to the location “0” in memory, which causes an access violation error.

+4
source share
2 answers

You must 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 to gl*Pointer calls is the offset in the GPU buffer, not the memory address.

EDIT

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

+2
source

You probably don't need a call

 glEnableClientState(GL_INDEX_ARRAY) ... glDisableClientState(GL_INDEX_ARRAY) 

This allows gl to know that indexes come from an object, not a direct pointer.
God, I hate these features.

-2
source

Source: https://habr.com/ru/post/1338637/


All Articles