polyimport.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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;
  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. for(i = 0; i < face->mNumIndices; i++) {
  93. int index = face->mIndices[i];
  94. tmesh->addIndex(index);
  95. }
  96. }
  97. if(!addSubmeshes && !listOnly) {
  98. String fileNameMesh = prefix+String(nd->mName.data)+".mesh";
  99. OSFILE *outFile = OSBasics::open(fileNameMesh.c_str(), "wb");
  100. tmesh->saveToFile(outFile, writeNormals, writeTangents, writeColors, writeBoneWeights, writeUVs, writeSecondaryUVs);
  101. OSBasics::close(outFile);
  102. delete tmesh;
  103. }
  104. }
  105. // draw all children
  106. for (n = 0; n < nd->mNumChildren; ++n) {
  107. addToMesh(prefix, tmesh, sc, nd->mChildren[n], swapZY, addSubmeshes, listOnly);
  108. }
  109. }
  110. int getBoneID(aiString name) {
  111. for(int i=0; i < bones.size(); i++) {
  112. if(bones[i]->mName == name) {
  113. return i;
  114. }
  115. }
  116. return 666;
  117. }
  118. void addToISkeleton(ISkeleton *skel, IBone *parent, const struct aiScene *sc, const struct aiNode* nd) {
  119. IBone *bone = new IBone();
  120. bone->parent = parent;
  121. bone->name = nd->mName;
  122. bone->t = nd->mTransformation;
  123. for(int i=0; i < bones.size(); i++) {
  124. if(bones[i]->mName == bone->name) {
  125. bone->bindMatrix = bones[i]->mOffsetMatrix;
  126. }
  127. }
  128. for (int n = 0; n < nd->mNumChildren; ++n) {
  129. addToISkeleton(skel, bone, sc, nd->mChildren[n]);
  130. }
  131. skel->addIBone(bone, getBoneID(bone->name));
  132. }
  133. int exportToFile(String prefix, bool swapZY, bool addSubmeshes, bool listOnly) {
  134. Polycode::Mesh *mesh = new Polycode::Mesh(Mesh::TRI_MESH);
  135. mesh->indexedMesh = true;
  136. addToMesh(prefix, mesh, scene, scene->mRootNode, swapZY, addSubmeshes, listOnly);
  137. if(addSubmeshes) {
  138. String fileNameMesh;
  139. if(prefix != "") {
  140. fileNameMesh = prefix+".mesh";
  141. } else {
  142. fileNameMesh = "out.mesh";
  143. }
  144. if(listOnly) {
  145. printf("%s\n", fileNameMesh.c_str());
  146. } else {
  147. OSFILE *outFile = OSBasics::open(fileNameMesh.c_str(), "wb");
  148. mesh->saveToFile(outFile, writeNormals, writeTangents, writeColors, writeBoneWeights, writeUVs, writeSecondaryUVs);
  149. OSBasics::close(outFile);
  150. }
  151. }
  152. if(hasWeights) {
  153. if(listOnly) {
  154. printf("%s.skeleton\n", prefix.c_str());
  155. } else {
  156. printf("Mesh has weights, exporting skeleton...\n");
  157. }
  158. String fileNameSkel = prefix+".skeleton";
  159. ISkeleton *skeleton = new ISkeleton();
  160. for (int n = 0; n < scene->mRootNode->mNumChildren; ++n) {
  161. if(scene->mRootNode->mChildren[n]->mNumChildren > 0) {
  162. addToISkeleton(skeleton, NULL, scene, scene->mRootNode->mChildren[n]);
  163. }
  164. }
  165. if(scene->HasAnimations()) {
  166. printf("Importing animations...\n");
  167. for(int i=0; i < scene->mNumAnimations;i++) {
  168. aiAnimation *a = scene->mAnimations[i];
  169. if(listOnly) {
  170. printf("%s%s.anim\n", prefix.c_str(), a->mName.data);
  171. } else {
  172. printf("Importing '%s' (%d tracks)\n", a->mName.data, a->mNumChannels);
  173. }
  174. IAnimation *anim = new IAnimation();
  175. anim->tps = a->mTicksPerSecond;
  176. anim->name = a->mName.data;
  177. anim->numTracks = a->mNumChannels;
  178. anim->length = a->mDuration/a->mTicksPerSecond;
  179. for(int c=0; c < a->mNumChannels; c++) {
  180. aiNodeAnim *nodeAnim = a->mChannels[c];
  181. ITrack *track = new ITrack();
  182. track->nodeAnim = nodeAnim;
  183. anim->tracks.push_back(track);
  184. }
  185. skeleton->addAnimation(anim);
  186. }
  187. } else {
  188. printf("No animations in file...\n");
  189. }
  190. if(!listOnly) {
  191. skeleton->saveToFile(prefix.c_str(), swapZY);
  192. }
  193. } else {
  194. if(!listOnly) {
  195. printf("No weight data, skipping skeleton export...\n");
  196. }
  197. }
  198. if(mesh) {
  199. delete mesh;
  200. }
  201. return 1;
  202. }
  203. int main(int argc, char **argv) {
  204. bool argsValid = true;
  205. bool showHelp = false;
  206. bool swapZYAxis = false;
  207. bool generateTangents = false;
  208. bool addSubmeshes = false;
  209. bool listOnly = false;
  210. bool showAssimpDebug = false;
  211. String prefix;
  212. int opt;
  213. while ((opt = getopt(argc, argv, "ngcwuvadlhp:st")) != -1) {
  214. switch ((char)opt) {
  215. case 'n':
  216. writeNormals = true;
  217. break;
  218. case 'g':
  219. writeTangents = true;
  220. break;
  221. case 'c':
  222. writeColors = true;
  223. break;
  224. case 'w':
  225. writeBoneWeights = true;
  226. break;
  227. case 'u':
  228. writeUVs = true;
  229. break;
  230. case 'v':
  231. writeSecondaryUVs = true;
  232. break;
  233. case 's':
  234. swapZYAxis = true;
  235. break;
  236. case 't':
  237. generateTangents = true;
  238. break;
  239. case 'a':
  240. addSubmeshes = true;
  241. break;
  242. case 'd':
  243. showAssimpDebug = true;
  244. break;
  245. case 'l':
  246. listOnly = true;
  247. break;
  248. case 'p':
  249. prefix = String(optarg);
  250. break;
  251. case 'h':
  252. showHelp = true;
  253. break;
  254. default:
  255. argsValid = false;
  256. break;
  257. }
  258. }
  259. if(listOnly && argc < 3) {
  260. argsValid = false;
  261. }
  262. if(!listOnly) {
  263. printf("Polycode import tool v"POLYCODE_VERSION_STRING"\n");
  264. }
  265. if(!argsValid) {
  266. printf("Invalid arguments! Run with -h to see available options.\n\n");
  267. return 0;
  268. }
  269. if(showHelp || argc < 2) {
  270. printf("usage: polyimport [-adhlstngcwuv] [-p output_prefix] source_file\n\n");
  271. printf("Misc options:\n");
  272. printf("d: Show Assimp debug info.\n");
  273. printf("h: Show this help.\n");
  274. printf("l: List output files, but do not convert.\n");
  275. printf("p: Specify a file prefix for exported files.\n\n");
  276. printf("Mesh import options:\n");
  277. printf("a: Add all meshes to a single mesh.\n");
  278. printf("s: Swap Z/Y axis (e.g. import from Blender)\n");
  279. printf("t: Generate tangents.\n\n");
  280. printf("Mesh export options:\n");
  281. printf("n: Export normals\n");
  282. printf("g: Export tangents\n");
  283. printf("c: Export colors\n");
  284. printf("w: Export bone weights\n");
  285. printf("u: Export UV coordinates\n");
  286. printf("v: Export secondary UV coordinates\n");
  287. printf("\n");
  288. return 0;
  289. }
  290. PHYSFS_init(argv[0]);
  291. if(showAssimpDebug) {
  292. struct aiLogStream stream;
  293. stream = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT,NULL);
  294. aiAttachLogStream(&stream);
  295. }
  296. int inputArg = argc-1;
  297. if(!listOnly) {
  298. printf("Loading %s...\n", argv[inputArg]);
  299. }
  300. scene = aiImportFile(argv[inputArg], aiProcess_JoinIdenticalVertices|
  301. aiProcess_Triangulate);
  302. if(scene) {
  303. if(generateTangents && !listOnly) {
  304. aiApplyPostProcessing(scene, aiProcess_CalcTangentSpace);
  305. }
  306. exportToFile(prefix, swapZYAxis, addSubmeshes, listOnly);
  307. } else {
  308. printf("Error opening scene...\n");
  309. }
  310. aiReleaseImport(scene);
  311. return 1;
  312. }