How to change buffers for glDrawElements

Viewed 49

I am drawing to rectangle using glDrawElements.

glBindVertexArray(m_VAO);
glDrawElements(GL_TRIANGLES, 6 , GL_UNSIGNED_INT, 0);
glBindVertexArray(0);

I want to draw two different geometry one facing the camera and one on the backside.

This is the data for drawing.

verticesRect = {
        // Positions                                      // Normal Coords          // Texture Coords
        (width / 2.0f) + taper,  height / 2.0f, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 0.0f,   // Top Right
        width / 2.0f, -height / 2.0f, 0.0f,              0.0 , 0.0, 1.0 ,     1.0f, 1.0f,   // Bottom Right
       -width / 2.0f, -height / 2.0f, 0.0f,              0.0 , 0.0, 1.0 ,     0.0f, 1.0f,   // Bottom Left
       (-width / 2.0f) - taper, height / 2.0f, 0.0f,     0.0 , 0.0, 1.0 ,     0.0f, 0.0f    // Top Left 
    };


     std::vector<float> verticesRectBack = {
         // Positions                                    // Normal Coords          // Texture Coords
        (width / 2.0f) + taper,  height / 2.0f, 0.0f,    0.0 , 0.0, -1.0 ,     1.0f, 0.0f,   // Top Right
        width / 2.0f, -height / 2.0f, 0.0f,              0.0 , 0.0, -1.0 ,     1.0f, 1.0f,   // Bottom Right
       -width / 2.0f, -height / 2.0f, 0.0f,              0.0 , 0.0, -1.0 ,     0.0f, 1.0f,   // Bottom Left
       (-width / 2.0f) - taper, height / 2.0f, 0.0f,     0.0 , 0.0, -1.0 ,     0.0f, 0.0f    // Top Left 
     };

     verticesRect.insert(verticesRect.end(), verticesRectBack.begin(), verticesRectBack.end());   // Combining both the data sets.

This is the VAO , VBO configuration of data.

glGenVertexArrays(1, &m_VAO);
    glGenBuffers(1, &m_VBO);
    glGenBuffers(1 , &m_EBO);
    glBindVertexArray(m_VAO);
    glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
    glBufferData(GL_ARRAY_BUFFER,verticesRect.size() * sizeof(float), &verticesRect[0], GL_STATIC_DRAW);    
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicesTaperRectangle), indicesTaperRectangle, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));

This is the data for indices.

GLuint indicesTaperRectangle[] = {  // Note that we start from 0!
    0, 1, 3, // First Triangle
    1, 2, 3  // Second Triangle
};

Currently i am only able to draw the first rectangle how can i draw the second rectangle.

1 Answers

This has nothing to do with buffers. What you're experiencing is back face culling and you can go by with just a single draw call if you disable it using glDisable(GL_CULL_FACE).

Another option is to make your geometry two rectangles, and flip its winding. For that you can reuse the vertex positions and just duplicate and flip the indices:

GLuint indicesTaperRectangle_FrontAndBack[] = {
    0, 1, 3,
    1, 2, 3,
    3, 2, 1,
    3, 1, 0 
};
Related