Results 1 to 2 of 2

Thread: OpenGL Vertex Buffer Array/Object

  1. #1
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default OpenGL Vertex Buffer Array/Object

    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?

  2. #2
    Join Date
    Feb 2007
    Posts
    49
    Thanks
    4
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: OpenGL Vertex Buffer Array/Object

    Did you setup projective matrix and viewport?

    VBO is just buffer for vertices. Vertices are stored in server area (GPU).

    Qt Code:
    1. glGenBuffers(1, &vboID);
    2. glBindBuffer(GL_ARRAY_BUFFER, vboID);
    3. glBufferData(GL_ARRAY_BUFFER, 24 * 3 * sizeof(float), vertices, GL_STATIC_DRAW);
    To copy to clipboard, switch view to plain text mode 

    When you have bind VBO object then glVertexPointer (and others glColorPointer...) take their data from VBO. The last parameter (pointer to vertices) indicates offset in the binded VBO object. Now, you can store vertices, normals, colors in separate VBO objects, or all together in one VBO object. It's up to you.

Similar Threads

  1. get QImage object from buffer
    By Sheng in forum Qt Programming
    Replies: 2
    Last Post: 21st September 2012, 02:03
  2. Replies: 1
    Last Post: 9th November 2009, 14:16
  3. Replies: 5
    Last Post: 11th March 2008, 13:38
  4. showing menu mouseclick event with opengl object
    By validator in forum Qt Programming
    Replies: 1
    Last Post: 8th March 2008, 23:25
  5. OpenGL show image from buffer memory leak[SOLVED]
    By ^NyAw^ in forum Qt Programming
    Replies: 0
    Last Post: 30th January 2008, 16:21

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.