Assimp导入器用指针清理

时间:2014-09-21 16:16:16

标签: c++ pointers assimp

我正在尝试使用Assimp导入一些动画文件,并将它们添加到现有动画列表中。但是我遇到了aiAnimation-> mChannels(类型aiNodeAnim **)的问题,因为当我的函数返回时,我的新aiAnimation对象的mChannels指针无效。这是因为Assimp的进口商在销毁后会自行清理。我需要的是复制频道,以便在导入器超出范围时不删除它们。

这是我的动画附加功能,它按照我的预期更新,但在从函数返回时会产生运行时异常“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);
    }
}

1 个答案:

答案 0 :(得分:0)

我没有足够的声誉来评论,但您是否在询问是否需要单独复制动画频道 ?答案是肯定的,因为如果您尝试memcpy数组的m_channels,甚至每个条目的m_newAnimation->mChannels[j] = new aiNodeAnim(); m_newAnimation->mChannels[j] = &m_channels[j]; ,您都无法确定它们是否会在内存中连续存在数组。您必须手动复制每个旋转,平移和缩放键阵列。

顺便说一下,这些代码行

{{1}}

可能不按你的意愿行事。在第一行中,您可以分配空间并初始化一个新动画,在第二行中,您可以使用导入器通道的地址将其分开。删除第二行并将aiNodeAnim的内容复制到数组中将解决此问题。