VGearsAFile.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <OgreBone.h>
  16. #include <OgreLogManager.h>
  17. #include <OgreKeyFrame.h>
  18. #include "data/VGearsAFile.h"
  19. #include "data/VGearsAFileSerializer.h"
  20. #include "core/Logger.h"
  21. namespace VGears{
  22. static Ogre::Vector3 ConvertRootTranslation(const Ogre::Vector3& translation, bool flip_forward_axis){
  23. return Ogre::Vector3(translation.x, translation.y, flip_forward_axis ? -translation.z : translation.z);
  24. }
  25. const Ogre::Real AFile::FRAME_DURATION(1.0f / 30.0f);
  26. const String AFile::RESOURCE_TYPE("VGearsAFile");
  27. AFile::AFile(
  28. Ogre::ResourceManager *creator, const String &name,
  29. Ogre::ResourceHandle handle, const String &group,
  30. bool is_manual, Ogre::ManualResourceLoader *loader
  31. ) :
  32. Resource(creator, name, handle, group, is_manual, loader), bone_count_(0)
  33. {}
  34. AFile::~AFile(){unload();}
  35. void AFile::SetBoneCount(const uint32 bone_count){bone_count_ = bone_count;}
  36. Ogre::AxisAlignedBox AFile::GetRootTranslationBounds(bool flip_forward_axis) const{
  37. Ogre::AxisAlignedBox bounds(Ogre::Vector3::ZERO, Ogre::Vector3::ZERO);
  38. for (const Frame& frame : frames_){
  39. bounds.merge(ConvertRootTranslation(frame.root_translation, flip_forward_axis));
  40. }
  41. return bounds;
  42. }
  43. void AFile::loadImpl(){
  44. AFileSerializer serializer;
  45. Ogre::DataStreamPtr stream(openResource());
  46. serializer.ImportAFile(stream, this);
  47. }
  48. void AFile::unloadImpl(){
  49. bone_count_ = 0;
  50. frames_.clear();
  51. }
  52. size_t AFile::CalculateSize() const{
  53. size_t size(0);
  54. FrameList::const_iterator it(frames_.begin()), it_end(frames_.end());
  55. while( it != it_end){
  56. size += sizeof(it->root_rotation);
  57. size += sizeof(it->root_translation);
  58. size
  59. += sizeof(BoneRotationList::value_type)
  60. * it->bone_rotations.size();
  61. ++ it;
  62. }
  63. return size;
  64. }
  65. void AFile::SetFrameRotation(
  66. Ogre::TransformKeyFrame* key_frame, const Ogre::Vector3& v
  67. ) const{
  68. Ogre::Quaternion rot;
  69. Ogre::Matrix3 mat;
  70. mat.FromEulerAnglesZXY(
  71. Ogre::Radian(Ogre::Degree(-v.y)), Ogre::Radian(Ogre::Degree(-v.x)),
  72. Ogre::Radian(Ogre::Degree(-v.z))
  73. );
  74. rot.FromRotationMatrix(mat);
  75. key_frame->setRotation(rot);
  76. }
  77. void AFile::AddTo(Ogre::SkeletonPtr skeleton, const String& name, bool flip_forward_axis) const{
  78. if(skeleton->hasAnimation(name)) return;
  79. Ogre::Real length((frames_.size() - 1) * FRAME_DURATION);
  80. Ogre::Animation *anim(skeleton->createAnimation(name, length));
  81. uint16 track_handle(0);
  82. Ogre::Bone* bone(skeleton->getBone("root"));
  83. Ogre::NodeAnimationTrack* track;
  84. track = anim->createNodeTrack(track_handle++, bone);
  85. Ogre::Real time;
  86. size_t index(0);
  87. for(
  88. FrameList::const_iterator frame(frames_.begin());
  89. frame != frames_.end();
  90. ++ frame
  91. ){
  92. time = (index++) * FRAME_DURATION;
  93. Ogre::TransformKeyFrame* key_frame(track->createNodeKeyFrame(time));
  94. key_frame->setTranslate(ConvertRootTranslation(frame->root_translation, flip_forward_axis));
  95. SetFrameRotation(key_frame, frame->root_rotation);
  96. }
  97. for(uint32 i(0); i < bone_count_; ++ i){
  98. if (i + 1 >= skeleton->getNumBones()){
  99. // TODO: Figure out why this happens/fix it.
  100. LOG_ERROR(
  101. "Bone " + std::to_string(i + 1) + " is out of bounds "
  102. + std::to_string(skeleton->getNumBones()) + " for: " + name
  103. + " in: " + skeleton->getName()
  104. );
  105. }
  106. else{
  107. bone = skeleton->getBone(i + 1);
  108. track = anim->createNodeTrack(track_handle ++, bone);
  109. time = 0;
  110. for (
  111. FrameList::const_iterator frame(frames_.begin());
  112. frame != frames_.end();
  113. ++ frame
  114. ){
  115. const Ogre::Vector3& rot(frame->bone_rotations[i]);
  116. Ogre::TransformKeyFrame* key_frame(
  117. track->createNodeKeyFrame(time)
  118. );
  119. SetFrameRotation(key_frame, rot);
  120. time += FRAME_DURATION;
  121. }
  122. }
  123. }
  124. }
  125. }