polyimport.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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(){}
  25. aiNodeAnim *nodeAnim;
  26. Polycode::String boneName;
  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);
  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]->boneName = anim->tracks[j]->nodeAnim->mNodeName.data;
  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. unsigned short boneNameLen = track->boneName.length();
  165. fwrite(&boneNameLen, sizeof(unsigned short), 1, file);
  166. char boneNameBuffer[1024];
  167. memcpy(boneNameBuffer, track->boneName.c_str(), boneNameLen);
  168. fwrite(boneNameBuffer, 1, boneNameLen, file);
  169. aiNodeAnim *nodeAnim = track->nodeAnim;
  170. unsigned int curveType,numPoints;
  171. unsigned int numCurves = 10;
  172. if(nodeAnim->mNumPositionKeys < 2) {
  173. numCurves -= 3;
  174. }
  175. fwrite(&numCurves, sizeof(unsigned int), 1, file);
  176. curveType = 0;
  177. numPoints = nodeAnim->mNumScalingKeys;
  178. fwrite(&curveType, sizeof(unsigned int), 1, file);
  179. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  180. for(int f =0; f < nodeAnim->mNumScalingKeys; f++) {
  181. aiVectorKey key = nodeAnim->mScalingKeys[f];
  182. fwrite(&key.mValue.x, sizeof(float), 1, file);
  183. float time = key.mTime;
  184. fwrite(&time, sizeof(float), 1, file);
  185. }
  186. if(swapZY) {
  187. curveType = 2;
  188. } else {
  189. curveType = 1;
  190. }
  191. numPoints = nodeAnim->mNumScalingKeys;
  192. fwrite(&curveType, sizeof(unsigned int), 1, file);
  193. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  194. for(int f =0; f < nodeAnim->mNumScalingKeys; f++) {
  195. aiVectorKey key = nodeAnim->mScalingKeys[f];
  196. fwrite(&key.mValue.y, sizeof(float), 1, file);
  197. float time = key.mTime;
  198. fwrite(&time, sizeof(float), 1, file);
  199. }
  200. if(swapZY) {
  201. curveType = 1;
  202. } else {
  203. curveType = 2;
  204. }
  205. numPoints = nodeAnim->mNumScalingKeys;
  206. fwrite(&curveType, sizeof(unsigned int), 1, file);
  207. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  208. for(int f =0; f < nodeAnim->mNumScalingKeys; f++) {
  209. aiVectorKey key = nodeAnim->mScalingKeys[f];
  210. fwrite(&key.mValue.z, sizeof(float), 1, file);
  211. float time = key.mTime;
  212. fwrite(&time, sizeof(float), 1, file);
  213. }
  214. curveType = 3;
  215. numPoints = nodeAnim->mNumRotationKeys;
  216. fwrite(&curveType, sizeof(unsigned int), 1, file);
  217. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  218. for(int f =0; f < nodeAnim->mNumRotationKeys; f++) {
  219. aiQuatKey key = nodeAnim->mRotationKeys[f];
  220. float val = key.mValue.w;
  221. fwrite(&val, sizeof(float), 1, file);
  222. float time = key.mTime;
  223. fwrite(&time, sizeof(float), 1, file);
  224. }
  225. curveType = 4;
  226. numPoints = nodeAnim->mNumRotationKeys;
  227. fwrite(&curveType, sizeof(unsigned int), 1, file);
  228. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  229. for(int f =0; f < nodeAnim->mNumRotationKeys; f++) {
  230. aiQuatKey key = nodeAnim->mRotationKeys[f];
  231. fwrite(&key.mValue.x, sizeof(float), 1, file);
  232. float time = key.mTime;
  233. fwrite(&time, sizeof(float), 1, file);
  234. }
  235. if(swapZY) {
  236. curveType = 6;
  237. } else {
  238. curveType = 5;
  239. }
  240. numPoints = nodeAnim->mNumRotationKeys;
  241. fwrite(&curveType, sizeof(unsigned int), 1, file);
  242. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  243. for(int f =0; f < nodeAnim->mNumRotationKeys; f++) {
  244. aiQuatKey key = nodeAnim->mRotationKeys[f];
  245. float val = key.mValue.y;
  246. if(swapZY) {
  247. val *= -1;
  248. }
  249. fwrite(&val, sizeof(float), 1, file);
  250. float time = key.mTime;
  251. fwrite(&time, sizeof(float), 1, file);
  252. }
  253. if(swapZY) {
  254. curveType = 5;
  255. } else {
  256. curveType = 6;
  257. }
  258. numPoints = nodeAnim->mNumRotationKeys;
  259. fwrite(&curveType, sizeof(unsigned int), 1, file);
  260. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  261. for(int f =0; f < nodeAnim->mNumRotationKeys; f++) {
  262. aiQuatKey key = nodeAnim->mRotationKeys[f];
  263. fwrite(&key.mValue.z, sizeof(float), 1, file);
  264. float time = key.mTime;
  265. fwrite(&time, sizeof(float), 1, file);
  266. }
  267. curveType = 7;
  268. numPoints = nodeAnim->mNumPositionKeys;
  269. if(numPoints > 1) {
  270. fwrite(&curveType, sizeof(unsigned int), 1, file);
  271. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  272. for(int f =0; f < nodeAnim->mNumPositionKeys; f++) {
  273. aiVectorKey key = nodeAnim->mPositionKeys[f];
  274. fwrite(&key.mValue.x, sizeof(float), 1, file);
  275. float time = key.mTime;
  276. fwrite(&time, sizeof(float), 1, file);
  277. }
  278. }
  279. if(swapZY) {
  280. curveType = 9;
  281. } else {
  282. curveType = 8;
  283. }
  284. numPoints = nodeAnim->mNumPositionKeys;
  285. if(numPoints > 1) {
  286. fwrite(&curveType, sizeof(unsigned int), 1, file);
  287. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  288. for(int f =0; f < nodeAnim->mNumPositionKeys; f++) {
  289. aiVectorKey key = nodeAnim->mPositionKeys[f];
  290. float val = key.mValue.y;
  291. if(swapZY) {
  292. val *= -1;
  293. }
  294. fwrite(&val, sizeof(float), 1, file);
  295. float time = key.mTime;
  296. fwrite(&time, sizeof(float), 1, file);
  297. }
  298. }
  299. if(swapZY) {
  300. curveType = 8;
  301. } else {
  302. curveType = 9;
  303. }
  304. numPoints = nodeAnim->mNumPositionKeys;
  305. if(numPoints > 1) {
  306. fwrite(&curveType, sizeof(unsigned int), 1, file);
  307. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  308. for(int f =0; f < nodeAnim->mNumPositionKeys; f++) {
  309. aiVectorKey key = nodeAnim->mPositionKeys[f];
  310. fwrite(&key.mValue.z, sizeof(float), 1, file);
  311. float time = key.mTime;
  312. fwrite(&time, sizeof(float), 1, file);
  313. }
  314. }
  315. }
  316. fclose(file);
  317. }
  318. }
  319. void addAnimation(IAnimation *anim) { animations.push_back(anim); }
  320. vector<IAnimation*> animations;
  321. vector<IBone*> bones;
  322. };