polyimport.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #include "assimp.h"
  2. #include "assimp.h"
  3. #include "aiPostProcess.h"
  4. #include "aiScene.h"
  5. #include <stdio.h>
  6. #include "PolyMesh.h"
  7. #include "PolyString.h"
  8. #include <vector>
  9. using std::vector;
  10. class IBone {
  11. public:
  12. IBone() {
  13. parent = NULL;
  14. }
  15. IBone *parent;
  16. aiString name;
  17. aiMatrix4x4 t;
  18. aiMatrix4x4 bindMatrix;
  19. unsigned int boneID;
  20. vector<IBone*> children;
  21. };
  22. class ITrack {
  23. public:
  24. ITrack(){ boneID = 666; }
  25. unsigned int boneID;
  26. aiNodeAnim *nodeAnim;
  27. };
  28. class IAnimation {
  29. public:
  30. IAnimation(){}
  31. Polycode::String name;
  32. unsigned int numTracks;
  33. float tps;
  34. float length;
  35. vector<ITrack*> tracks;
  36. };
  37. bool BonesSortPredicate(const IBone* d1, const IBone* d2)
  38. {
  39. return d1->boneID < d2->boneID;
  40. }
  41. class ISkeleton {
  42. public:
  43. ISkeleton() {
  44. }
  45. void addIBone(IBone *bone, int boneID) {
  46. bone->boneID = boneID;
  47. bones.push_back(bone);
  48. }
  49. unsigned int getBoneID(aiString name) {
  50. for(int i=0; i < bones.size(); i++) {
  51. if(bones[i]->name == name) {
  52. return i;
  53. }
  54. }
  55. return -1;
  56. }
  57. void saveToFile(const char *fileName, bool swapZY) {
  58. using Polycode::String;
  59. String fileNameSkel = String(fileName)+".skeleton";
  60. FILE *file = fopen(fileNameSkel.c_str(), "wb");
  61. unsigned int numBones = bones.size();
  62. fwrite(&numBones, sizeof(unsigned int), 1, file);
  63. std::sort(bones.begin(), bones.end(), BonesSortPredicate);
  64. for(int i=0; i < bones.size(); i++) {
  65. bones[i]->boneID = i;
  66. }
  67. for(int i=0; i < animations.size(); i++) {
  68. IAnimation *anim = animations[i];
  69. for(int j=0; j < anim->numTracks; j++) {
  70. anim->tracks[j]->boneID = this->getBoneID(anim->tracks[j]->nodeAnim->mNodeName);
  71. }
  72. }
  73. for(int i=0; i < bones.size(); i++) {
  74. unsigned int len = bones[i]->name.length;
  75. fwrite(&len, sizeof(unsigned int), 1, file);
  76. fwrite(bones[i]->name.data, 1, len, file);
  77. unsigned int hasParent;
  78. if(bones[i]->parent) {
  79. hasParent = 1;
  80. fwrite(&hasParent, sizeof(unsigned int), 1, file);
  81. fwrite(&bones[i]->parent->boneID, sizeof(unsigned int), 1, file);
  82. } else {
  83. hasParent = 0;
  84. fwrite(&hasParent, sizeof(unsigned int), 1, file);
  85. }
  86. aiVector3D scale;
  87. aiQuaternion rotation;
  88. aiVector3D position;
  89. bones[i]->t.Decompose(scale, rotation, position);
  90. fwrite(&position.x, sizeof(float), 1, file);
  91. if(swapZY) {
  92. fwrite(&position.z, sizeof(float), 1, file);
  93. position.y *= -1;
  94. fwrite(&position.y, sizeof(float), 1, file);
  95. } else {
  96. fwrite(&position.y, sizeof(float), 1, file);
  97. fwrite(&position.z, sizeof(float), 1, file);
  98. }
  99. fwrite(&scale.x, sizeof(float), 1, file);
  100. if(swapZY) {
  101. fwrite(&scale.z, sizeof(float), 1, file);
  102. fwrite(&scale.y, sizeof(float), 1, file);
  103. } else {
  104. fwrite(&scale.y, sizeof(float), 1, file);
  105. fwrite(&scale.z, sizeof(float), 1, file);
  106. }
  107. fwrite(&rotation.w, sizeof(float), 1, file);
  108. fwrite(&rotation.x, sizeof(float), 1, file);
  109. if(swapZY) {
  110. fwrite(&rotation.z, sizeof(float), 1, file);
  111. rotation.y *= -1;
  112. fwrite(&rotation.y, sizeof(float), 1, file);
  113. } else {
  114. fwrite(&rotation.y, sizeof(float), 1, file);
  115. fwrite(&rotation.z, sizeof(float), 1, file);
  116. }
  117. bones[i]->bindMatrix.Decompose(scale, rotation, position);
  118. fwrite(&position.x, sizeof(float), 1, file);
  119. if(swapZY) {
  120. fwrite(&position.z, sizeof(float), 1, file);
  121. position.y *= -1;
  122. fwrite(&position.y, sizeof(float), 1, file);
  123. } else {
  124. fwrite(&position.y, sizeof(float), 1, file);
  125. fwrite(&position.z, sizeof(float), 1, file);
  126. }
  127. fwrite(&scale.x, sizeof(float), 1, file);
  128. if(swapZY) {
  129. fwrite(&scale.z, sizeof(float), 1, file);
  130. fwrite(&scale.y, sizeof(float), 1, file);
  131. } else {
  132. fwrite(&scale.y, sizeof(float), 1, file);
  133. fwrite(&scale.z, sizeof(float), 1, file);
  134. }
  135. fwrite(&rotation.w, sizeof(float), 1, file);
  136. fwrite(&rotation.x, sizeof(float), 1, file);
  137. if(swapZY) {
  138. fwrite(&rotation.z, sizeof(float), 1, file);
  139. rotation.y *= -1;
  140. fwrite(&rotation.y, sizeof(float), 1, file);
  141. } else {
  142. fwrite(&rotation.y, sizeof(float), 1, file);
  143. fwrite(&rotation.z, sizeof(float), 1, file);
  144. }
  145. }
  146. fclose(file);
  147. // unsigned int numAnimations = animations.size();
  148. // fwrite(&numAnimations, sizeof(unsigned int), 1, file);
  149. for(int i=0; i < animations.size(); i++) {
  150. char anim_s[2];
  151. sprintf(anim_s, "%d", i);
  152. String fileNameAnim = String(fileName)+"_animation";//+anim+".anim";
  153. fileNameAnim += anim_s;
  154. fileNameAnim += ".anim";
  155. file = fopen(fileNameAnim.c_str(), "wb");
  156. IAnimation *anim = animations[i];
  157. // unsigned int len = anim->name.size();
  158. // fwrite(&len, sizeof(unsigned int), 1, file);
  159. // fwrite(anim->name.c_str(), 1, len, file);
  160. fwrite(&anim->length, sizeof(float), 1, file);
  161. fwrite(&anim->numTracks, sizeof(unsigned int), 1, file);
  162. for(int j=0; j < anim->numTracks; j++) {
  163. ITrack *track = anim->tracks[j];
  164. fwrite(&track->boneID, sizeof(unsigned int), 1, file);
  165. aiNodeAnim *nodeAnim = track->nodeAnim;
  166. unsigned int curveType,numPoints;
  167. unsigned int numCurves = 10;
  168. if(nodeAnim->mNumPositionKeys < 2) {
  169. numCurves -= 3;
  170. }
  171. fwrite(&numCurves, sizeof(unsigned int), 1, file);
  172. curveType = 0;
  173. numPoints = nodeAnim->mNumScalingKeys;
  174. fwrite(&curveType, sizeof(unsigned int), 1, file);
  175. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  176. for(int f =0; f < nodeAnim->mNumScalingKeys; f++) {
  177. aiVectorKey key = nodeAnim->mScalingKeys[f];
  178. fwrite(&key.mValue.x, sizeof(float), 1, file);
  179. float time = key.mTime;
  180. fwrite(&time, sizeof(float), 1, file);
  181. }
  182. if(swapZY) {
  183. curveType = 2;
  184. } else {
  185. curveType = 1;
  186. }
  187. numPoints = nodeAnim->mNumScalingKeys;
  188. fwrite(&curveType, sizeof(unsigned int), 1, file);
  189. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  190. for(int f =0; f < nodeAnim->mNumScalingKeys; f++) {
  191. aiVectorKey key = nodeAnim->mScalingKeys[f];
  192. fwrite(&key.mValue.y, sizeof(float), 1, file);
  193. float time = key.mTime;
  194. fwrite(&time, sizeof(float), 1, file);
  195. }
  196. if(swapZY) {
  197. curveType = 1;
  198. } else {
  199. curveType = 2;
  200. }
  201. numPoints = nodeAnim->mNumScalingKeys;
  202. fwrite(&curveType, sizeof(unsigned int), 1, file);
  203. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  204. for(int f =0; f < nodeAnim->mNumScalingKeys; f++) {
  205. aiVectorKey key = nodeAnim->mScalingKeys[f];
  206. fwrite(&key.mValue.z, sizeof(float), 1, file);
  207. float time = key.mTime;
  208. fwrite(&time, sizeof(float), 1, file);
  209. }
  210. curveType = 3;
  211. numPoints = nodeAnim->mNumRotationKeys;
  212. fwrite(&curveType, sizeof(unsigned int), 1, file);
  213. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  214. for(int f =0; f < nodeAnim->mNumRotationKeys; f++) {
  215. aiQuatKey key = nodeAnim->mRotationKeys[f];
  216. float val = key.mValue.w;
  217. fwrite(&val, sizeof(float), 1, file);
  218. float time = key.mTime;
  219. fwrite(&time, sizeof(float), 1, file);
  220. }
  221. curveType = 4;
  222. numPoints = nodeAnim->mNumRotationKeys;
  223. fwrite(&curveType, sizeof(unsigned int), 1, file);
  224. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  225. for(int f =0; f < nodeAnim->mNumRotationKeys; f++) {
  226. aiQuatKey key = nodeAnim->mRotationKeys[f];
  227. fwrite(&key.mValue.x, sizeof(float), 1, file);
  228. float time = key.mTime;
  229. fwrite(&time, sizeof(float), 1, file);
  230. }
  231. if(swapZY) {
  232. curveType = 6;
  233. } else {
  234. curveType = 5;
  235. }
  236. numPoints = nodeAnim->mNumRotationKeys;
  237. fwrite(&curveType, sizeof(unsigned int), 1, file);
  238. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  239. for(int f =0; f < nodeAnim->mNumRotationKeys; f++) {
  240. aiQuatKey key = nodeAnim->mRotationKeys[f];
  241. float val = key.mValue.y;
  242. if(swapZY) {
  243. val *= -1;
  244. }
  245. fwrite(&val, sizeof(float), 1, file);
  246. float time = key.mTime;
  247. fwrite(&time, sizeof(float), 1, file);
  248. }
  249. if(swapZY) {
  250. curveType = 5;
  251. } else {
  252. curveType = 6;
  253. }
  254. numPoints = nodeAnim->mNumRotationKeys;
  255. fwrite(&curveType, sizeof(unsigned int), 1, file);
  256. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  257. for(int f =0; f < nodeAnim->mNumRotationKeys; f++) {
  258. aiQuatKey key = nodeAnim->mRotationKeys[f];
  259. fwrite(&key.mValue.z, sizeof(float), 1, file);
  260. float time = key.mTime;
  261. fwrite(&time, sizeof(float), 1, file);
  262. }
  263. curveType = 7;
  264. numPoints = nodeAnim->mNumPositionKeys;
  265. if(numPoints > 1) {
  266. fwrite(&curveType, sizeof(unsigned int), 1, file);
  267. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  268. for(int f =0; f < nodeAnim->mNumPositionKeys; f++) {
  269. aiVectorKey key = nodeAnim->mPositionKeys[f];
  270. fwrite(&key.mValue.x, sizeof(float), 1, file);
  271. float time = key.mTime;
  272. fwrite(&time, sizeof(float), 1, file);
  273. }
  274. }
  275. if(swapZY) {
  276. curveType = 9;
  277. } else {
  278. curveType = 8;
  279. }
  280. numPoints = nodeAnim->mNumPositionKeys;
  281. if(numPoints > 1) {
  282. fwrite(&curveType, sizeof(unsigned int), 1, file);
  283. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  284. for(int f =0; f < nodeAnim->mNumPositionKeys; f++) {
  285. aiVectorKey key = nodeAnim->mPositionKeys[f];
  286. float val = key.mValue.y;
  287. if(swapZY) {
  288. val *= -1;
  289. }
  290. fwrite(&val, sizeof(float), 1, file);
  291. float time = key.mTime;
  292. fwrite(&time, sizeof(float), 1, file);
  293. }
  294. }
  295. if(swapZY) {
  296. curveType = 8;
  297. } else {
  298. curveType = 9;
  299. }
  300. numPoints = nodeAnim->mNumPositionKeys;
  301. if(numPoints > 1) {
  302. fwrite(&curveType, sizeof(unsigned int), 1, file);
  303. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  304. for(int f =0; f < nodeAnim->mNumPositionKeys; f++) {
  305. aiVectorKey key = nodeAnim->mPositionKeys[f];
  306. fwrite(&key.mValue.z, sizeof(float), 1, file);
  307. float time = key.mTime;
  308. fwrite(&time, sizeof(float), 1, file);
  309. }
  310. }
  311. }
  312. fclose(file);
  313. }
  314. }
  315. void addAnimation(IAnimation *anim) { animations.push_back(anim); }
  316. vector<IAnimation*> animations;
  317. vector<IBone*> bones;
  318. };