简体   繁体   English

两种不同的OpenGL对象。 VAO VBO IBO网格变形问题

[英]Two Different Objects OpenGL. VAO VBO IBO Issue with mesh distortion

I am trying to create two separate objects to be rendered at the same time, a cube and a sphere. 我正在尝试创建两个要同时渲染的单独对象,一个立方体和一个球体。 The issue is once I add the code for the sphere, the cube is missing some faces etc. 问题是,一旦我添加了球体的代码,立方体就缺少了一些面等。

Here is my code: 这是我的代码:

    glGenBuffers(1, &g_VBO);
    glBindBuffer(GL_ARRAY_BUFFER, g_VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*g_numberOfVertices[0], g_pMeshVertices[0], GL_DYNAMIC_DRAW);

    glGenBuffers(1, &g_IBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_IBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLint) * 3 * g_numberOfFaces[0], g_pMeshIndices[0], GL_DYNAMIC_DRAW);
    glGenVertexArrays(1, &g_VAO);

    glBindVertexArray(g_VAO);
    glBindBuffer(GL_ARRAY_BUFFER, g_VBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_IBO);
    glVertexAttribPointer(positionIndex, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, position)));
    glVertexAttribPointer(normalIndex, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, normal)));

    glEnableVertexAttribArray(positionIndex);   
    glEnableVertexAttribArray(normalIndex);

    /////////////////////////////////////////ORN///////////////////////////////////////////////

    glGenBuffers(1, &ornVBO);
    glBindBuffer(GL_ARRAY_BUFFER, ornVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*g_numberOfVertices[1], g_pMeshVertices[1], GL_DYNAMIC_DRAW);

    glGenBuffers(1, &ornIBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ornIBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLint) * 3 * g_numberOfFaces[1], g_pMeshIndices[1], GL_DYNAMIC_DRAW);
    glGenVertexArrays(1, &ornVAO);

    glBindVertexArray(ornVAO);
    glBindBuffer(GL_ARRAY_BUFFER, ornVBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ornIBO);
    glVertexAttribPointer(positionIndex, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, position)));
    glVertexAttribPointer(normalIndex, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, normal)));

    glEnableVertexAttribArray(positionIndex);
    glEnableVertexAttribArray(normalIndex);

So it renders the sphere fine, but then all the cubes have missing triangle faces. 因此,它使球体很好,但随后所有多维数据集都缺少三角形面。 I think I have narrowed it down to being an issue with the IBO side but I can't quite figure it out. 我认为我已将其范围缩小为IBO方面的问题,但我不太清楚。

Here is the image: https://puu.sh/vLBSm/1617b5d996.png 这是图像: https : //puu.sh/vLBSm/1617b5d996.png

Next to and behind the sphere and the cube are other cubes ignore those, they have the same issue but are purposefully there. 在球体和多维数据集的旁边和后面是其他多维数据集,它们忽略了它们,它们具有相同的问题,但故意存在于此。 I just can't work out how to display both properly. 我只是想不出如何正确显示两者。 and Im sure it has something to do with this: 我确定这与此有关:

glGenBuffers(1, &ornIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ornIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLint) * 3 * g_numberOfFaces[1], g_pMeshIndices[1], GL_DYNAMIC_DRAW);
glGenVertexArrays(1, &ornVAO);

In the ORN code, you are overwritting the index buffer binding of the first VAO. 在ORN代码中,您要覆盖第一个VAO的索引缓冲区绑定。

At this point: 这一点:

glGenBuffers(1, &ornIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ornIBO);

g_VAO is still active, so the index buffer binding is replaced by ornIBO . g_VAO仍处于活动状态,因此索引缓冲区绑定被ornIBO代替。 To solve that you should unbind g_VAO before starting the ORN code: 要解决此问题,您应该在启动ORN代码之前先解除g_VAO绑定:

glBindVertexArray(g_VAO);
glBindBuffer(GL_ARRAY_BUFFER, g_VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_IBO);
glVertexAttribPointer(positionIndex, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, position)));
glVertexAttribPointer(normalIndex, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, normal)));

glEnableVertexAttribArray(positionIndex);   
glEnableVertexAttribArray(normalIndex);

//Unbind!!
glBindVertexArray(0);

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

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