0

I have a VBO and an IBO in OpenGL, but am unable to draw them properly. Could you please let me know what I could have forgotten in the frame display function ? - struct Point3D is a struct with 3 floats inside (x,y,z). - nbVertex is the amount of vertexes in the glVertex array. - nbVBOInd is the amount of indices in the VBOInd array.

    glGenBuffers(1, &VertexVBOID);
    glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(struct Point3D)*nbVertex, glVertex, GL_STATIC_DRAW);

    glGenBuffers(1, &IndexVBOID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*nbVBOInd, VBOInd, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, sizeof(glVertex), BUFFER_OFFSET(0));   //The starting point of the VBO, for the vertices

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));   //The starting point of the IBO

Thanks !

Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89

2 Answers2

3

I see the same problem as rodrigo - you have data type mismatch, as you can see here:

glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*nbVBOInd, VBOInd, GL_STATIC_DRAW);

sizeof(int) - using integer type

glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));   

GL_UNSIGNED_SHORT - using short type

according to openGL specification, there are only unsigned data types possible for glDrawElements. To fix this you need:

  • change VBOInd to unsigned type in declaration like:

    unsigned int* VBOInd = new unsigned int[nbVBOInd]

  • replace 6th call with

    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*nbVBOInd, VBOInd, GL_STATIC_DRAW);

  • replace 11th (last) call with

    glDrawElements(GL_TRIANGLES, nbVBOInd, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

Anyway I believe that the problem is hidden in pointer setup, change 9th call to:

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

If that doesn't work, please show us how glVertex and VBOInd is declared and filled with data. Maybe you're using std::vector? You need to call these data containers like:

glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*nbVBOInd, &VBOInd[0], GL_STATIC_DRAW);

If something's unclear, just ask in comments..

Raven
  • 4,783
  • 8
  • 44
  • 75
2

Try changing the last line to:

glDrawElements(GL_TRIANGLES, nbVBOInd, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

Unless your data in the IndexVBOID are really short, but then, the sizeof(int) above would be wrong.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • Thanks for the correction, but changing this last line still doesn't work properly. After running it the first time, nothing is displayed, and after the second time, everything disappears. Is it trues that I should run all these commands at each frame display, or should I free up some things in between ? Thanks ! – Laurent Crivello Aug 18 '11 at 08:42
  • Unless your geometry changes you should write the first two paragraphs once, in the initialization code, to be run just once, and the two last paragraphs in the render function. Note that each call to glGenBuffers eats up graphics resources! – rodrigo Aug 18 '11 at 09:02
  • Ok, I'll try to optilmize this and see why I have some strange behaviours (maybe the vertex array is badly set after some refreshes, I'll check that too. Thanks ! – Laurent Crivello Aug 18 '11 at 09:41