Assimp Skeletal Animation添加了每个顶点具有4个以上骨骼的骨骼数据

时间:2018-11-07 09:49:02

标签: c++ opengl modeling assimp

使用http://ogldev.atspace.co.uk/www/tutorial38/tutorial38.html教程,我可以使动画在OpenGL项目中工作。但是在使用的模型中,每个顶点似乎有4个以上的骨骼,因此此功能不断导致错误:

void SkinnedMesh::VertexBoneData::AddBoneData(GLuint BoneID, float Weight)
{
    const GLuint arraySize = sizeof(IDs) / sizeof(GLuint);

    for (GLuint i = 0 ; i < arraySize; i++) 
    {
        if (Weights[i] == 0.0) 
        {
            IDs[i]     = BoneID;
            Weights[i] = Weight;
            return;
        }        
    }

    // should never get here - more bones than we have space for
    assert(0);
}

如果我注释掉断言,代码会运行,但是我会得到不受骨骼运动影响的顶点的顶点伪像,我假设模型的每个顶点有四个以上的骨骼。有一种简单的方法可以将顶点分配给不同的骨骼吗?

调用AddBoneData的函数如下:

void SkinnedMesh::LoadBones(GLuint MeshIndex, const aiMesh* pMesh, vector<VertexBoneData>& Bones)
{
    for (GLuint i = 0 ; i < pMesh->mNumBones ; i++) 
    {                
        GLuint BoneIndex = 0;        
        string BoneName(pMesh->mBones[i]->mName.data);

        if (m_BoneMapping.find(BoneName) == m_BoneMapping.end()) 
        {
            // Allocate an index for a new bone
            BoneIndex = m_NumBones;
            m_NumBones++;            
            BoneInfo bi;            
            m_BoneInfo.push_back(bi);
            m_BoneInfo[BoneIndex].BoneOffset = AssimpToGLM(pMesh->mBones[i]->mOffsetMatrix);            
            m_BoneMapping[BoneName] = BoneIndex;
        }
        else 
        {
            BoneIndex = m_BoneMapping[BoneName];
        }                      

        for (GLuint j = 0 ; j < pMesh->mBones[i]->mNumWeights ; j++) 
        {
            GLuint VertexID = m_Entries[MeshIndex].BaseVertex + pMesh->mBones[i]->mWeights[j].mVertexId;
            float Weight  = pMesh->mBones[i]->mWeights[j].mWeight;                   
            Bones[VertexID].AddBoneData(BoneIndex, Weight);
        }
    }    
}

0 个答案:

没有答案