Animation.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (C) 2014, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "Common.h"
  6. //==============================================================================
  7. void exportAnimation(
  8. const Exporter& exporter,
  9. const aiAnimation& anim,
  10. uint32_t index)
  11. {
  12. // Get name
  13. std::string name = anim.mName.C_Str();
  14. if(name.size() == 0)
  15. {
  16. name = std::string("unnamed_") + std::to_string(index);
  17. }
  18. // Find if it's skeleton animation
  19. /*bool isSkeletalAnimation = false;
  20. for(uint32_t i = 0; i < scene.mNumMeshes; i++)
  21. {
  22. const aiMesh& mesh = *scene.mMeshes[i];
  23. if(mesh.HasBones())
  24. {
  25. }
  26. }*/
  27. std::fstream file;
  28. LOGI("Exporting animation %s\n", name.c_str());
  29. file.open(exporter.outDir + name + ".ankianim", std::ios::out);
  30. file << XML_HEADER << "\n";
  31. file << "<animation>\n";
  32. file << "\t<channels>\n";
  33. for(uint32_t i = 0; i < anim.mNumChannels; i++)
  34. {
  35. const aiNodeAnim& nAnim = *anim.mChannels[i];
  36. file << "\t\t<channel>\n";
  37. // Name
  38. file << "\t\t\t<name>" << nAnim.mNodeName.C_Str() << "</name>\n";
  39. // Positions
  40. file << "\t\t\t<positionKeys>\n";
  41. for(uint32_t j = 0; j < nAnim.mNumPositionKeys; j++)
  42. {
  43. const aiVectorKey& key = nAnim.mPositionKeys[j];
  44. if(exporter.flipyz)
  45. {
  46. file << "\t\t\t\t<key><time>" << key.mTime << "</time>"
  47. << "<value>" << key.mValue[0] << " "
  48. << key.mValue[2] << " " << -key.mValue[1]
  49. << "</value></key>\n";
  50. }
  51. else
  52. {
  53. file << "\t\t\t\t<key><time>" << key.mTime << "</time>"
  54. << "<value>" << key.mValue[0] << " "
  55. << key.mValue[1] << " " << key.mValue[2]
  56. << "</value></key>\n";
  57. }
  58. }
  59. file << "\t\t\t</positionKeys>\n";
  60. // Rotations
  61. file << "\t\t\t<rotationKeys>\n";
  62. for(uint32_t j = 0; j < nAnim.mNumRotationKeys; j++)
  63. {
  64. const aiQuatKey& key = nAnim.mRotationKeys[j];
  65. aiMatrix3x3 mat =
  66. toAnkiMatrix(key.mValue.GetMatrix(), exporter.flipyz);
  67. aiQuaternion quat(mat);
  68. //aiQuaternion quat(key.mValue);
  69. file << "\t\t\t\t<key><time>" << key.mTime << "</time>"
  70. << "<value>" << quat.x << " " << quat.y
  71. << " " << quat.z << " "
  72. << quat.w << "</value></key>\n";
  73. }
  74. file << "\t\t\t</rotationKeys>\n";
  75. // Scale
  76. file << "\t\t\t<scalingKeys>\n";
  77. for(uint32_t j = 0; j < nAnim.mNumScalingKeys; j++)
  78. {
  79. const aiVectorKey& key = nAnim.mScalingKeys[j];
  80. // Note: only uniform scale
  81. file << "\t\t\t\t<key><time>" << key.mTime << "</time>"
  82. << "<value>"
  83. << ((key.mValue[0] + key.mValue[1] + key.mValue[2]) / 3.0)
  84. << "</value></key>\n";
  85. }
  86. file << "\t\t\t</scalingKeys>\n";
  87. file << "\t\t</channel>\n";
  88. }
  89. file << "\t</channels>\n";
  90. file << "</animation>\n";
  91. }