Skeleton.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <cstring>
  2. #include <fstream>
  3. #include "anki/resource/Skeleton.h"
  4. #include "anki/util/BinaryStream.h"
  5. namespace anki {
  6. //==============================================================================
  7. // load =
  8. //==============================================================================
  9. void Skeleton::load(const char* filename)
  10. {
  11. std::ifstream fs;
  12. fs.open(filename);
  13. if(!fs.is_open())
  14. {
  15. throw ANKI_EXCEPTION("Cannot open \"" + filename + "\"");
  16. }
  17. try
  18. {
  19. BinaryStream bs(fs.rdbuf());
  20. // Magic word
  21. char magic[8];
  22. bs.read(magic, 8);
  23. if(bs.fail() || memcmp(magic, "ANKISKEL", 8))
  24. {
  25. throw ANKI_EXCEPTION("Incorrect magic word");
  26. }
  27. // Bones num
  28. uint bonesNum = bs.readUint();
  29. bones.resize(bonesNum);
  30. // For all bones
  31. for(uint i=0; i<bones.size(); i++)
  32. {
  33. Bone& bone = bones[i];
  34. bone.id = i;
  35. bone.name = bs.readString();
  36. for(uint j=0; j<3; j++)
  37. {
  38. bone.head[j] = bs.readFloat();
  39. }
  40. for(uint j=0; j<3; j++)
  41. {
  42. bone.tail[j] = bs.readFloat();
  43. }
  44. // Matrix
  45. Mat4 m4;
  46. for(uint j=0; j<16; j++)
  47. {
  48. m4[j] = bs.readFloat();
  49. }
  50. // Matrix for real
  51. bone.rotSkelSpace = m4.getRotationPart();
  52. bone.tslSkelSpace = m4.getTranslationPart();
  53. Mat4 MAi(m4.getInverse());
  54. bone.rotSkelSpaceInv = MAi.getRotationPart();
  55. bone.tslSkelSpaceInv = MAi.getTranslationPart();
  56. // Parent
  57. uint parentId = bs.readUint();
  58. if(parentId == 0xFFFFFFFF)
  59. {
  60. bone.parent = NULL;
  61. }
  62. else if(parentId >= bonesNum)
  63. {
  64. throw ANKI_EXCEPTION("Incorrect parent id");
  65. }
  66. else
  67. {
  68. bone.parent = &bones[parentId];
  69. }
  70. // Children
  71. uint childsNum = bs.readUint();
  72. if(childsNum > Bone::MAX_CHILDS_PER_BONE)
  73. {
  74. throw ANKI_EXCEPTION("Children for bone \"" + bone.getName() +
  75. "\" exceed the max");
  76. }
  77. bone.childsNum = childsNum;
  78. for(uint j=0; j<bone.childsNum; j++)
  79. {
  80. uint id = bs.readUint();
  81. if(id >= bonesNum)
  82. {
  83. throw ANKI_EXCEPTION("Incorrect child id");
  84. }
  85. bone.childs[j] = &bones[id];
  86. }
  87. }
  88. }
  89. catch(std::exception& e)
  90. {
  91. throw ANKI_EXCEPTION_R("Skeleton \"" + filename + "\"", e);
  92. }
  93. }
  94. } // end namespace