我正在努力通过带有多个网格的 assimp 加载模型,这里是加载模型的代码:
bool loadAssImp(
const char * path,
std::vector<unsigned short> & indices,
std::vector<glm::vec3> & vertices,
std::vector<glm::vec2> & uvs,
std::vector<glm::vec3> & normals
) {
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate);
if (!scene) {
cout << importer.GetErrorString() << endl;
return false;
}
aiMesh* mesh;
for (unsigned int l = 0; l < scene->mNumMeshes; l++)
{
mesh = scene->mMeshes[l];
cout << l << endl;
// Fill vertices positions
vertices.reserve(mesh->mNumVertices);
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
aiVector3D pos = mesh->mVertices[i];
vertices.push_back(glm::vec3(pos.x, pos.y, pos.z));
}
// Fill vertices texture coordinates
uvs.reserve(mesh->mNumVertices);
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
aiVector3D UVW = mesh->mTextureCoords[0][i]; // Assume only 1 set of UV coords; AssImp supports 8 UV sets.
uvs.push_back(glm::vec2(UVW.x, UVW.y));
}
// Fill vertices normals
normals.reserve(mesh->mNumVertices);
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
aiVector3D n = mesh->mNormals[i];
normals.push_back(glm::vec3(n.x, n.y, n.z));
}
// Fill face indices
indices.reserve(3 * mesh->mNumFaces);
for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
// Assume the model has only triangles.
indices.push_back(mesh->mFaces[i].mIndices[0]);
indices.push_back(mesh->mFaces[i].mIndices[1]);
indices.push_back(mesh->mFaces[i].mIndices[2]);
}
}
// The "scene" pointer will be deleted automatically by "importer"
return true;
}
我尝试加载的模型有两个网格,头部和躯干
如果我像这样开始循环:for (int l = scene->mNumMeshes - 1; l >= 0 ; l--)
然后它几乎正确地加载模型,一些模型顶点没有正确绘制,但头部和几乎所有躯干都被绘制了。
如果我像这样开始循环:for (unsigned int l = 0; l < scene->mNumMeshes; l++)
仅绘制躯干(但完整绘制,没有任何缺失的部分)
奇怪的是,这两种方式的顶点和索引计数是相同的。
最佳答案
您还需要设置 aiNode 实例描述的转换。因此,当在节点上加载场景循环时,设置本地节点变换,将指定的网格绘制到当前节点。 如果不应用这些转换信息,模型的渲染将会被破坏。
关于c++ - assimp模型加载不加载所有网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52346231/