polyimport.h 9.9 KB

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