Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glDrawArray() gives a memory exception

While looping through all objects I want to render in my 3D-engine, I get an error when trying to call

glDrawArrays(mesh->primitiveType, 0, mesh->vertexCount); 

Because it tries to read from location 0x0000000, so apparently the pointer bound to the mesh->vertexBuffer index points to zero. This all happens within my RenderableObject class. Instances of this class have a mesh bound to them, and this mesh contains an index that should link to the VertexArray. But apparently

glBindBuffer(GL_ARRAY_BUFFER, mesh->vertexBuffer);

Is failing.

The strange thing is, that it will run on my mac, and various other windows computers - but it won't run on this (windows) computer. Because I'm testing I removed all 3D models, and found it's the primitives that are causing the problem, somehow the MSVC++ compiler "optimized" my code, to remove everything after

glGenBuffers(1, &CubeMesh.vertexBuffer);

And that's probably why nothing got bound, or so I thought. I disabled linker/compiler optimizations, and I could see allbreakpoints would get hit now - but I still get the same exception, and I'm absolutely clueless as to why it doesn't work.

The entire source of the project can be found @ https://github.com/Wrap/TwinGame/tree/master/src , as far as I can tell the problem is in the Primitives.cpp, and/or the RenderableObject.cpp (specifically the RenderableObject::Draw(); method) file. Am I trying to read something that is protected, what is wrong with the LoadPrimitives(); method?

Thanks for taking the time to read this.

like image 493
Jan-Willem Buurlage Avatar asked Apr 24 '11 09:04

Jan-Willem Buurlage


1 Answers

RenderableObject::RenderableObject(ObjectManager* objectmgr) : Object(objectmgr), visible(true), scale(1.0f), mesh(0) {
    mObjMgr->registerRenderable(this);
}

I think here is your problem mesh(0). Maybe you are calling the Draw() method before you initialize this value. Maybe your Renderer class does it.

Try to add assert(mesh != 0) before any function call.

void RenderableObject::Draw(class ShaderManager* shaderMgr){
    //TODO: iterate through all meshes that belong to the object

    assert(mesh != 0)

    glBindBuffer(GL_ARRAY_BUFFER, mesh->vertexBuffer[0]);

And I hope, that you are checking, if GL_ARB_vertex_buffer_object is available. I hope this works.

like image 115
mani3xis Avatar answered Oct 14 '22 10:10

mani3xis