I am very new to open gl and am struggling to understand relation between vaos vbos and ibos like how is currently bound vao related to currently bound ibo?
now i am trying to render 4 triangles with vertex data and index data for ibo as
float vertices[] = {
-0.5f, -0.5f, 2.0f, 1.0f, 0.0f, 0.0f ,1.0,
0.5f, -0.5f, 2.0f, 0.0f, 1.0f, 0.0f ,1.0,
0.0f, 0.5f, 2.0f, 0.0f, 0.0f, 1.0f ,1.0,
0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f ,1.0,
0.0f, 0.0f, 3.0f, 0.0f, 1.0f, 1.0f ,1.0,
0.0f, -1.0f, 2.0f, 1.0f, 0.0f, 1.0f ,1.0,
};
float vertices1[] = {
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f ,1.0,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f ,1.0,
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f ,1.0,
0.0f, 0.0f, -1.0f, 1.0f, 1.0f, 0.0f ,1.0,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f ,1.0,
0.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f ,1.0
};
unsigned indices[] = {
0,1,2,3,4,5
};
unsigned indices1[] = {
0,1,
1,2,
2,0,
3,4,
4,5,
5,3
};
and i am creating vaos and ibos as:
unsigned int VBO[2], VAO[2], IBO[2];
glGenVertexArrays(2, VAO);
glGenBuffers(2, VBO);
glGenBuffers(2, IBO);
glBindVertexArray(VAO[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glBindVertexArray(0);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(VAO[1]);
glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices1), vertices1, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices1), indices1, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glBindVertexArray(0);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
And I am drawing it as:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[0]);
glBindVertexArray(VAO[0]);
glDrawElements(GL_TRIANGLES, sizeof(indices1) / sizeof(unsigned), GL_UNSIGNED_INT, 0);
glBindVertexArray(0));
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[1]);
glBindVertexArray(VAO[1]);
glDrawElements(GL_LINES, sizeof(indices1)/sizeof(unsigned), GL_UNSIGNED_INT, 0);
glBindVertexArray(0));
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
If i dont unbind vao in the draw call then it will not be drawn as expected i.e. partially drawn lines or missing triangles. Are'nt vaos supposed to override any previous bindings and bind a new one specified? Even if i dont unbind vao after setting glVertexAttribPointer I get a blank screen! Then I got Totally confused on when to bind before doing what. What does vao hold other than vertex attribute?like can i unbind ibo before vao?