polyimport.cpp 9.9 KB

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