Q:

My VBO is always empty

I'm trying to render an obj file with a VBO but I have some problems. I have a Renderer object that has a method "setPositions()" that simply stores some data from a QVector to a VBO. The render part is in another method. I couldn't render anything, so I wanted to check if the array was really copied in the VBO. Instead of working on the Qvector, I wrote this simple code to make some test to render a simple square. As you guessed, the bufferSize variable is still at 0, and I have no render.
     /* setPositions() */        
     float vertices[] ={
          0, 0, 0,
          100, 0, 0,
          100, 100, 0,
          0, 100, 0
     };
     glGenBuffers(1, &pos); // Gluint pos : class attribute

     glBindBuffer(GL_ARRAY_BUFFER, pos);

     glBufferData(GL_ARRAY_BUFFER,
                   12 * sizeof (float),
                   NULL,
                   GL_STATIC_DRAW);
     glBufferSubData(GL_ARRAY_BUFFER,
                      0,
                      12 * sizeof (float),
                      &vertices[0]);

     int bufferSize = 0;
     glGetBufferParameteriv (GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &bufferSize);

     if (bufferSize != (12*sizeof(float)))
     {
          glDeleteBuffers (1, &pos);
          std::cout << "sizes mismatch. Buffer: " << bufferSize << " Array : " << 12*sizeof(float) << std::endl;
     }

     glBindBuffer (GL_ARRAY_BUFFER, 0);

     /* render() */
     glBindBuffer (GL_ARRAY_BUFFER, pos);

     glEnableClientState (GL_VERTEX_ARRAY);

     glVertexPointer (3, GL_FLOAT, 0, BUFFER_OFFSET (0));

     glDrawArrays (GL_QUADS, 0, 4);

     glDisableClientState (GL_VERTEX_ARRAY);

     glBindBuffer (GL_ARRAY_BUFFER, 0);

Does somebody have an idea ?

A:

the problem didn't come from the part of code I showed you.
I just read some doc and found the problem.
To have Qt and OpenGL working together properly, you have to set an OpenGL context.
But you have to make sure that you set it before any call to an OpenGL function (or at least a drawing function i guess). So, to have it working, I just added a line in the constructor of my OpenGL window (wich inherits from QGLWidget) :
this->makeCurrent ();

Magic.
Also, I found that Qt had a simple way to handle buffers with QGLBuffer.
 buf = new QGLBuffer (QGLBuffer::VertexBuffer); // I just declared a QGLBuffer *buf in my class attributes
 buf->create (); // equivalent to glGenBuffers()

 buf->bind (); // equivalent to glBindBuffer()

 /* The two next functions are the equivalent of glBufferData() (you have to call setUsagePattern before allocation) */
 buf->setUsagePattern (QGLBuffer::DynamicDraw);
 buf->allocate (data, 18 * sizeof (float));     

 buf->release (); // equivalent to glBindBuffer(target, 0)

For the render, you just have to bind the buffer as described above and use the regular OpenGL functions.
I hope it'll help somebody.

