Hello,
I have a problem.
I'm trying to write a DBO MeshLoader.
The problem is the animations.
The mesh animations without works, but unless I bind the animation to the mesh, it is misrepresented.
Here is the code:
irr::scene::IAnimatedMesh* CDBOMeshFileLoader::createMesh(irr::io::IReadFile* file)
{
irr::scene::CSkinnedMesh* animMesh = new irr::scene::CSkinnedMesh();
if( !ks::LoadDBOEx(file, &m_pObject) )
{
SAFE_DROP(animMesh);
}
else if( !CalculateFrameAndMesh(m_pObject) )
{
SAFE_DROP(animMesh);
}
else
{
// alles gut gegangen Meshdaten lesen
for( ks::uint32 x = 0;x < m_pObject->nMeshCount;x++)
{
ks::uint32 nVertexCount = m_pObject->ppMeshList[x]->dwVertexCount;
ks::uint32 nIndexCount = m_pObject->ppMeshList[x]->dwIndexCount;
irr::scene::SSkinMeshBuffer* pMeshBuffer = animMesh->addMeshBuffer();
// vertices
pMeshBuffer->Vertices_Standard.reallocate(nVertexCount);
//pMeshBuffer->Vertices_Standard.set_used(0);
if( m_pObject->ppMeshList[x]->bUsesMaterial )
{
GetColor(pMeshBuffer->Material.DiffuseColor, m_pObject->ppMeshList[x]->mMaterial.Diffuse);
GetColor(pMeshBuffer->Material.SpecularColor, m_pObject->ppMeshList[x]->mMaterial.Specular);
GetColor(pMeshBuffer->Material.EmissiveColor, m_pObject->ppMeshList[x]->mMaterial.Emissive);
pMeshBuffer->Material.Shininess=0.f;
pMeshBuffer->Material.Lighting = false;
//pMeshBuffer->Material.BackfaceCulling = false;
}
for( ks::uint32 y = 0;y < nVertexCount;y++)
{
irr::video::S3DVertex* pVertex = 0;
ks::GetMeshBuffer(m_pObject->ppMeshList[x], y, &pVertex,pMeshBuffer->Material.DiffuseColor);
pMeshBuffer->Vertices_Standard.push_back(*pVertex);
}
// indices
pMeshBuffer->Indices.reallocate(nIndexCount);
//pMeshBuffer->Indices.set_used(0);
for( ks::uint32 z = 0;z < nIndexCount;z++)
{
ks::uint32 index = m_pObject->ppMeshList[x]->pIndices[z];
pMeshBuffer->Indices.push_back(index);
}
pMeshBuffer->recalculateBoundingBox();
for( ks::uint32 nTxtCount = 0;nTxtCount < m_pObject->ppMeshList[x]->dwTextureCount;nTxtCount++)
{
if( strlen(m_pObject->ppMeshList[x]->pTextures[nTxtCount].pName) > 0 )
{
pMeshBuffer->Material.setTexture(nTxtCount, m_pSMgr->getVideoDriver()->getTexture(m_pObject->ppMeshList[x]->pTextures[nTxtCount].pName));
}
}
//pMeshBuffer->
}// end meshdata
// read animation
for( ks::uint32 nMeshCount = 0;nMeshCount < m_pObject->nMeshCount;nMeshCount++)
{
ks::sMesh* pMesh = m_pObject->ppMeshList[nMeshCount];
//irr::scene::ISkinnedMesh::SJoint* pMeshJoint = animMesh->addJoint(0);
for( ks::uint32 nBoneCount = 0; nBoneCount < pMesh->dwBoneCount; nBoneCount++)
{
irr::scene::ISkinnedMesh::SJoint* joint = animMesh->addJoint(0);//pMeshJoint);
ks::sBone* dboBone = &pMesh->pBones[nBoneCount];
for( ks::uint32 nWeightCount = 0;nWeightCount < dboBone->dwNumInfluences;nWeightCount++)
{
/*irr::scene::ISkinnedMesh::SWeight* weight = animMesh->addWeight(joint);
weight->vertex_id = dboBone->pVertices[nWeightCount];
weight->buffer_id = nMeshCount;
weight->strength = dboBone->pWeights[nWeightCount];*/
irr::scene::ISkinnedMesh::SWeight weight;
weight.vertex_id = dboBone->pVertices[nWeightCount];
weight.buffer_id = nMeshCount;
weight.strength = dboBone->pWeights[nWeightCount];
joint->Weights.push_back(weight);
}
ks::sAnimation* anim = ks::FindAnimation(pMesh->pFrameRef[nBoneCount], m_pObject->pAnimationSet);
if( anim )
{
ReadAnimation(animMesh, joint, anim);
}
}
}
//animMesh->updateBoundingBox();
animMesh->finalize();
}
SAFE_DELETE(m_pObject);
return animMesh;
}
Here is a method to read animation
bool CDBOMeshFileLoader::ReadAnimation(irr::scene::ISkinnedMesh* pMesh, irr::scene::ISkinnedMesh::SJoint* pJoint, ks::sAnimation* pAnim)
{
SAFE_MEMORY(pJoint);
SAFE_MEMORY(pAnim);
SAFE_MEMORY(pMesh);
for( ks::uint32 nPos = 0;nPos < pAnim->dwNumPositionKeys;++nPos)
{
irr::scene::ISkinnedMesh::SPositionKey* pPosKey = pMesh->addPositionKey(pJoint);
pPosKey->frame = (float)pAnim->pPositionKeys[nPos].dwTime;
pPosKey->position = irr::core::vector3df(
pAnim->pPositionKeys[nPos].vecPos.x,
pAnim->pPositionKeys[nPos].vecPos.y,
pAnim->pPositionKeys[nPos].vecPos.z);
}
for( ks::uint32 nRot = 0;nRot < pAnim->dwNumRotateKeys;nRot++)
{
irr::scene::ISkinnedMesh::SRotationKey* pRotKey = pMesh->addRotationKey(pJoint);
pRotKey->frame = (float)pAnim->pRotateKeys[nRot].dwTime;
pRotKey->rotation = irr::core::quaternion(
pAnim->pRotateKeys[nRot].Quaternion.x,
pAnim->pRotateKeys[nRot].Quaternion.y,
pAnim->pRotateKeys[nRot].Quaternion.z,
pAnim->pRotateKeys[nRot].Quaternion.w);
}
for( ks::uint32 nScale = 0;nScale < pAnim->dwNumScaleKeys;nScale++)
{
irr::scene::ISkinnedMesh::SScaleKey* pScaleKey = pMesh->addScaleKey(pJoint);
pScaleKey->frame = (float)pAnim->pScaleKeys[nScale].dwTime;
pScaleKey->scale = irr::core::vector3df(
pAnim->pScaleKeys[nScale].vecScale.x,
pAnim->pScaleKeys[nScale].vecScale.y,
pAnim->pScaleKeys[nScale].vecScale.z);
}
// this not works
for( ks::uint32 nMatrix = 0;nMatrix < pAnim->dwNumMatrixKeys;nMatrix++)
{
// // read matrix
irr::core::matrix4 mat(irr::core::matrix4::EM4CONST_NOTHING);
if( pAnim->pFrame )
{
GetMatrix(mat, pAnim->pMatrixKeys[nMatrix].matMatrix);//*pAnim->pFrame->matCombined);//*m_pObject->ppMeshList[x]->pBones->matTranslation);
}else
{
GetMatrix(mat, pAnim->pMatrixKeys[nMatrix].matMatrix);
}
irr::scene::ISkinnedMesh::SRotationKey keyR;
keyR.frame= (float)pAnim->pMatrixKeys[nMatrix].dwTime;
keyR.rotation = irr::core::quaternion(mat.getTransposed());
irr::scene::ISkinnedMesh::SPositionKey keyP;
keyP.frame = (float)pAnim->pMatrixKeys[nMatrix].dwTime+1;
keyP.position = mat.getTranslation();
pJoint->PositionKeys.push_back(keyP);
pJoint->RotationKeys.push_back(keyR);
if( nMatrix > 4 )
{
irr::core::stringc s = pJoint->Name;
}
//irr::scene::ISkinnedMesh::SScaleKey *keyS=animMesh->addScaleKey(joint);
//keyS->frame=(float)anim->pMatrixKeys[nMatrix].dwTime;
//keyS->scale=mat.getScale();
}
return true;
}
Your can see on the picture a mesh whitout animation and animated
The Picture is attached on this thread.