polyimport.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #include "postprocess.h"
  2. #include "scene.h"
  3. #include <stdio.h>
  4. #include "polycode/core/PolyMesh.h"
  5. #include "polycode/core/PolyString.h"
  6. #include <vector>
  7. using std::vector;
  8. class IBone {
  9. public:
  10. IBone() {
  11. parent = NULL;
  12. }
  13. IBone *parent;
  14. aiString name;
  15. aiMatrix4x4 t;
  16. aiMatrix4x4 bindMatrix;
  17. unsigned int boneID;
  18. vector<IBone*> children;
  19. };
  20. class ITrack {
  21. public:
  22. ITrack(){}
  23. aiNodeAnim *nodeAnim;
  24. Polycode::String boneName;
  25. };
  26. class IAnimation {
  27. public:
  28. IAnimation(){}
  29. Polycode::String name;
  30. unsigned int numTracks;
  31. float tps;
  32. float length;
  33. vector<ITrack*> tracks;
  34. };
  35. bool BonesSortPredicate(const IBone* d1, const IBone* d2)
  36. {
  37. return d1->boneID < d2->boneID;
  38. }
  39. class ISkeleton {
  40. public:
  41. ISkeleton() {
  42. }
  43. void addIBone(IBone *bone, int boneID) {
  44. bone->boneID = boneID;
  45. bones.push_back(bone);
  46. }
  47. unsigned int getBoneID(aiString name) {
  48. for(int i=0; i < bones.size(); i++) {
  49. if(bones[i]->name == name) {
  50. return i;
  51. }
  52. }
  53. return -1;
  54. }
  55. void saveToFile(const char *fileName, bool swapZY) {
  56. using Polycode::String;
  57. String fileNameSkel = String(fileName);
  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]->boneName = anim->tracks[j]->nodeAnim->mNodeName.data;
  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. unsigned short boneNameLen = track->boneName.length();
  163. fwrite(&boneNameLen, sizeof(unsigned short), 1, file);
  164. char boneNameBuffer[1024];
  165. memcpy(boneNameBuffer, track->boneName.c_str(), boneNameLen);
  166. fwrite(boneNameBuffer, 1, boneNameLen, file);
  167. aiNodeAnim *nodeAnim = track->nodeAnim;
  168. unsigned int curveType,numPoints;
  169. unsigned int numCurves = 10;
  170. if(nodeAnim->mNumPositionKeys < 2) {
  171. numCurves -= 3;
  172. }
  173. fwrite(&numCurves, sizeof(unsigned int), 1, file);
  174. curveType = 0;
  175. numPoints = nodeAnim->mNumScalingKeys;
  176. fwrite(&curveType, sizeof(unsigned int), 1, file);
  177. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  178. for(int f =0; f < nodeAnim->mNumScalingKeys; f++) {
  179. aiVectorKey key = nodeAnim->mScalingKeys[f];
  180. fwrite(&key.mValue.x, sizeof(float), 1, file);
  181. float time = key.mTime;
  182. fwrite(&time, sizeof(float), 1, file);
  183. }
  184. if(swapZY) {
  185. curveType = 2;
  186. } else {
  187. curveType = 1;
  188. }
  189. numPoints = nodeAnim->mNumScalingKeys;
  190. fwrite(&curveType, sizeof(unsigned int), 1, file);
  191. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  192. for(int f =0; f < nodeAnim->mNumScalingKeys; f++) {
  193. aiVectorKey key = nodeAnim->mScalingKeys[f];
  194. fwrite(&key.mValue.y, sizeof(float), 1, file);
  195. float time = key.mTime;
  196. fwrite(&time, sizeof(float), 1, file);
  197. }
  198. if(swapZY) {
  199. curveType = 1;
  200. } else {
  201. curveType = 2;
  202. }
  203. numPoints = nodeAnim->mNumScalingKeys;
  204. fwrite(&curveType, sizeof(unsigned int), 1, file);
  205. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  206. for(int f =0; f < nodeAnim->mNumScalingKeys; f++) {
  207. aiVectorKey key = nodeAnim->mScalingKeys[f];
  208. fwrite(&key.mValue.z, sizeof(float), 1, file);
  209. float time = key.mTime;
  210. fwrite(&time, sizeof(float), 1, file);
  211. }
  212. curveType = 3;
  213. numPoints = nodeAnim->mNumRotationKeys;
  214. fwrite(&curveType, sizeof(unsigned int), 1, file);
  215. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  216. for(int f =0; f < nodeAnim->mNumRotationKeys; f++) {
  217. aiQuatKey key = nodeAnim->mRotationKeys[f];
  218. float val = key.mValue.w;
  219. fwrite(&val, sizeof(float), 1, file);
  220. float time = key.mTime;
  221. fwrite(&time, sizeof(float), 1, file);
  222. }
  223. curveType = 4;
  224. numPoints = nodeAnim->mNumRotationKeys;
  225. fwrite(&curveType, sizeof(unsigned int), 1, file);
  226. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  227. for(int f =0; f < nodeAnim->mNumRotationKeys; f++) {
  228. aiQuatKey key = nodeAnim->mRotationKeys[f];
  229. fwrite(&key.mValue.x, sizeof(float), 1, file);
  230. float time = key.mTime;
  231. fwrite(&time, sizeof(float), 1, file);
  232. }
  233. if(swapZY) {
  234. curveType = 6;
  235. } else {
  236. curveType = 5;
  237. }
  238. numPoints = nodeAnim->mNumRotationKeys;
  239. fwrite(&curveType, sizeof(unsigned int), 1, file);
  240. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  241. for(int f =0; f < nodeAnim->mNumRotationKeys; f++) {
  242. aiQuatKey key = nodeAnim->mRotationKeys[f];
  243. float val = key.mValue.y;
  244. if(swapZY) {
  245. val *= -1;
  246. }
  247. fwrite(&val, sizeof(float), 1, file);
  248. float time = key.mTime;
  249. fwrite(&time, sizeof(float), 1, file);
  250. }
  251. if(swapZY) {
  252. curveType = 5;
  253. } else {
  254. curveType = 6;
  255. }
  256. numPoints = nodeAnim->mNumRotationKeys;
  257. fwrite(&curveType, sizeof(unsigned int), 1, file);
  258. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  259. for(int f =0; f < nodeAnim->mNumRotationKeys; f++) {
  260. aiQuatKey key = nodeAnim->mRotationKeys[f];
  261. fwrite(&key.mValue.z, sizeof(float), 1, file);
  262. float time = key.mTime;
  263. fwrite(&time, sizeof(float), 1, file);
  264. }
  265. curveType = 7;
  266. numPoints = nodeAnim->mNumPositionKeys;
  267. if(numPoints > 1) {
  268. fwrite(&curveType, sizeof(unsigned int), 1, file);
  269. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  270. for(int f =0; f < nodeAnim->mNumPositionKeys; f++) {
  271. aiVectorKey key = nodeAnim->mPositionKeys[f];
  272. fwrite(&key.mValue.x, sizeof(float), 1, file);
  273. float time = key.mTime;
  274. fwrite(&time, sizeof(float), 1, file);
  275. }
  276. }
  277. if(swapZY) {
  278. curveType = 9;
  279. } else {
  280. curveType = 8;
  281. }
  282. numPoints = nodeAnim->mNumPositionKeys;
  283. if(numPoints > 1) {
  284. fwrite(&curveType, sizeof(unsigned int), 1, file);
  285. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  286. for(int f =0; f < nodeAnim->mNumPositionKeys; f++) {
  287. aiVectorKey key = nodeAnim->mPositionKeys[f];
  288. float val = key.mValue.y;
  289. if(swapZY) {
  290. val *= -1;
  291. }
  292. fwrite(&val, sizeof(float), 1, file);
  293. float time = key.mTime;
  294. fwrite(&time, sizeof(float), 1, file);
  295. }
  296. }
  297. if(swapZY) {
  298. curveType = 8;
  299. } else {
  300. curveType = 9;
  301. }
  302. numPoints = nodeAnim->mNumPositionKeys;
  303. if(numPoints > 1) {
  304. fwrite(&curveType, sizeof(unsigned int), 1, file);
  305. fwrite(&numPoints, sizeof(unsigned int), 1, file);
  306. for(int f =0; f < nodeAnim->mNumPositionKeys; f++) {
  307. aiVectorKey key = nodeAnim->mPositionKeys[f];
  308. fwrite(&key.mValue.z, sizeof(float), 1, file);
  309. float time = key.mTime;
  310. fwrite(&time, sizeof(float), 1, file);
  311. }
  312. }
  313. }
  314. fclose(file);
  315. }
  316. }
  317. void addAnimation(IAnimation *anim) { animations.push_back(anim); }
  318. vector<IAnimation*> animations;
  319. vector<IBone*> bones;
  320. };