Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 ?

share|improve this question

closed as too localized by Trevor Powell, Sean Middleditch, Nick Wiggill, Josh Petrie, Sam Hocevar Jan 6 '13 at 16:34

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

3  
My idea: learn how to fish and use glGetError() or gDEBugger. –  Sam Hocevar Dec 20 '12 at 14:55
    
I'm starting to feel like OpenGL should have been designed to show errors without explicit calls to glGetError().. –  Jari Komppa Dec 20 '12 at 15:07
    
haha ! I'm totally new to OpenGL, I couldn't guess. I'll try. –  jean Dec 20 '12 at 15:30
1  
Why not copy the data with glBufferData? I assume you're using glBufferData to set the GL_STATIC_DRAW, but you can copy the data with the same call, without the need of glBufferSubData.. It's possible that the driver gets stubborn with GL_STATIC_DRAW and figures that since you said it was static, it's not getting updated, period =) –  Jari Komppa Dec 20 '12 at 17:09
1  
So, things to try: copy data with glBufferData, or try GL_DYNAMIC_DRAW. –  Jari Komppa Dec 20 '12 at 17:10

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
o_O. Okay. Remember to mark your answer as correct so it doesn't show up as unanswered. –  Jari Komppa Dec 21 '12 at 9:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.