polyimport.cpp 8.1 KB

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