Search code examples
c++openglcubeindicesvertices

What are the correct vertices and indices for a cube using this mesh function?


I'm trying to create a cube using vertices and indices. The vertices I found by plotting it onto a graph, and the indices were carefully patterned and calculated to make two triangles per side of a cube. However, when I turned on the program, a cube did not show, why is this? Are my vertices/indices incorrect? Am I putting in the wrong number of vertices/indices in the createMesh function? Or am I doing this all incorrectly?

Function that has indices and vertices.

void createObjects() {
    std::vector<GLuint> indices {
        //Top
        0, 1, 2,
        2, 3, 1,

        //Bottom
        4, 5, 6,
        6, 7, 5,

        //Front
        8, 9, 10,
        10, 11, 9,

        //Back
        12, 13, 14,
        14, 15, 13,

        //Left
        16, 17, 18,
        18, 19, 17,

        //Right
        20, 21, 22, 
        22, 23, 21

        /*0, 3, 1,
        1, 3, 2,
        2, 3, 0,
        0, 1, 2*/
    };


    std::vector<GLfloat> vertices {
        //Top
        -1, 1, -1, //0
         1, 1, -1  //1
        -1, 1,  1, //2
         1, 1,  1, //3

        //Bottom
        -1, -1, -1, //4
         1, -1, -1, //5
        -1, -1,  1, //6
         1, -1,  1  //7

        //Front
        -1,  1, 1, //8
         1,  1, 1, //9
        -1, -1, 1, //10
         1, -1, 1, //11

        //Back
        -1,  1, -1, //12
         1,  1, -1, //13
        -1, -1, -1, //14
         1, -1, -1, //15

        //Left
        -1,  1,  1, //16
        -1,  1, -1, //17
        -1, -1,  1, //18
        -1, -1, -1, //19

        //Right
        1,  1,  1, //20
        1,  1, -1, //21
        1, -1,  1, //22
        1, -1, -1  //23



        /*-1, -1, 0,
         0, -1, 1,
         1, -1, 0,
         0,  1, 0*/
    };


    for (std::size_t x = 0; x < 2; x++) {
        meshVector.emplace_back(new Mesh());
        meshVector[x]->createMesh(vertices, indices, 24, 12);
    }
}

Create mesh function and draw function

void Mesh::createMesh(const std::vector<GLfloat>& vertices, const std::vector<GLuint>& indices, GLuint numOfVertices, GLuint numOfIndices) {
    indexCount = numOfIndices;

    //Binding
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);

    //Information
    //VBO Information
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(*vertices.data()) * numOfVertices, vertices.data(), GL_STATIC_DRAW);

    //IBO Information
    glGenBuffers(1, &IBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(*indices.data()) * numOfIndices, indices.data(), GL_STATIC_DRAW);


    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(0);

    //Unbinding
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

}

void Mesh::renderMesh() {
    //Binding
    glBindVertexArray(VAO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);

    //Rendering
    glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);

    //Unbinding
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
}


Solution

  • Removed GLuint numOfVertices/Indices and replaced sizeof(*vertices.data()) * numOfVertices with sizeof(GLuint) * indices.size()`. Finally, replaced the cube vertices and indices with new coordinates

    std::vector<GLuint> indices {
            //Top
            2, 6, 7,
            2, 3, 7,
    
            //Bottom
            0, 4, 5,
            0, 1, 5,
    
            //Left
            0, 2, 6,
            0, 4, 6,
    
            //Right
            1, 3, 7,
            1, 5, 7,
    
            //Front
            0, 2, 3,
            0, 1, 3,
    
            //Back
            4, 6, 7,
            4, 5, 7
        };
    
    
        std::vector<GLfloat> vertices {
            -1, -1,  0.5, //0
             1, -1,  0.5, //1
            -1,  1,  0.5, //2
             1,  1,  0.5, //3
            -1, -1, -0.5, //4
             1, -1, -0.5, //5
            -1,  1, -0.5, //6
             1,  1, -0.5  //7
        };
    

    That's it.