polyimport.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #include "polyimport.h"
  2. #include "PolyPolygon.h"
  3. #include "OSBasics.h"
  4. #include "physfs.h"
  5. using namespace Polycode;
  6. const struct aiScene* scene = NULL;
  7. bool hasWeights = false;
  8. vector<aiBone*> bones;
  9. unsigned int numBones = 0;
  10. unsigned int addBone(aiBone *bone) {
  11. for(int i=0; i < bones.size(); i++) {
  12. if(bones[i]->mName == bone->mName)
  13. return i;
  14. }
  15. bones.push_back(bone);
  16. return bones.size()-1;
  17. }
  18. void addToMesh(String prefix, Polycode::Mesh *tmesh, const struct aiScene *sc, const struct aiNode* nd, bool swapZY, bool addSubmeshes, bool listOnly) {
  19. int i;
  20. unsigned int n = 0, t;
  21. // draw all meshes assigned to this node
  22. for (; n < nd->mNumMeshes; ++n) {
  23. if(!addSubmeshes) {
  24. tmesh = new Polycode::Mesh(Mesh::TRI_MESH);
  25. }
  26. const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
  27. if(listOnly) {
  28. printf("%s%s.mesh\n", prefix.c_str(), nd->mName.data);
  29. } else {
  30. printf("Importing mesh:%s (%d vertices) (%d faces) \n", mesh->mName.data, mesh->mNumVertices, mesh->mNumFaces);
  31. }
  32. //apply_material(sc->mMaterials[mesh->mMaterialIndex]);
  33. for (t = 0; t < mesh->mNumFaces; ++t) {
  34. const struct aiFace* face = &mesh->mFaces[t];
  35. Polycode::Polygon *poly = new Polycode::Polygon();
  36. for(i = 0; i < face->mNumIndices; i++) {
  37. Vertex *vertex = new Vertex();
  38. int index = face->mIndices[i];
  39. if(mesh->mColors[0] != NULL) {
  40. vertex->vertexColor.setColorRGBA(mesh->mColors[0][index].r, mesh->mColors[0][index].g, mesh->mColors[0][index].b, mesh->mColors[0][index].a);
  41. }
  42. if(mesh->mTangents != NULL) {
  43. if(swapZY)
  44. vertex->tangent = Vector3(mesh->mTangents[index].x, mesh->mTangents[index].z, -mesh->mTangents[index].y);
  45. else
  46. vertex->tangent = Vector3(mesh->mTangents[index].x, mesh->mTangents[index].y, mesh->mTangents[index].z);
  47. }
  48. if(mesh->mNormals != NULL) {
  49. if(swapZY)
  50. vertex->setNormal(mesh->mNormals[index].x, mesh->mNormals[index].z, -mesh->mNormals[index].y);
  51. else
  52. vertex->setNormal(mesh->mNormals[index].x, mesh->mNormals[index].y, mesh->mNormals[index].z);
  53. }
  54. if(mesh->HasTextureCoords(0))
  55. {
  56. vertex->setTexCoord(mesh->mTextureCoords[0][index].x, mesh->mTextureCoords[0][index].y);
  57. }
  58. for( unsigned int a = 0; a < mesh->mNumBones; a++) {
  59. aiBone* bone = mesh->mBones[a];
  60. unsigned int boneIndex = addBone(bone);
  61. for( unsigned int b = 0; b < bone->mNumWeights; b++) {
  62. if(bone->mWeights[b].mVertexId == index) {
  63. vertex->addBoneAssignment(boneIndex, bone->mWeights[b].mWeight);
  64. hasWeights = true;
  65. }
  66. }
  67. }
  68. if(swapZY)
  69. vertex->set(mesh->mVertices[index].x, mesh->mVertices[index].z, -mesh->mVertices[index].y);
  70. else
  71. vertex->set(mesh->mVertices[index].x, mesh->mVertices[index].y, mesh->mVertices[index].z);
  72. poly->addVertex(vertex);
  73. }
  74. tmesh->addPolygon(poly);
  75. }
  76. if(!addSubmeshes && !listOnly) {
  77. String fileNameMesh = prefix+String(nd->mName.data)+".mesh";
  78. OSFILE *outFile = OSBasics::open(fileNameMesh.c_str(), "wb");
  79. tmesh->saveToFile(outFile);
  80. OSBasics::close(outFile);
  81. delete tmesh;
  82. }
  83. }
  84. // draw all children
  85. for (n = 0; n < nd->mNumChildren; ++n) {
  86. addToMesh(prefix, tmesh, sc, nd->mChildren[n], swapZY, addSubmeshes, listOnly);
  87. }
  88. }
  89. int getBoneID(aiString name) {
  90. for(int i=0; i < bones.size(); i++) {
  91. if(bones[i]->mName == name) {
  92. return i;
  93. }
  94. }
  95. return 666;
  96. }
  97. void addToISkeleton(ISkeleton *skel, IBone *parent, const struct aiScene *sc, const struct aiNode* nd) {
  98. IBone *bone = new IBone();
  99. bone->parent = parent;
  100. bone->name = nd->mName;
  101. bone->t = nd->mTransformation;
  102. for(int i=0; i < bones.size(); i++) {
  103. if(bones[i]->mName == bone->name) {
  104. bone->bindMatrix = bones[i]->mOffsetMatrix;
  105. }
  106. }
  107. for (int n = 0; n < nd->mNumChildren; ++n) {
  108. addToISkeleton(skel, bone, sc, nd->mChildren[n]);
  109. }
  110. skel->addIBone(bone, getBoneID(bone->name));
  111. }
  112. int exportToFile(String prefix, bool swapZY, bool addSubmeshes, bool listOnly) {
  113. Polycode::Mesh *mesh = new Polycode::Mesh(Mesh::TRI_MESH);
  114. addToMesh(prefix, mesh, scene, scene->mRootNode, swapZY, addSubmeshes, listOnly);
  115. if(!listOnly && addSubmeshes) {
  116. String fileNameMesh;
  117. if(prefix != "") {
  118. fileNameMesh = prefix+".mesh";
  119. } else {
  120. fileNameMesh = "out.mesh";
  121. }
  122. OSFILE *outFile = OSBasics::open(fileNameMesh.c_str(), "wb");
  123. mesh->saveToFile(outFile);
  124. OSBasics::close(outFile);
  125. }
  126. if(hasWeights) {
  127. if(listOnly) {
  128. printf("%s.skeleton\n", prefix.c_str());
  129. } else {
  130. printf("Mesh has weights, exporting skeleton...\n");
  131. }
  132. String fileNameSkel = prefix+".skeleton";
  133. ISkeleton *skeleton = new ISkeleton();
  134. for (int n = 0; n < scene->mRootNode->mNumChildren; ++n) {
  135. if(scene->mRootNode->mChildren[n]->mNumChildren > 0) {
  136. addToISkeleton(skeleton, NULL, scene, scene->mRootNode->mChildren[n]);
  137. }
  138. }
  139. if(scene->HasAnimations()) {
  140. printf("Importing animations...\n");
  141. for(int i=0; i < scene->mNumAnimations;i++) {
  142. aiAnimation *a = scene->mAnimations[i];
  143. if(listOnly) {
  144. printf("%s%s.anim\n", prefix.c_str(), a->mName.data);
  145. } else {
  146. printf("Importing '%s' (%d tracks)\n", a->mName.data, a->mNumChannels);
  147. }
  148. IAnimation *anim = new IAnimation();
  149. anim->tps = a->mTicksPerSecond;
  150. anim->name = a->mName.data;
  151. anim->numTracks = a->mNumChannels;
  152. anim->length = a->mDuration/a->mTicksPerSecond;
  153. for(int c=0; c < a->mNumChannels; c++) {
  154. aiNodeAnim *nodeAnim = a->mChannels[c];
  155. ITrack *track = new ITrack();
  156. track->nodeAnim = nodeAnim;
  157. anim->tracks.push_back(track);
  158. }
  159. skeleton->addAnimation(anim);
  160. }
  161. } else {
  162. printf("No animations in file...\n");
  163. }
  164. if(!listOnly) {
  165. skeleton->saveToFile(prefix.c_str(), swapZY);
  166. }
  167. } else {
  168. if(!listOnly) {
  169. printf("No weight data, skipping skeleton export...\n");
  170. }
  171. }
  172. if(mesh) {
  173. delete mesh;
  174. }
  175. return 1;
  176. }
  177. int main(int argc, char **argv) {
  178. bool argsValid = true;
  179. bool showHelp = false;
  180. bool swapZYAxis = false;
  181. bool generateTangents = false;
  182. bool addSubmeshes = false;
  183. bool listOnly = false;
  184. bool showAssimpDebug = false;
  185. String prefix;
  186. int opt;
  187. while ((opt = getopt(argc, argv, "adlhp:st")) != -1) {
  188. switch ((char)opt) {
  189. case 's':
  190. swapZYAxis = true;
  191. break;
  192. case 't':
  193. generateTangents = true;
  194. break;
  195. case 'a':
  196. addSubmeshes = true;
  197. break;
  198. case 'd':
  199. showAssimpDebug = true;
  200. break;
  201. case 'l':
  202. listOnly = true;
  203. break;
  204. case 'p':
  205. prefix = String(optarg);
  206. break;
  207. case 'h':
  208. showHelp = true;
  209. break;
  210. default:
  211. argsValid = false;
  212. break;
  213. }
  214. }
  215. if(listOnly && argc < 3) {
  216. argsValid = false;
  217. }
  218. if(!listOnly) {
  219. printf("Polycode import tool v"POLYCODE_VERSION_STRING"\n");
  220. }
  221. if(!argsValid) {
  222. printf("Invalid arguments! Run with -h to see available options.\n\n");
  223. return 0;
  224. }
  225. if(showHelp) {
  226. printf("usage: polyimport [-adhlst] [-p output_prefix] source_file\n\n");
  227. printf("a: Add all meshes to a single mesh.\n");
  228. printf("d: Show Assimp debug info.\n");
  229. printf("h: Show this help.\n");
  230. printf("l: List output files, but do not convert.\n");
  231. printf("p: Specify a file prefix for exported files.\n");
  232. printf("s: Swap Z/Y axis (e.g. import from Blender)\n");
  233. printf("t: Generate tangents.\n");
  234. printf("\n");
  235. return 0;
  236. }
  237. PHYSFS_init(argv[0]);
  238. if(showAssimpDebug) {
  239. struct aiLogStream stream;
  240. stream = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT,NULL);
  241. aiAttachLogStream(&stream);
  242. }
  243. if(argc < 2) {
  244. printf("Please specify input filename\n");
  245. return 0;
  246. }
  247. int inputArg = argc-1;
  248. if(!listOnly) {
  249. printf("Loading %s...\n", argv[inputArg]);
  250. }
  251. scene = aiImportFile(argv[inputArg],aiProcessPreset_TargetRealtime_Quality);
  252. if(generateTangents && !listOnly) {
  253. aiApplyPostProcessing(scene, aiProcess_CalcTangentSpace);
  254. }
  255. if(scene) {
  256. exportToFile(prefix, swapZYAxis, addSubmeshes, listOnly);
  257. } else {
  258. printf("Error opening scene...\n");
  259. }
  260. aiReleaseImport(scene);
  261. return 1;
  262. }