我使用Assimp做骨架动画。因此,我总是需要它加载的数据。但是由于某种原因,该数据变得不可访问并导致分段错误。下面是问题代码:
static Mesh processMesh(const aiMesh *aimesh, const aiScene *scene) {
Mesh mesh;
mesh.ai_scene = scene;
std::cout << mesh.ai_scene->mRootNode->mName.data; // OK
...
}
但是,如果在网格物体内部进行操作,则会导致错误。例如:
void boneTransform(float TimeInSeconds) {
glm::mat4 identity = glm::mat4();
std::cout << ai_scene->mRootNode->mName.data; // SEG FAULT
...
}
编辑:
ai_scene
在Mesh结构中:private:
const aiScene *ai_scene = nullptr;
场景本身是在“模型”结构中创建的,如下所示:
void init(const std::string &path, const GLuint params) {
// Create an instance of the Importer class
Assimp::Importer importer;
const aiScene *scene = importer.ReadFile(path, params);
// If the import failed, report it
if (!scene) {
std::cout << importer.GetErrorString() << "\n";
return;
}
processNode(scene->mRootNode, scene);
}
processNode
代码:void processNode(const aiNode *node, const aiScene *scene) {
for (size_t i = 0; i < node->mNumMeshes; i++) {
aiMesh *mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(Mesh::processMesh(mesh, scene));
}
for(size_t i = 0; i < node->mNumChildren; i++) {
processNode(node->mChildren[i], scene);
}
}
这是什么问题以及如何解决呢?
最佳答案
通过扩展Assimp::Importer importer
的范围解决了问题:
// Create an instance of the Importer class
Assimp::Importer importer;
void init(const std::string &path, const GLuint params) {
const aiScene *scene = importer.ReadFile(path, params);
...
}
关于c++ - 复制指针到aiScene,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55405904/