Hey,

I wanted to use the vertex buffer array or object from OpenGl, but I can't get it to work.

This is what I do:

I create a widget and setup the opengl stuff.

Qt Code:
  1. void Gui::GLWidget::initializeGL(){
  2. glShadeModel(GL_SMOOTH); // Enable Smooth Shading
  3. glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
  4. glClearDepth(1.0f); // Depth Buffer Setup
  5. glEnable(GL_DEPTH_TEST); // Enables Depth Testing
  6. glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
  7. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
  8.  
  9. GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
  10. GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
  11. GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f };
  12.  
  13. glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // Setup The Ambient Light
  14. glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
  15. glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
  16.  
  17. glEnable(GL_LIGHT1);
  18. glEnable(GL_CULL_FACE);
  19. glCullFace(GL_BACK);
  20. }
To copy to clipboard, switch view to plain text mode 

Then I paint the stuff in here:

Qt Code:
  1. void Gui::GLWidget::paintGL(){
  2. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
  3. glLoadIdentity(); // Reset The Current Modelview Matrix
  4. glTranslatef(0.0, 0.0, -30.0); // Move Left 1.5 Units And Into The Screen 6.0
  5.  
  6. world->render();
  7. }
  8.  
  9. void Bfe::World::render(){
  10. qreal timeElapsed = ((qreal)time.elapsed() / 1000.0);
  11. time.restart();
  12.  
  13. camera->render();
  14.  
  15. heightMap->render(wireFrame);
  16.  
  17. for(QVector<Render::RenderObject *>::Iterator i = objects.begin(); i < objects.end(); i++)
  18. {
  19. (*i)->render();
  20. }
  21. }
  22.  
  23. //I start rendering the objects with my for loop, which is one cube at the moment. But it doesn't draw
  24.  
  25. Render::Cube::Cube(){
  26. //Create the object matrix
  27. // cube ///////////////////////////////////////////////////////////////////////
  28. // v6----- v5
  29. // /| /|
  30. // v1------v0|
  31. // | | | |
  32. // | |v7---|-|v4
  33. // |/ |/
  34. // v2------v3
  35. //
  36. //
  37. // TODO: Vertex Array doesn't work yet!
  38.  
  39. // vertex coords array
  40. GLfloat vertices[] = {1,1,1, -1,1,1, -1,-1,1, 1,-1,1, // v0-v1-v2-v3
  41. 1,1,1, 1,-1,1, 1,-1,-1, 1,1,-1, // v0-v3-v4-v5
  42. 1,1,1, 1,1,-1, -1,1,-1, -1,1,1, // v0-v5-v6-v1
  43. -1,1,1, -1,1,-1, -1,-1,-1, -1,-1,1, // v1-v6-v7-v2
  44. -1,-1,-1, 1,-1,-1, 1,-1,1, -1,-1,1, // v7-v4-v3-v2
  45. 1,-1,-1, -1,-1,-1, -1,1,-1, 1,1,-1}; // v4-v7-v6-v5
  46. this->vertices = &vertices[0];
  47. // normal array
  48. GLfloat normals[] = {0,0,1, 0,0,1, 0,0,1, 0,0,1, // v0-v1-v2-v3
  49. 1,0,0, 1,0,0, 1,0,0, 1,0,0, // v0-v3-v4-v5
  50. 0,1,0, 0,1,0, 0,1,0, 0,1,0, // v0-v5-v6-v1
  51. -1,0,0, -1,0,0, -1,0,0, -1,0,0, // v1-v6-v7-v2
  52. 0,-1,0, 0,-1,0, 0,-1,0, 0,-1,0, // v7-v4-v3-v2
  53. 0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1}; // v4-v7-v6-v5
  54. this->normals = &normals[0];
  55.  
  56. // color array
  57. GLfloat colors[] = {1,1,1, 1,1,0, 1,0,0, 1,0,1, // v0-v1-v2-v3
  58. 1,1,1, 1,0,1, 0,0,1, 0,1,1, // v0-v3-v4-v5
  59. 1,1,1, 0,1,1, 0,1,0, 1,1,0, // v0-v5-v6-v1
  60. 1,1,0, 0,1,0, 0,0,0, 1,0,0, // v1-v6-v7-v2
  61. 0,0,0, 0,0,1, 1,0,1, 1,0,0, // v7-v4-v3-v2
  62. 0,0,1, 0,0,0, 0,1,0, 0,1,1}; // v4-v7-v6-v5
  63. this->colors = &colors[0];
  64.  
  65. }
  66.  
  67. void Render::Cube::render(){
  68. glEnableClientState(GL_NORMAL_ARRAY);
  69. glEnableClientState(GL_COLOR_ARRAY);
  70. glEnableClientState(GL_VERTEX_ARRAY);
  71.  
  72. glNormalPointer(GL_FLOAT, 0, normals);
  73. glColorPointer(3, GL_FLOAT, 0, colors);
  74. glVertexPointer(3, GL_FLOAT, 0, vertices);
  75.  
  76. glPushMatrix();
  77. // rotate + half the size, else the cube is somewhat under the ground
  78. glTranslatef(position.x(), position.y() + 1, position.z());
  79.  
  80. glDrawArrays(GL_FLOAT, 0, 24);
  81.  
  82. glPopMatrix();
  83.  
  84. glDisableClientState(GL_NORMAL_ARRAY);
  85. glDisableClientState(GL_COLOR_ARRAY);
  86. glDisableClientState(GL_VERTEX_ARRAY);
  87.  
  88. }
To copy to clipboard, switch view to plain text mode 

Can anyone give me a heads-up on what I'm doing wrong?

Also, can anyone tell me if and how I can use a vertex buffer object?