c++openglglutfreeglut

How to use Vertex Array Object?


I am trying to simulate a cube bouncing between walls. For this purpose, I have constructed 2 vertex array objects, one of them representing the cube and the other representing walls. Firstly, I created walls. I am drawing walls with glDrawElements call since I have used index (element) buffers to create it. I have also created a cube model, but I am not using index buffer for it, so I will use glDrawArrays call to draw it as far as I know. My walls are showing up nicely, but I cannot figure out how to draw the cube (at this step, I am not even trying to animate the cube, I just want to draw it) This is what my code looks like:

#include "Cube.h"

#include <cstring>

GLuint vao[2];

void init()
{
    cube();

    GLuint program = InitShader( "vshader.glsl", "fshader.glsl" );
    glUseProgram( program );
    GLuint vPosition = glGetAttribLocation(program, "vPosition");
    glGenVertexArrays(2, vao);

    glBindVertexArray(vao[0]);
    GLuint cube_buffer;
    glGenBuffers(1, &cube_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, cube_buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(cubePoints), cubePoints, GL_STATIC_DRAW);
    glEnableVertexAttribArray(vPosition);
    glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);

    const float mid_value  = 0.0f;
    const float far_value  = 0.35f;
    const float near_value = 1.0f;
    const float positions[60] = {
        /// positions               /// colors
        mid_value, -near_value, 1.0, 0.0, 0.0, /// 0
        near_value, -near_value, 1.0, 0.0, 0.0, /// 1
        near_value, mid_value, 0.0, 1.0, 0.0, /// 2
        near_value, near_value, 0.0, 1.0, 0.0, /// 3
        mid_value, near_value, 1.0, 1.0, 0.0, /// 4
        -near_value, near_value, 1.0, 1.0, 0.0, /// 5
        -near_value, mid_value, 0.0, 0.0, 1.0, /// 6
        -near_value, -near_value, 0.0, 0.0, 1.0, /// 7
        far_value, -far_value, 1.0, 0.0, 0.0, /// 8
        far_value, far_value, 0.0, 1.0, 0.0, /// 9
        -far_value, far_value, 1.0, 1.0, 0.0, /// 10
        -far_value, -far_value, 0.0, 0.0, 1.0 /// 11
    };

    unsigned int indices[36] = {
        7,11,0,
        0,8,1,
        1,8,2,
        2,9,3,
        3,9,4,
        4,10,5,
        5,10,6,
        6,11,7,
        11,0,8,
        8,2,9,
        9,4,10,
        10,6,11,
    };

    glBindVertexArray(vao[1]);
    GLuint room_buffer;
    glGenBuffers(1, &room_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, room_buffer);
    glBufferData(GL_ARRAY_BUFFER, 60 * sizeof(float), positions, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*)0);  /// positions
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*)(2* sizeof(float)));  /// colors

    GLuint index_buffer;
    glGenBuffers(1, &index_buffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(unsigned int), indices, GL_STATIC_DRAW);

    glClearColor(1.0, 1.0, 1.0, 1.0);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);


    // glDrawArrays(...); --> how to use this, or should I really use this here?

    glutSwapBuffers();
}

void idle() {
    glutPostRedisplay();
}

void reshape(int width, int height) {
    glViewport(0, 0, width, height);
}

int main( int argc, char **argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowSize( 600, 600 );

    glutCreateWindow( "Bouncing Cube" );
    glewExperimental = GL_TRUE;
    glewInit();

    init();

    glutDisplayFunc(display);
    glutIdleFunc(idle);
    glutReshapeFunc(reshape);

    glutMainLoop();
}

In this code, cubePoints is filled by cube() method in another translation unit. I am not exactly sure how to draw cube on the screen, and how to use glDrawArrays here? Should I bind or unbind anything to draw it? I feel like I am not using vertex array objects properly. Below is what my walls look like:

walls

I simply want cube to appear on the far side, where the white rectangle is located at. Is it possible to call glDrawArrays with glDrawElements?


Solution

  • You must bind the Vertex Array Object before the "draw" call. The drawing instruction uses the data stored in the currently bound Vertex Array Object:

    void display()
    {
        glClear(GL_COLOR_BUFFER_BIT);
    
        glBindVertexArray(vao[0]);
        glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);
    
        glBindVertexArray(vao[1]);
        glDrawArrays(...);
    
        glutSwapBuffers();
    }