polyimport.cpp 7.9 KB

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