目前的问题是我的3D对象只是部分渲染。如果我有一个典型的圆柱体,两端都是“填充的”,则两端完美呈现,而圆柱体本身不是。我有一个例子来说明。3D对象的部分渲染
这是代码是如何构建的:
struct ObjMeshVertex{
Vector3f pos;
Vector2f texcoord;
Vector3f normal;
};
struct ObjMeshFace{
ObjMeshVertex vertices[3];
};
struct ObjMesh{
std::vector<ObjMeshFace> faces;
};
ObjMesh myMesh;
for(size_t i = 0; i < faces.size(); ++i){
ObjMeshFace face;
for(size_t j = 0; j < 3; ++j){
face.vertices[j].pos = positions[faces[i].pos_index[j] - 1];
face.vertices[j].texcoord = texcoords[faces[i].tex_index[j] - 1];
face.vertices[j].normal = normals[faces[i].nor_index[j] - 1];
}
myMesh.faces.push_back(face);
}
设置:
glGenVertexArraysOES(1, &_boxVAO);
glBindVertexArrayOES(_boxVAO);
int sizeOfFaces = myMesh.faces.size() * sizeof(ObjMeshFace);
glGenBuffers(1, &_boxBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _boxBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeOfFaces, &(myMesh.faces[0]), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(ObjMeshVertex), 0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(ObjMeshVertex), (void*)offsetof(ObjMeshVertex, texcoord));
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE ,sizeof(ObjMeshVertex), (void*)offsetof(ObjMeshVertex, normal));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glBindVertexArrayOES(0);
抽奖:
glBindVertexArrayOES(_boxVAO);
glDrawArrays(GL_TRIANGLES, 0, indicesize);
其中indicesize = myMesh.faces.size()
所以我的对象看起来像这样:
渲染结束这样的:
而且我只是在渲染1个对象,而不是多个,这似乎对这种行为有点古怪。
球并排侧:
最新渲染:
它看起来像一个完整的子网缺失,这可能是导入器/解析器或obj导出器的错误。你能用更简单的模型(立方体?球体?)重现这一点吗?也许尝试在一个独立的OBJ查看器中验证模型(Assimp有一个独立的我相信)。 – Tim
我从来没有成功地使用Assimp,但我只是试图渲染一个失败的球体。虽然如果我从我的分析器中的打印输出手动初始化我的索引和顶点,它呈现很好。我怀疑我的代码的一部分可能会出现问题,而我无法真正弄清楚。图像被附上。 – JavaCake
你确定这个obj作家没有发出四边形吗?每个obj规范允许有超过3个顶点的面。你需要对它们进行三角测量。 – Tim