我试图从 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 很旧,但格式和库的设置方式适合我的目的,所以请不要建议切换到另一种格式。
最佳答案
万一有人遇到这个问题,这里是从 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;
}
}
}
}
关于c++ - 使用 Lib3ds 获取顶点数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13256753/