opengl 使用 EBO 绘制 3d 立方体

opengl drawing a 3d cube with with EBO

我正在尝试使用 EBO、VAO 和 VBO 使用 OPENGL 绘制立方体。

第一个函数初始化立方体的VAO

initVAO()
{
    GLfloat cube_vertices[] = {
        // front
        -1.0, -1.0,  1.0,
         1.0, -1.0,  1.0,
         1.0,  1.0,  1.0,
        -1.0,  1.0,  1.0,
        // back
        -1.0, -1.0, -1.0,
         1.0, -1.0, -1.0,
         1.0,  1.0, -1.0,
        -1.0,  1.0, -1.0
    };
    GLushort cube_elements[] = {
        // front
        0, 1, 2,
        2, 3, 0,
        // right
        1, 5, 6,
        6, 2, 1,
        // back
        7, 6, 5,
        5, 4, 7,
        // left
        4, 0, 3,
        3, 7, 4,
        // bottom
        4, 5, 1,
        1, 0, 4,
        // top
        3, 2, 6,
        6, 7, 3
    };

    vertices.insert(vertices.begin(), std::begin(cube_vertices), std::end(cube_vertices));
    indices.insert(indices.begin(), std::begin(cube_elements), std::end(cube_elements));

    /* create vao */
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);

    /* create ebo */
    glGenBuffers(1, &EBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_elements), cube_elements, GL_STATIC_DRAW);

    /* create vbo */
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW);

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

    /* unbind buffers */
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    glBindVertexArray(0);
    
}

第二个函数用它来绘制

draw()
{

    shader->activateShader();

    /* calculate projection matrix */
    //TODO:: find a new place for the projection matrix calculation
    int width = (GLfloat)ResourceManager::getInstance()->getWindowSize().x;
    int height = (GLfloat)ResourceManager::getInstance()->getWindowSize().y;
    glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)width / height, 0.1f, 100.0f);

    /* update shader uniforms*/
    glUniformMatrix4fv(shader->projectionUniform, 1, GL_FALSE, glm::value_ptr(proj));
    glUniformMatrix4fv(shader->viewUniform, 1, GL_FALSE, glm::value_ptr(Engine::getInstance()->camera->createViewMatrix()));
    glUniformMatrix4fv(shader->modelUniform, 1, GL_FALSE, glm::value_ptr(transform->createModleMatrix()));

    glUniform4f(shader->colorUniform, color.x, color.y, color.z, 1);

    glBindVertexArray(VAO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);

    glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
    glUseProgram(0);

}

现在程序没有绘制任何东西,我不知道它出了什么问题

我知道代码的其他部分可以工作,因为我在没有 EBO 的情况下对其进行了测试并且它可以工作,但我不知道该怎么做。

参见Index buffers。索引缓冲区绑定在顶点数组对象中说明。当缓冲区绑定到目标 ELEMENT_ARRAY_BUFFER 时,此缓冲区将关联到当前绑定的顶点数组对象。当调用 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 时,元素缓冲区到当前绑定的 VAO 的绑定被打破。删除这行代码:

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);


glDrawElements 中的类型说明必须与索引的类型匹配。由于索引的类型是GLushort,指定的类型必须是GL_UNSIGNED_SHORT:

glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);

glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT, 0);