使用此网格函数的多维数据集正确的顶点和索引是什么?

问题描述 投票:0回答:1

我正在尝试使用顶点和索引创建一个多维数据集。我通过将顶点绘制到图形上找到了顶点,并对索引进行了精心设计,并计算出它们在立方体的每一侧都形成两个三角形。但是,当我打开程序时,没有显示多维数据集,为什么?我的顶点/索引不正确吗?我在createMesh函数中输入了错误的顶点/索引数量吗?还是我做错了这一切?

具有索引和顶点的函数。

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);
    }
}

创建网格函数和绘制函数

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);
}

c++ opengl cube indices vertices
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.