简体   繁体   English

立方体 OPENGL 上的缺失面

[英]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.索引直接来自 obj 文件,顶点也是如此。 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. 1.) 元素索引从 0 而不是 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.要么减少vertex_index数组中的所有顶点独立,要么在temp_vertices开头添加一个“虚拟”顶点坐标,以解决问题。

2.) The 2nd parameter or glDrawElements is the number of the indices rather than the size of the index buffer in bytes: 2.) 第二个参数或glDrawElements是索引的数量而不是索引缓冲区的大小(以字节为单位):

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() .但我建议将索引放在std::vector<short>并使用std::vector::size()


Further use the WIDTH and HEIGHT when setting up the projection matrix, for calculating the aspect ratio of the viewport.在设置投影矩阵时进一步使用WIDTHHEIGHT来计算视口的纵横比。

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 glPolygonModeglPolygonMode

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

To generate the following image生成以下图像

In addition to the answer of Rabbid76 :除了Rabbid76回答

I took the values provided by OP and made a sketch:我采用了 OP 提供的值并绘制了草图:

立方体顶点草图

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.如果我没有犯任何错误(我小心地记住了三角形的方向),这甚至应该与背面剔除(仅渲染 CCW 方向的三角形)一起工作以仅显示在立方体外部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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