记录一下assimp的简单使用,下面的ImportModel函数把相应的注释去掉可以拿到模型的mesh、vertex、face、material的属性。有关函数的API推荐去查询:
#include <iostream>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
using namespace std;
#define print(x) cout << x << endl
#define print2D(v) cout << "(" << v[0] << ", " << v[1] << ")"
#define print3D(v) cout << "(" << v[0] << ", " << v[1] << ", " << v[2] << ")"
#define printColor3D(v) cout << "(" << v.r << ", " << v.g << ", " << v.b << ")"
void ImportModel(const std::string &path) //path是路径
{
// Create an instance of the Importer class
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(path,
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType);
// If the import failed, report it
if (!scene)
{
std::cout << "import error!" << std::endl;
}
std::cout << "import success!" << std::endl;
/* mesh
// 打印mesh
int meshNum = scene->mNumMeshes;
print(meshNum);
// 打印mesh的名字
for (int i = 0; i < meshNum; ++i)
{
aiMesh* mesh = scene->mMeshes[i];
print(mesh->mName.C_Str());
}
*/
/*vertex
// 打印指定mesh的顶点数
aiMesh* mesh = scene->mMeshes[0];
int numVertex = mesh->mNumVertices;
print(numVertex);
//打印指定mesh的顶点属性
for (int i = 0; i < numVertex; ++i)
{
cout << i << ": ";
print3D(mesh->mVertices[i]); // 顶点坐标
cout << "\t";
print3D(mesh->mNormals[i]); // 法线坐标
cout << "\t";
if (mesh->HasVertexColors(0))
{
printColor3D(mesh->mColors[0][i]); // 顶点颜色
cout << "\t";
}
print2D(mesh->mTextureCoords[0][i]); // 纹理坐标
print("");
}
*/
/*faces
aiMesh* mesh = scene->mMeshes[0];
int numFaces = mesh->mNumFaces;
//打印面数
print(numFaces);
for (int i = 0; i < numFaces; ++i)
{
aiFace faces = mesh->mFaces[i];
cout << i << ": (";
for (int j = 0; j < faces.mNumIndices; ++j)
{
cout << faces.mIndices[j] << ",";// 打印顶点索引
}
print(")");
}
*/
/*material
//是否有材质
print(scene->HasMaterials());
int numMaterials = scene->mNumMaterials;
//打印材质的数量
print(numMaterials);
for (int i = 0; i < numMaterials; ++i)
{
aiMaterial* material = scene->mMaterials[i];
aiString path;
//print(material->GetName().C_Str());//材质名字
material->GetTexture(aiTextureType_DIFFUSE, 0, &path);
string pathRel = path.C_Str();
if (pathRel.find_first_of("\\") != std::string::npos)
print(pathRel.substr(pathRel.find_last_of("\\") + 1));
else
print(pathRel);
}
*/
}