如何使用 tinyobjloader 在 OpenGL 中绘制 OBJ 文件?

问题描述

我正在尝试在 OpenGL 中绘制 this free airwing model from Starfox 64。我在 Blender 中将 .fbx 文件转换为 .obj 并使用 tinyobjloader 加载它(我的大学科目的所有要求)。

我几乎将示例代码(使用现代 API)添加到我的程序中,替换了文件名,并抓取了 attrib.verticesattrib.normals 向量来绘制机翼。

我可以使用 GL_POINTS 查看顶点:

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,&vertices[0]);
glDrawArrays(GL_POINTS,vertices.size() / 3);
gldisableClientState(GL_VERTEX_ARRAY);

哪个看起来正确(我......认为?):

enter image description here

但我不确定如何渲染实体模型。简单地用 GL_TRIANGLES(显示)或 GL_QUADS 替换 GL_POINTS 不起作用:

enter image description here

我使用 OpenGL 1.1 w/ gluT(再次,大学)。我想我只是不知道我在做什么,真的。帮助?

解决方法

E:当我最初写这个答案时,我只使用了顶点和法线。我已经想出了如何让材质和纹理发挥作用,但目前没有时间写出来。当我有时间时,我会添加它,但如果您想同时浏览 tinyobj 标头,这在很大程度上是相同的逻辑。 :-)

我在最后一天学到了很多关于 TinyOBJLoader 的知识,所以我希望这对未来的人有所帮助。 Credit goes to this GitHub repositoryfileloader.cpp 中非常清楚和干净地使用 TinyOBJLoader。

总结一下我在研究该代码时学到的东西:

形状属于 shape_t 类型。对于单个模型 OBJ,shapes 的大小为 1。我假设 OBJ 文件可以包含多个对象,但我对文件格式了解不多。

shape_t 的成员 mesh 的类型为 mesh_t。该成员存储从 OBJ 的面部行解析的信息。您可以通过检查 material_ids 成员的大小来确定对象的面数。

每个面的顶点、纹理坐标和法线索引都存储在网格的 indices 成员中。这是 std::vector<index_t> 类型。这是一个扁平的索引向量。所以对于具有三角面的模型 f1,f2 ... fi,它存储v1,t1,n1,v2,t2,n2 ... vi,ti,ni。请记住,这些索引对应于整个顶点、纹理坐标或法线。我个人通过将模型导入 Blender 并在打开三角测量的情况下导出它来对我的模型进行三角测量。 TinyOBJ 有自己的三角测量算法,您可以通过设置 reader_config.triangulate 标志来打开。

到目前为止,我只处理了顶点和法线。以下是我访问和存储它们以在 OpenGL 中使用的方法:

  1. 将平面顶点和法线数组转换为 3 个一组,即 3D 向量
for (size_t vec_start = 0; vec_start < attrib.vertices.size(); vec_start += 3) {
    vertices.emplace_back(
        attrib.vertices[vec_start],attrib.vertices[vec_start + 1],attrib.vertices[vec_start + 2]);
}

for (size_t norm_start = 0; norm_start < attrib.normals.size(); norm_start += 3) {
    normals.emplace_back(
        attrib.normals[norm_start],attrib.normals[norm_start + 1],attrib.normals[norm_start + 2]);
}

这样顶点和法线容器的索引将与面条目给出的索引相对应。

  1. 循环遍历每个面,并将顶点和法线索引存储在单独的对象中
for (auto shape = shapes.begin(); shape < shapes.end(); ++shape) {
    const std::vector<tinyobj::index_t>& indices = shape->mesh.indices;
    const std::vector<int>& material_ids = shape->mesh.material_ids;

    for (size_t index = 0; index < material_ids.size(); ++index) {
        // offset by 3 because values are grouped as vertex/normal/texture
        triangles.push_back(Triangle(
            { indices[3 * index].vertex_index,indices[3 * index + 1].vertex_index,indices[3 * index + 2].vertex_index },{ indices[3 * index].normal_index,indices[3 * index + 1].normal_index,indices[3 * index + 2].normal_index })
        );
    }
}

画图很容易:

glBegin(GL_TRIANGLES);
for (auto triangle = triangles.begin(); triangle != triangles.end(); ++triangle) {
    glNormal3f(normals[triangle->normals[0]].X,normals[triangle->normals[0]].Y,normals[triangle->normals[0]].Z);
    glVertex3f(vertices[triangle->vertices[0]].X,vertices[triangle->vertices[0]].Y,vertices[triangle->vertices[0]].Z);

    glNormal3f(normals[triangle->normals[1]].X,normals[triangle->normals[1]].Y,normals[triangle->normals[1]].Z);
    glVertex3f(vertices[triangle->vertices[1]].X,vertices[triangle->vertices[1]].Y,vertices[triangle->vertices[1]].Z);

    glNormal3f(normals[triangle->normals[2]].X,normals[triangle->normals[2]].Y,normals[triangle->normals[2]].Z);
    glVertex3f(vertices[triangle->vertices[2]].X,vertices[triangle->vertices[2]].Y,vertices[triangle->vertices[2]].Z);
}
glEnd();

enter image description here