QGearsHRCSkeletonLoader.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. -----------------------------------------------------------------------------
  3. The MIT License (MIT)
  4. Copyright (c) 2013-08-17 Tobias Peters <tobias.peters@kreativeffekt.at>
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. -----------------------------------------------------------------------------
  21. */
  22. #include "data/QGearsHRCSkeletonLoader.h"
  23. #include <OgreBone.h>
  24. #include "data/QGearsHRCFile.h"
  25. namespace QGears
  26. {
  27. //-------------------------------------------------------------------------
  28. const String HRCSkeletonLoader::ROOT_BONE_NAME( "root" );
  29. const Ogre::Quaternion HRCSkeletonLoader::ROOT_ORIENTATION( HRCSkeletonLoader::createRootOrientation() );
  30. typedef HRCFile::BoneList BoneList;
  31. //-------------------------------------------------------------------------
  32. HRCSkeletonLoader::HRCSkeletonLoader( HRCFile &hrc_file ) :
  33. m_hrc_file( hrc_file )
  34. {
  35. }
  36. //-------------------------------------------------------------------------
  37. HRCSkeletonLoader::~HRCSkeletonLoader()
  38. {
  39. }
  40. //---------------------------------------------------------------------
  41. Ogre::Quaternion
  42. HRCSkeletonLoader::createRootOrientation()
  43. {
  44. Ogre::Radian angle( Ogre::Degree( 180 ) );
  45. Ogre::Vector3 axis( 0, 1, 1 );
  46. return Ogre::Quaternion( angle, axis );
  47. }
  48. //-------------------------------------------------------------------------
  49. void
  50. HRCSkeletonLoader::loadResource( Ogre::Resource *resource )
  51. {
  52. Ogre::Skeleton *skeleton( static_cast<Ogre::Skeleton *>(resource) );
  53. assert( skeleton );
  54. m_hrc_file.load();
  55. skeleton->_notifyOrigin( m_hrc_file.getName() );
  56. Ogre::Bone *root( skeleton->createBone( ROOT_BONE_NAME ) );
  57. root->setOrientation( ROOT_ORIENTATION );
  58. typedef std::map< String, Ogre::Real> LengthMap;
  59. LengthMap length_map;
  60. LengthMap::const_iterator length;
  61. BoneList &bones( m_hrc_file.getBones() );
  62. for( BoneList::const_iterator bone( bones.begin() )
  63. ;bone != bones.end()
  64. ;++bone )
  65. {
  66. Ogre::Bone* child( skeleton->createBone( bone->name ) );
  67. Ogre::Bone* parent( skeleton->getBone( bone->parent ) );
  68. length_map[ bone->name ] = bone->length;
  69. length = length_map.find( bone->parent );
  70. if( length != length_map.end() )
  71. {
  72. child->setPosition( 0, length->second, 0 );
  73. }
  74. parent->addChild( child );
  75. }
  76. }
  77. //-------------------------------------------------------------------------
  78. }