Skeleton.cpp 2.2 KB

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