# OpenGL Vertex buffer Objects: Strange glBufferData behaviour

This topic is 2814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys,

I've been working on modifying my application to use Vertex Buffer Objects, but I encountered a strange problem that I have been stuck with for days (which is probably related to the way I implemented it).

VBOs are rendered only if I actually pass the buffer data, using glBufferData, in the display function, just before calling glDrawArrays. If I pass the data to glBufferData beforehand (in the init functions) and simply remember the VBO reference, it seems that the data is somehow forgotten.

The relevant pieces of code can be found below. The object is rendered if I #define RENDER_BUFFER, and otherwise it's not. I checked for glErrors on every OpenGL call, but none were present.

[source]void Init(){
#ifndef RENDER_BUFFER
glGenBuffers(1, &mVboObject);
glBindBuffer(GL_ARRAY_BUFFER, mVboObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * mNumFaces * 3 , mVertices.get(), GL_STATIC_DRAW);
#else
glGenBuffers(1, &mVboObject);
#endif
}[/source]

[source]void Render(){
/*
* Draw the object
*/

glEnableClientState(GL_VERTEX_ARRAY);

#ifdef RENDER_BUFFER
//This renders perfectly
glBindBuffer(GL_ARRAY_BUFFER, mVboObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * mNumFaces * 3 , mVertices.get(), GL_STATIC_DRAW);

#else
//This doesn't render at all
glBindBuffer(GL_ARRAY_BUFFER, mVboObject);

#endif

glVertexPointer(3,GL_FLOAT,sizeof(Vertex),0);

glPushMatrix();
glScaled(20,20,20);
glDrawArrays(GL_TRIANGLES, 0, mNumFaces*3);
glPopMatrix();

glDisableClientState(GL_VERTEX_ARRAY);

glBindBuffer(GL_ARRAY_BUFFER, 0);
}[/source]

##### Share on other sites
Are you calling Init() before you have a rendering context?

##### Share on other sites
I definitely thought I did, but after your comment I went and checked to make sure and I somehow managed to start the main loop before actually initialising the object. In fact, I was creating the object twice, one time before and one time after the call to glutMainLoop and the GL Initialisation code.
So it looked like this:

 MyObject* obj = new MyObject("File.obj"); //This shouldn't have been here at all Graphics.init(); //The call to glutMainLoop was in here, which caused the problem obj = new MyObject("File.obj"); //This wasn't called until the glutMainLoop terminated 

Stupid mistake, but thanks for your help, saves me a long evening of debugging

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 15
• 21
• 26
• 11