c++openglglm-mathvertex-buffervao

OpenGL correct way to draw multiple objects?


I am trying to understand how to correctly draw multiple objects, and to do that i wanted to draw a cube face by face. Problem is i obviously didn't get it right because only the first face is being drawn. I have a 24 vertex structure (4 vertex per face) and 36 indices (6 per face, 3 per triangle). Feel free to correct me on anything.


UPDATED

//array of vertex -- this is only the first face

Vertex vertices[24] = {
    // vertex 0
    {glm::vec3(-1.0f, -1.0f, 1.0f),  glm::vec3( 1.0f, 0.0f, 0.0f), glm::vec2(0.0f,0.0f), glm::vec3(0.0,0.0,0.0), glm::vec3(0.0,0.0,0.0)},
    // vertex 1
    {glm::vec3( 1.0f, -1.0f, 1.0f),  glm::vec3( 1.0f, 1.0f, 0.0f), glm::vec2(1.0f,0.0f), glm::vec3(0.0,0.0,0.0), glm::vec3(0.0,0.0,0.0)}, 
    // vertex 2
    {glm::vec3( 1.0f,  1.0f, 1.0f),  glm::vec3( 0.0f, 0.0f, 1.0f), glm::vec2(1.0f,1.0f), glm::vec3(0.0,0.0,0.0), glm::vec3(0.0,0.0,0.0)},
    // vertex 3
    {glm::vec3(-1.0f,  1.0f, 1.0f),  glm::vec3( 0.0f, 1.0f, 0.0f), glm::vec2(0.0f,1.0f), glm::vec3(0.0,0.0,0.0), glm::vec3(0.0,0.0,0.0)},

unsigned int indices[36] = {
    0, 2, 1, // front
    2, 0, 3,
    8, 10,9, // face dx
    10,8,11,
    12,14,13,// face sx
    14,12,15,
    4, 6, 5, // back face
    6, 4, 7,
    20,22,21,// bottom face
    22,20,23,
    16,18,17,// top face
    18,16,19
};

// creation and population of buffers
// i am only putting here two because it's the same process for all six faces
// face 1
glGenVertexArrays(1,&global.vao0);   
glGenBuffers(1,&global.vb0);
glBindVertexArray(global.vao0);
glBindBuffer(GL_ARRAY_BUFFER,global.vb0);
glGenBuffers(1,&global.ib0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,global.ib0);    
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*4, &vertices[0], GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*6, &indices[0], GL_STATIC_DRAW);

//face 2
glGenVertexArrays(1,&global.vao1);    
glGenBuffers(1,&global.vb1);
glBindVertexArray(global.vao1);
glBindBuffer(GL_ARRAY_BUFFER,global.vb1);
glGenBuffers(1,&global.ib1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,global.ib1);    
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*4, &vertices[4], GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*6, &indices[6], GL_STATIC_DRAW);

Then i go and draw them

//my vertex struct has 5 attributes of course, but don't mind those because that's not the point
//drawing face 1
glBindVertexArray(global.vao0);
glBindBuffer(GL_ARRAY_BUFFER,global.vb0);    
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, global.ib0);    
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 
    reinterpret_cast<GLvoid*>(offsetof(struct Vertex, position)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 
     reinterpret_cast<GLvoid*>(offsetof(struct Vertex, color)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), 
     reinterpret_cast<GLvoid*>(offsetof(struct Vertex, textcoord)));
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 
     reinterpret_cast<GLvoid*>(offsetof(struct Vertex, normal)));
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 
     reinterpret_cast<GLvoid*>(offsetof(struct Vertex, tangent)));
global.t0.Bind(GL_TEXTURE0); // texture representing this face
glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
glBindVertexArray(0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glDisableVertexAttribArray(3);
glDisableVertexAttribArray(4);

//drawing face 2
glBindVertexArray(global.vao1);
glBindBuffer(GL_ARRAY_BUFFER,global.vb1);    
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, global.ib1);    
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 
    reinterpret_cast<GLvoid*>(offsetof(struct Vertex, position)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 
     reinterpret_cast<GLvoid*>(offsetof(struct Vertex, color)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), 
     reinterpret_cast<GLvoid*>(offsetof(struct Vertex, textcoord)));
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 
     reinterpret_cast<GLvoid*>(offsetof(struct Vertex, normal)));
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 
     reinterpret_cast<GLvoid*>(offsetof(struct Vertex, tangent)));
global.t1.Bind(GL_TEXTURE0); // different texture to see if i'm drawing it correctly
glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
glBindVertexArray(0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glDisableVertexAttribArray(3);
glDisableVertexAttribArray(4);

... same thing for other faces

What am i doing wrong?


Solution

  • global.vb1 contains 4 vertices. However, the indices contained in global.ib1 are 8, 10, 9, 10, 8, 11. What do you expect? There are just 4 vertices in the buffer. Hence the indices need to be in range [0, 3].

    If you split the vertex into 6 buffers with 4 vertices each, the corresponding indexes for each of them must be 0, 1, 2, 0, 2, 3:

    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*6, &indices[6], GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,global.ib1); 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*6, &indices[0], GL_STATIC_DRAW);