Animation.cpp 2.5 KB

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