glDrawArray() gives a memory exception
Asked Answered
G

1

29

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.

Garnierite answered 24/4, 2011 at 9:57 Comment(13)
+1 for making a nice, thought-through question with the details. :) Keep it going.Bearcat
Everything you're doing looks to be okay, so perhaps this may be a driver bug; are your drivers on Windows up to date? If so, have you tried testing the code on a Windows box with a different GPU?Nicker
@Xeo, thank you :). @ void-pointer: Yes, it will run on other windows boxes (with different GPU's). My drivers are up to date.Garnierite
Does the machine in question actually support Buffer Objects? Even if the driver hands you the extension's function pointer it doesn't mean that the extension actually is supported. Check the extension string (glGetString(GL_EXTENSIONS);)Contraoctave
If it runs on other Windows boxes and your drivers are up to date, it may be a bug in the driver. If you don't mind, which GPU are you testing it on? Or as datenwolf suggests, it may be an unsupported feature.Nicker
My GPU is the NVIDIA GeForce GT 220, which should most definately support BufferObjects, in fact I run a test which should shut the program down if it doesn't support OpenGL 2.0+.Garnierite
I trust you've done the "obvious" debugging steps like calling glGet() with GL_ARRAY_BUFFER_BINDING immediately after the glBindBuffer() call and comparing with the value of mesh->vertexBuffer, and checking that the latter is not 0 for some reason? Does glGetError() return anything after the glBindBuffer() call?Moonfaced
If it runs on every computer other than that one box, have you considered it's the box, and not the app, that is at fault?Expediential
Have you tried using pure software rendering? that would at least tell you on which side of the aisle your problem is on.Isabelleisac
Off-topic, why on earth are you heap allocating vertexBuffer, just make it a field in the structure.Capri
Have you confirmed that the mesh pointer is valid and the memory exception is caused by vertexCount?Boracite
I had this issue before: make sure you are not enabling any other data arrays in OpenGL that don't have a buffer pointed to it. I used to have legacy color buffer enabled to highlight some bugs, and forgot to remove the piece of code. So I would enable glColorArray before I rendered the buffer, but I never assigned a data pointer pointing to the actual buffer containing colors. Does that make sense?Hillel
Just a note glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), &cubeVertices, GL_STATIC_DRAW);is wrong, you don't want &cubeVertices, just cubeVerticesOrnithischian
C
2
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.

Catarrhine answered 9/5, 2011 at 18:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.