我正在尝试使用 Assimp 导入一些动画文件并将它们添加到现有动画列表中。但是我在使用 aiAnimation->mChannels(类型 aiNodeAnim**)时遇到问题,因为当我的函数返回时,我的新 aiAnimation 对象的 mChannels 指针无效。这是因为 Assimp 的 Importer 在销毁时会自行清理。我需要的是复制通道,以便在导入器超出范围时不会删除它们。
这是我的动画附加函数,它按照我的预期进行更新,但在从函数返回时会产生运行时异常“Engine.exe 已触发断点”。
void Animation::AddAnimation(const char* p_filePath)
{
Assimp::Importer m_importer;
const aiScene* m_scene = m_importer.ReadFile(p_filePath,
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType | aiProcess_GenSmoothNormals);
for (int i = 0; i < m_scene->mNumAnimations; ++i)
{
aiAnimation* m_newAnimation = new aiAnimation(*m_scene->mAnimations[i]);
m_newAnimation->mName = m_scene->mAnimations[i]->mName;
m_newAnimation->mDuration = m_scene->mAnimations[i]->mDuration;
m_newAnimation->mTicksPerSecond = m_scene->mAnimations[i]->mTicksPerSecond;
m_newAnimation->mNumChannels = m_scene->mAnimations[i]->mNumChannels;
if (m_scene->mAnimations[i]->mChannels != NULL)
{
aiNodeAnim* m_channels = *m_scene->mAnimations[i]->mChannels;
*m_newAnimation->mChannels = new aiNodeAnim[m_scene->mAnimations[i]->mNumChannels];
for (int j = 0; j < m_scene->mAnimations[i]->mNumChannels; ++j)
{
m_newAnimation->mChannels[j] = new aiNodeAnim();
m_newAnimation->mChannels[j] = &m_channels[j];
}
}
g_channels = m_newAnimation->mChannels;
g_animations.push_back(m_newAnimation);
}
}