简体   繁体   中英

Missing faces on cube OPENGL

如您所见,立方体缺少面

i tried everything, i know that my program is drawing the correct vertices, i basically pointed it down to indices, here is the mainloop

void mainLoop() {

    while (!glfwWindowShouldClose(window)) {

        glfwSwapBuffers(window);
        glEnable(GL_DEPTH_TEST);
        glClearColor(0.1, 0.1, 0.3, 1.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glfwPollEvents();
        glFrontFace(GL_CW);
        glDisable(GL_CULL_FACE);

        glfwGetFramebufferSize(window, &WIDTH, &HEIGHT);
        glViewport(1, 1, WIDTH, HEIGHT);

        glm::mat4 view = glm::lookAt(
            glm::vec3(1.2f, 1.2f, 1.2f),
            glm::vec3(0.0f, 0.0f, 0.0f),
            glm::vec3(0.0f, 0.0f, 1.0f)
        );


        glm::mat4 projectionmatrix = glm::perspective(glm::radians(45.0f), 800.0f / 800.0f, 1.0f, 10.0f);

        glm::mat4 identitymatrix = glm::mat4(1.0f);

        glm::mat4 modelmatrix = identitymatrix;

        MVPmatrix = projectionmatrix * view * modelmatrix;

        GLint loc = glGetUniformLocation(SHADERPROGRAM, "MVPmatrix");
        glUniformMatrix4fv(loc, 1, GL_FALSE, &MVPmatrix[0][0]);

        short vertex_index[] = { 2U, 4U, 1U, 8U, 6U, 5U, 5U, 2U, 1U, 6U, 3U, 2U, 3U, 8U, 4U, 1U, 8U, 5U, 2U, 3U, 4U, 8U, 7U, 6U, 5U, 6U, 2U, 6U, 7U, 3U, 3U, 7U, 8U, 1U, 4U, 8U };


        glEnableVertexAttribArray(0);

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



        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertex_index) * sizeof(unsigned short), &vertex_index[0], GL_STATIC_DRAW);


        glBufferData(GL_ARRAY_BUFFER, temp_vertices.size() * sizeof(glm::vec3), &temp_vertices[0], GL_STATIC_DRAW); 

        glUseProgram(SHADERPROGRAM);
        glDrawElements(GL_TRIANGLES, sizeof(vertex_index) ,  GL_UNSIGNED_SHORT, (void*)0);


    }

}

i really dont know at this point and i've been trying for ages. The indices are straight from a obj file so are the vertices. Also i tried turning off culling without success, also i tried adding manual indices amounts and even tried to offset them.

importing the verts for a cube works perfectly fine, but as soon as i try to make a indexed cube oops.

I have found 2 issues in your code:

1.) Element indices start at 0 rather than 1.

Either decrement all the the vertex indies in the vertex_index array or add a "dummy" vertex coordinate at begin of temp_vertices , to solve the issue.

2.) The 2nd parameter or glDrawElements is the number of the indices rather than the size of the index buffer in bytes:

glDrawElements(GL_TRIANGLES, sizeof(vertex_index), GL_UNSIGNED_SHORT, (void*)0);
glDrawElements(GL_TRIANGLES, sizeof(vertex_index)/sizeof(short), GL_UNSIGNED_SHORT, (void*)0);

But I recommend to put the indices in std::vector<short> and to use std::vector::size() .


Further use the WIDTH and HEIGHT when setting up the projection matrix, for calculating the aspect ratio of the viewport.

Except of that your code works fine. I used the following projection and view matrix

glm::mat4 view = glm::lookAt(
    glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 projectionmatrix = glm::perspective(
    glm::radians(90.0f), (float)WIDTH / (float)HEIGHT, 0.1f, 10.0f);

And glPolygonMode

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

To generate the following image

In addition to the answer of Rabbid76 :

I took the values provided by OP and made a sketch:

立方体顶点草图

Then I created the vertex indices for it and got:

GLushort vertex_index[] = {
  0, 1, 2, 2, 3, 0, // front
  0, 4, 5, 5, 1, 0, // right
  4, 7, 6, 6, 5, 4, // back
  7, 3, 2, 2, 6, 7, // left
  0, 3, 7, 7, 4, 0, // bottom
  1, 5, 6, 6, 2, 1 // top
};

If I didn't make any mistake (I carefully kept orientation of triangles in mind), this should even work with back face culling (rendering only CCW oriented triangles) to show outside of cube only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM