polyimport.cpp 10 KB

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