1

我正在尝试从 lib3ds 解析的文件中获取顶点,但遇到了很多问题。即我没有得到我出去的顶点。我尝试这样做的代码是:

//loop through meshes
for(int i = 0; i < model->meshes_size; i++)
{
    Lib3dsMesh* mesh = model->meshes[i];
    //loop through the faces in that mesh
    for(int j = 0; j < model->meshes[i]->nfaces; j++)
    {
        int testv = mesh->nvertices;
        Lib3dsFace face = mesh->faces[i];
        //loop through the vertices in each face
        for(int k = 0; k < 3; k++)
        {
            myVertices[index] = model->meshes[i]->faces[j].index[0];
            myVertices[index + 1] = model->meshes[i]->faces[j].index[1];
            myVertices[index + 2] = model->meshes[i]->faces[j].index[2];

            index += 3;
        }
    }
}

不幸的是,lib3ds 的文档不存在,所以我无法弄清楚。你如何使用这个库获取顶点数组?另外我知道 3ds 很旧,但库的格式和设置方式适合我的目的,所以请不要建议切换到另一种格式。

4

1 回答 1

2

如果有人遇到这个问题,这里是从 lib3ds 获取顶点然后将它们加载到单个顶点数组中的代码。该数组仅包含 x、y、z、x2、y2、z2 等形式的数据。

void Renderer3ds::loadVertices(string fileName)
{
    Lib3dsFile* model = lib3ds_file_open(fileName.c_str());

    if(!model)
    {
        throw strcat("Unable to load ", fileName.c_str());
    }

    int faces = getNumFaces(model);
    myNumVertices = faces * 3;
    myVertices = new double[myNumVertices * 3];

    int index = 0;

    //loop through meshes
    for(int i = 0; i < model->meshes_size; i++)
    {
        Lib3dsMesh* mesh = model->meshes[i];
        //loop through the faces in that mesh
        for(int j = 0; j < mesh->nfaces; j++)
        {
            Lib3dsFace face = mesh->faces[j];
            //loop through the vertices in each face
            for(int k = 0; k < 3; k++)
            {
                myVertices[index] = mesh->vertices[face.index[k]][0];
                myVertices[index + 1] = mesh->vertices[face.index[k]][1];
                myVertices[index + 2] = mesh->vertices[face.index[k]][2];

                index += 3;
            }
        }
    }
}
于 2012-11-06T20:15:47.820 回答