0
votes

I am trying to create two separate objects to be rendered at the same time, a cube and a sphere. The issue is once I add the code for the sphere, the cube is missing some faces etc.

Here is my code:

    glGenBuffers(1, &g_VBO);
    glBindBuffer(GL_ARRAY_BUFFER, g_VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*g_numberOfVertices[0], g_pMeshVertices[0], GL_DYNAMIC_DRAW);

    glGenBuffers(1, &g_IBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_IBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLint) * 3 * g_numberOfFaces[0], g_pMeshIndices[0], GL_DYNAMIC_DRAW);
    glGenVertexArrays(1, &g_VAO);

    glBindVertexArray(g_VAO);
    glBindBuffer(GL_ARRAY_BUFFER, g_VBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_IBO);
    glVertexAttribPointer(positionIndex, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, position)));
    glVertexAttribPointer(normalIndex, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, normal)));

    glEnableVertexAttribArray(positionIndex);   
    glEnableVertexAttribArray(normalIndex);

    /////////////////////////////////////////ORN///////////////////////////////////////////////

    glGenBuffers(1, &ornVBO);
    glBindBuffer(GL_ARRAY_BUFFER, ornVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*g_numberOfVertices[1], g_pMeshVertices[1], GL_DYNAMIC_DRAW);

    glGenBuffers(1, &ornIBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ornIBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLint) * 3 * g_numberOfFaces[1], g_pMeshIndices[1], GL_DYNAMIC_DRAW);
    glGenVertexArrays(1, &ornVAO);

    glBindVertexArray(ornVAO);
    glBindBuffer(GL_ARRAY_BUFFER, ornVBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ornIBO);
    glVertexAttribPointer(positionIndex, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, position)));
    glVertexAttribPointer(normalIndex, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, normal)));

    glEnableVertexAttribArray(positionIndex);
    glEnableVertexAttribArray(normalIndex);

So it renders the sphere fine, but then all the cubes have missing triangle faces. I think I have narrowed it down to being an issue with the IBO side but I can't quite figure it out.

Here is the image: https://puu.sh/vLBSm/1617b5d996.png

Next to and behind the sphere and the cube are other cubes ignore those, they have the same issue but are purposefully there. I just can't work out how to display both properly. and Im sure it has something to do with this:

glGenBuffers(1, &ornIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ornIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLint) * 3 * g_numberOfFaces[1], g_pMeshIndices[1], GL_DYNAMIC_DRAW);
glGenVertexArrays(1, &ornVAO);
1

1 Answers

0
votes

In the ORN code, you are overwritting the index buffer binding of the first VAO.

At this point:

glGenBuffers(1, &ornIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ornIBO);

g_VAO is still active, so the index buffer binding is replaced by ornIBO. To solve that you should unbind g_VAO before starting the ORN code:

glBindVertexArray(g_VAO);
glBindBuffer(GL_ARRAY_BUFFER, g_VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_IBO);
glVertexAttribPointer(positionIndex, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, position)));
glVertexAttribPointer(normalIndex, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, normal)));

glEnableVertexAttribArray(positionIndex);   
glEnableVertexAttribArray(normalIndex);

//Unbind!!
glBindVertexArray(0);