OpenGL 3.0: Access Violation exception caused by VBOs
I am updating a quite outdated OpenGL project. So I am trying to move from vertex arrays to VBOs, but I have an access violation exception that I cannot trace.
So I am using a VBO and an IBO that I generate in my class constructor:
if (glewIsSupported("GL_VERSION_3_0")) {
glGenBuffers(2, bufferIds);
}
(glewInit() was already called)
The various rendering objects are defined as:
struct RenderBufferVertexElement
{
NxVec3 position;
NxVec3 normal;
float texCoord[2];
};
// The rendering buffers
RenderBufferVertexElement* mVertexRenderBuffer;
NxU32* mIndexRenderBuffer;
// Buffer Objects
enum {
VBO,
IBO
};
GLuint bufferIds[2];
Then in my drawing function I do:
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
// VBO
glBindBuffer(GL_ARRAY_BUFFER, bufferIds[VBO]);
glBufferData(GL_ARRAY_BUFFER, sizeof(mVertexRenderBuffer), mVertexRenderBuffer, GL_STREAM_DRAW);
glVertexPointer(开发者_Python百科3, GL_FLOAT, sizeof(RenderBufferVertexElement), BUFFER_OFFSET(offsetof(RenderBufferVertexElement,position)));
glNormalPointer(GL_FLOAT, sizeof(RenderBufferVertexElement), BUFFER_OFFSET( offsetof(RenderBufferVertexElement,normal) ));
// IBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferIds[IBO]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(mIndexRenderBuffer), mIndexRenderBuffer, GL_STREAM_DRAW);
textures.front()->setClientActive();
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(RenderBufferVertexElement), BUFFER_OFFSET(offsetof(RenderBufferVertexElement,texCoord)));
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures.front()->textureId);
[...]
glDrawElements(GL_TRIANGLES, numElements, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glDisableClientState(...);
glDisable(GL_TEXTURE_2D);
Everything worked fine with old style calls to glVertexPointer... but now my program crashes with an access violation exception. Any idea where that might come from?
Thanks
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Don't call them. They are not needed and deprecated. You just need to tell OpenGL how your data is organized with Attribute Sets. Take a look at this Tutorial: http://arcsynthesis.org/gltut/
And also, do not use GLew. It causes glErrors() to appear because it uses a deprecated way to enumerate Extensions. Use Gl3w instead.
精彩评论