AnimationFile.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "../../common/Logger.h"
  2. #include "AnimationFile.h"
  3. AnimationFile::AnimationFile(const Ogre::String& file):
  4. File(file)
  5. {
  6. }
  7. AnimationFile::AnimationFile(File* pFile):
  8. File(pFile)
  9. {
  10. }
  11. AnimationFile::AnimationFile(File* pFile, const u32 offset, const u32 length):
  12. File(pFile, offset, length)
  13. {
  14. }
  15. AnimationFile::AnimationFile(u8* pBuffer, const u32 offset, const u32 length):
  16. File(pBuffer, offset, length)
  17. {
  18. }
  19. AnimationFile::~AnimationFile()
  20. {
  21. }
  22. void
  23. AnimationFile::GetData( std::vector< s16 >& skeleton_length, const Unit& unit, const int offset_to_animations, const int number_of_animation, const int start_animation, Ogre::SkeletonPtr skeleton)
  24. {
  25. for (int i = 0; i < number_of_animation; ++i)
  26. {
  27. /*LOGGER->Log(LOGGER_INFO, "Animation Header %02x%02x %02x %02x %02x %02x %02x%02x %02x%02x %02x%02x %02x%02x%02x%02x",
  28. GetU8(offset_to_animations + i * 0x10 + 0x00), GetU8(offset_to_animations + i * 0x10 + 0x01), GetU8(offset_to_animations + i * 0x10 + 0x02), GetU8(offset_to_animations + i * 0x10 + 0x03),
  29. GetU8(offset_to_animations + i * 0x10 + 0x04), GetU8(offset_to_animations + i * 0x10 + 0x05), GetU8(offset_to_animations + i * 0x10 + 0x06), GetU8(offset_to_animations + i * 0x10 + 0x07),
  30. GetU8(offset_to_animations + i * 0x10 + 0x08), GetU8(offset_to_animations + i * 0x10 + 0x09), GetU8(offset_to_animations + i * 0x10 + 0x0A), GetU8(offset_to_animations + i * 0x10 + 0x0B),
  31. GetU8(offset_to_animations + i * 0x10 + 0x0C), GetU8(offset_to_animations + i * 0x10 + 0x0D), GetU8(offset_to_animations + i * 0x10 + 0x0E), GetU8(offset_to_animations + i * 0x10 + 0x0F));
  32. */
  33. AnimationHeader header;
  34. header.number_of_frames = GetU16LE(offset_to_animations + i * 0x10 + 0x00);
  35. header.number_of_bones = GetU8(offset_to_animations + i * 0x10 + 0x02);
  36. header.number_of_frames_translation = GetU8(offset_to_animations + i * 0x10 + 0x03);
  37. header.number_of_static_translation = GetU8(offset_to_animations + i * 0x10 + 0x04);
  38. header.number_of_frames_rotation = GetU8(offset_to_animations + i * 0x10 + 0x05);
  39. header.offset_to_frames_translation_data = GetU16LE(offset_to_animations + i * 0x10 + 0x06);
  40. header.offset_to_static_translation_data = GetU16LE(offset_to_animations + i * 0x10 + 0x08);
  41. header.offset_to_frames_rotation_data = GetU16LE(offset_to_animations + i * 0x10 + 0x0A);
  42. header.offset_to_animation_data = GetU32LE(offset_to_animations + i * 0x10 + 0x0C) - 0x80000000;
  43. m_AnimationHeaders.push_back(header);
  44. }
  45. for (size_t i = 0; (i < static_cast<size_t>(number_of_animation)) && (start_animation + i < unit.animations.size()); ++i)
  46. {
  47. if (unit.animations[start_animation + i] == "" || unit.animations[start_animation + i] == " ")
  48. {
  49. continue;
  50. }
  51. /*
  52. File file(mpBuffer, m_AnimationHeaders[i].offset_to_animation_data, 0x04 + m_AnimationHeaders[i].number_of_bones * 0x08 + m_AnimationHeaders[i].number_of_frames_translation * m_AnimationHeaders[i].number_of_frames * 0x02 + m_AnimationHeaders[i].number_of_static_translation * 0x02 + m_AnimationHeaders[i].number_of_frames_rotation * m_AnimationHeaders[i].number_of_frames);
  53. file.WriteFile(RString((Ogre::String("dump/") + Ogre::String("animation_") + Ogre::StringConverter::toString(i) + Ogre::String("_data")).c_str()));
  54. */
  55. Ogre::Animation* anim = skeleton->createAnimation(unit.animations[start_animation + i], (float)(m_AnimationHeaders[i].number_of_frames - 1) / 30.0f);
  56. for (u32 j = 0; j < m_AnimationHeaders[i].number_of_frames; ++j)
  57. {
  58. Frame frame;
  59. // root bone
  60. Ogre::Bone* root = skeleton->getBone(0);
  61. Ogre::NodeAnimationTrack* track;
  62. if (j == 0)
  63. {
  64. track = anim->createNodeTrack(0, root);
  65. track->removeAllKeyFrames();
  66. }
  67. else
  68. {
  69. track = anim->getNodeTrack(0);
  70. }
  71. Ogre::TransformKeyFrame* frame_root = track->createNodeKeyFrame((float)j / 30.0f);
  72. Ogre::Quaternion rot;
  73. Ogre::Matrix3 mat;
  74. mat.FromEulerAnglesZXY(Ogre::Radian(Ogre::Degree(180)), Ogre::Radian(Ogre::Degree(0)), Ogre::Radian(Ogre::Degree(0)));
  75. rot.FromRotationMatrix(mat);
  76. frame_root->setRotation(rot);
  77. for (u32 k = 0; k < m_AnimationHeaders[i].number_of_bones; ++k)
  78. {
  79. BonePosition position;
  80. u8 flag = GetU8(m_AnimationHeaders[i].offset_to_animation_data + 0x04 + k * 0x08 + 0x00);
  81. u8 rx = GetU8(m_AnimationHeaders[i].offset_to_animation_data + 0x04 + k * 0x08 + 0x01);
  82. u8 ry = GetU8(m_AnimationHeaders[i].offset_to_animation_data + 0x04 + k * 0x08 + 0x02);
  83. u8 rz = GetU8(m_AnimationHeaders[i].offset_to_animation_data + 0x04 + k * 0x08 + 0x03);
  84. u8 tx = GetU8(m_AnimationHeaders[i].offset_to_animation_data + 0x04 + k * 0x08 + 0x04);
  85. u8 ty = GetU8(m_AnimationHeaders[i].offset_to_animation_data + 0x04 + k * 0x08 + 0x05);
  86. u8 tz = GetU8(m_AnimationHeaders[i].offset_to_animation_data + 0x04 + k * 0x08 + 0x06);
  87. // rotation
  88. if (flag & 0x01)
  89. {
  90. position.rotation_x = 360.0f * GetU8(m_AnimationHeaders[i].offset_to_animation_data + m_AnimationHeaders[i].offset_to_frames_rotation_data + rx * m_AnimationHeaders[i].number_of_frames + j) / 255.0f;
  91. }
  92. else
  93. {
  94. position.rotation_x = 360.0f * rx / 255.0f;
  95. }
  96. if (flag & 0x02)
  97. {
  98. position.rotation_y = 360.0f * GetU8(m_AnimationHeaders[i].offset_to_animation_data + m_AnimationHeaders[i].offset_to_frames_rotation_data + ry * m_AnimationHeaders[i].number_of_frames + j) / 255.0f;
  99. }
  100. else
  101. {
  102. position.rotation_y = 360.0f * ry / 255.0f;
  103. }
  104. if (flag & 0x04)
  105. {
  106. position.rotation_z = 360.0f * GetU8(m_AnimationHeaders[i].offset_to_animation_data + m_AnimationHeaders[i].offset_to_frames_rotation_data + rz * m_AnimationHeaders[i].number_of_frames + j) / 255.0f;
  107. }
  108. else
  109. {
  110. position.rotation_z = 360.0f * rz / 255.0f;
  111. }
  112. // translation
  113. position.translation_x = 0;
  114. position.translation_y = 0;
  115. position.translation_z = 0;
  116. if (flag & 0x10)
  117. {
  118. position.translation_x = static_cast<float>( -(s16)GetU16LE(m_AnimationHeaders[i].offset_to_animation_data + m_AnimationHeaders[i].offset_to_frames_translation_data + tx * m_AnimationHeaders[i].number_of_frames * 2 + j * 2));
  119. }
  120. else if (tx != 0xFF)
  121. {
  122. position.translation_x = static_cast<float>(-(s16)GetU16LE(m_AnimationHeaders[i].offset_to_animation_data + m_AnimationHeaders[i].offset_to_static_translation_data + tx * 2));
  123. }
  124. if (flag & 0x20)
  125. {
  126. position.translation_y = static_cast<float>(-(s16)GetU16LE(m_AnimationHeaders[i].offset_to_animation_data + m_AnimationHeaders[i].offset_to_frames_translation_data + ty * m_AnimationHeaders[i].number_of_frames * 2 + j * 2));
  127. }
  128. else if (ty != 0xFF)
  129. {
  130. position.translation_y = static_cast<float>(-(s16)GetU16LE(m_AnimationHeaders[i].offset_to_animation_data + m_AnimationHeaders[i].offset_to_static_translation_data + ty * 2));
  131. }
  132. if (flag & 0x40)
  133. {
  134. position.translation_z = static_cast<float>(-(s16)GetU16LE(m_AnimationHeaders[i].offset_to_animation_data + m_AnimationHeaders[i].offset_to_frames_translation_data + tz * m_AnimationHeaders[i].number_of_frames * 2 + j * 2));
  135. }
  136. else if (tz != 0xFF)
  137. {
  138. position.translation_z = static_cast<float>(-(s16)GetU16LE(m_AnimationHeaders[i].offset_to_animation_data + m_AnimationHeaders[i].offset_to_static_translation_data + tz * 2));
  139. }
  140. //LOGGER->Log(LOGGER_INFO, "%d %d", m_AnimationHeaders[i].number_of_frames, j);
  141. //LOGGER->Log(LOGGER_INFO, "animation (%f %f %f) (%f %f %f)", position.rotation_x, position.rotation_y, position.rotation_z, position.translation_x, position.translation_y, position.translation_z);
  142. frame.bone.push_back(position);
  143. Ogre::Bone* bone1 = skeleton->getBone(k * 2 + 1);
  144. Ogre::Bone* bone2 = skeleton->getBone(k * 2 + 2);
  145. Ogre::NodeAnimationTrack* track1;
  146. Ogre::NodeAnimationTrack* track2;
  147. if (j == 0)
  148. {
  149. track1 = anim->createNodeTrack(k * 2 + 1, bone1);
  150. track1->removeAllKeyFrames();
  151. track2 = anim->createNodeTrack(k * 2 + 2, bone2);
  152. track2->removeAllKeyFrames();
  153. }
  154. else
  155. {
  156. track1 = anim->getNodeTrack(k * 2 + 1);
  157. track2 = anim->getNodeTrack(k * 2 + 2);
  158. }
  159. Ogre::TransformKeyFrame* frame1 = track1->createNodeKeyFrame((float)j / 30.0f);
  160. Ogre::TransformKeyFrame* frame2 = track2->createNodeKeyFrame((float)j / 30.0f);
  161. float length = skeleton_length[ k ];
  162. frame1->setTranslate(Ogre::Vector3(position.translation_x, position.translation_z - length, position.translation_y) / 1024);
  163. Ogre::Quaternion rot;
  164. Ogre::Matrix3 mat;
  165. mat.FromEulerAnglesZXY(Ogre::Radian(Ogre::Degree(-position.rotation_y)), Ogre::Radian(Ogre::Degree(-position.rotation_x)), Ogre::Radian(Ogre::Degree(-position.rotation_z)));
  166. rot.FromRotationMatrix(mat);
  167. frame2->setRotation(rot);
  168. }
  169. }
  170. }
  171. }