1

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?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Fra
  • 45
  • 5
  • What are the vertices and what are the indices? – Rabbid76 Oct 29 '20 at 17:15
  • They are two arrays. Vertices contains every vertex of the cube i want to draw, while indices contains the indexes of those vertices i will pass in the to draw triangle by triangle for every face. Basically structs with 5 vectors – Fra Oct 29 '20 at 17:23
  • I'm interested in the values of the indices. Especially in the indices of `global.ib1` – Rabbid76 Oct 29 '20 at 17:26
  • i updated the question, hope it's clear – Fra Oct 29 '20 at 17:34
  • `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]. ((I suspected that, so I asked about the indexes of `global.ib1`) – Rabbid76 Oct 29 '20 at 17:38
  • 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. – Rabbid76 Oct 29 '20 at 17:43
  • You're right. That's correct. I didn't notice! Thank you – Fra Oct 29 '20 at 17:44

1 Answers1

1

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);
Rabbid76
  • 202,892
  • 27
  • 131
  • 174