34 const aiBone* bone =
nullptr;
37 aiMatrix4x4 meshInverseTrans = aiMatrix4x4().Inverse();
39 for (
unsigned int i = 0; i < scene->mNumMeshes; ++i)
41 const aiMesh* mesh = scene->mMeshes[i];
42 for (
unsigned int j = 0; j < mesh->mNumBones; ++j)
44 const aiBone* b = mesh->mBones[j];
45 if (strcmp(b->mName.C_Str(), node->mName.C_Str()) == 0)
48 if (meshNode !=
nullptr)
64 aiMatrix4x4 boneToMesh = meshInverseTrans * mScene->
getFullTransform(node) * bone->mOffsetMatrix;
66 return boneToMesh * meshInverseTrans.Inverse();
173 std::string name = node->mName.C_Str();
176 Ogre::Bone* ogreBone = 0;
177 if(mSkeleton->hasBone(name))
179 ogreBone = mSkeleton->getBone(name);
183 ogreBone = mSkeleton->createBone(name);
184 mBones.push_back(
ALBone(node, bone));
192 mBones[bid].mBone = bone;
195 if(mSkeleton->hasBone(name))
197 Ogre::Bone* ogreBone = mSkeleton->getBone(name);
199 for(
unsigned int n=0; n<bone->mNumWeights; ++n)
201 Ogre::VertexBoneAssignment vba;
202 aiVertexWeight aiWeight = bone->mWeights[n];
204 vba.boneIndex = ogreBone->getHandle();
205 vba.vertexIndex = aiWeight.mVertexId;
206 vba.weight = aiWeight.mWeight;
207 submesh->addBoneAssignment(vba);
215 for(
unsigned int n=0; n<mBones.size(); ++n)
217 aiNode* node = mBones[n].mNode;
218 aiBone* bone = mBones[n].mBone;
219 std::string boneName = node->mName.C_Str();
222 Ogre::Bone* ogreBone = 0;
223 if (mSkeleton->hasBone(boneName))
224 ogreBone = mSkeleton->getBone(boneName);
236 mBones[n].mBindMatrix = bindTrans;
241 aiQuaternion rotation;
243 bindTrans.Decompose(scale, rotation, pos);
244 Ogre::Bone* ogreBone = mSkeleton->getBone(boneName);
245 ogreBone->setPosition(pos.x, pos.y, pos.z);
246 ogreBone->setScale(scale.x, scale.y, scale.z);
247 ogreBone->setOrientation(rotation.w, rotation.x, rotation.y, rotation.z);
248 ogreBone->setBindingPose();
249 ogreBone->setInitialState();
253 for(
unsigned int i=0; i<node->mNumChildren; ++i)
255 aiNode* child = node->mChildren[i];
256 std::string childName = child->mName.C_Str();
259 if(mSkeleton->hasBone(childName))
263 ogreBone->addChild(mSkeleton->getBone(childName));
265 catch (Ogre::Exception &)
276 mSkeleton->getRootBones();
323 std::string animName(anim->mName.C_Str());
326 Ogre::Animation* ogreAnim = mSkeleton->createAnimation(animName,
static_cast<float>(anim->mDuration/anim->mTicksPerSecond));
327 ogreAnim->setLength((
float)(anim->mDuration/anim->mTicksPerSecond));
330 aiMatrix4x4 meshInverseTrans = meshTrans;
331 meshInverseTrans.Inverse();
333 aiNodeAnim** boneAnim = anim->mChannels;
334 for(
unsigned int i=0; i<anim->mNumChannels; ++i)
336 if(mSkeleton->hasBone(boneAnim[i]->mNodeName.C_Str()))
339 aiNode* currentBone = bone.
mNode;
340 Ogre::Bone* ogbone = mSkeleton->getBone(boneAnim[i]->mNodeName.C_Str());
342 aiVectorKey* posKey = boneAnim[i]->mPositionKeys;
343 aiQuatKey* rotKey = boneAnim[i]->mRotationKeys;
344 aiVectorKey* scaleKey = boneAnim[i]->mScalingKeys;
345 unsigned int nbKey[3] = {boneAnim[i]->mNumPositionKeys, boneAnim[i]->mNumRotationKeys, boneAnim[i]->mNumScalingKeys};
346 unsigned int keySize = getBiggerValue(nbKey, 3);
348 std::set<float> times;
349 for(
unsigned int n=0; n<keySize; ++n)
351 if(boneAnim[i]->mNumPositionKeys > n)
353 times.insert(
static_cast<float>(posKey[n].mTime));
356 if(boneAnim[i]->mNumScalingKeys > n)
358 times.insert(
static_cast<float>(scaleKey[n].mTime));
361 if(boneAnim[i]->mNumRotationKeys > n)
363 times.insert(
static_cast<float>(rotKey[n].mTime));
367 std::vector<ALKeyFrame> keyframes;
368 for(std::set<float>::iterator it=times.begin(); it!=times.end(); ++it)
372 for(
unsigned int n=0; n<keySize; ++n)
374 if(boneAnim[i]->mNumPositionKeys > n)
376 if(posKey[n].mTime == *it)
382 if(boneAnim[i]->mNumScalingKeys > n)
384 if(scaleKey[n].mTime == *it)
390 if(boneAnim[i]->mNumRotationKeys > n)
392 if(rotKey[n].mTime == *it)
399 keyframes.push_back(key);
402 Ogre::NodeAnimationTrack* nAnimTrk = ogreAnim->createNodeTrack(ogbone->getHandle(), ogbone);
403 for(
unsigned int n=0; n<keyframes.size(); ++n)
405 if (currentBone != 0)
407 keyframes[n].createInterpolation(keyframes, currentBone);
409 aiVector3D kpos = keyframes[n].getPosition().mValue;
410 aiQuaternion krotation = keyframes[n].getRotation().mValue;
411 aiVector3D kscale = keyframes[n].getScale().mValue;
414 aiQuaternion brotation;
417 aiMatrix4x4 boneMat(kscale, krotation, kpos);
419 bone.
mBindMatrix.Decompose(bscale, brotation, bpos);
420 if (currentBone->mParent && !mScene->
isBone(currentBone->mParent))
423 boneMat = meshInverseTrans * boneMat;
424 boneMat.Decompose(kscale, krotation, kpos);
428 krotation = aiQuaternion(brotation.GetMatrix().Inverse() * krotation.GetMatrix());
429 Ogre::TransformKeyFrame* key = nAnimTrk->createNodeKeyFrame((
float)(keyframes[n].getTime() / anim->mTicksPerSecond));
430 key->setTranslate(Ogre::Vector3(kpos.x, kpos.y, kpos.z));
431 key->setRotation(Ogre::Quaternion(krotation.w, krotation.x, krotation.y, krotation.z));
432 key->setScale(Ogre::Vector3(kscale.x, kscale.y, kscale.z));
438 mSkeleton->optimiseAllAnimations();