polyimport.cpp 8.0 KB

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