使用Lib3ds获取顶点数组

时间:2012-11-06 18:06:28

标签: c++

我正在尝试从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已经老了,但是格式和库的设置方式都符合我的目的,所以请不要建议切换到另一种格式。

1 个答案:

答案 0 :(得分: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;
            }
        }
    }
}