polyimport.h 9.8 KB

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