polyimport.cpp 10.0 KB

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