polyimport.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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(Polycode::Mesh *tmesh, const struct aiScene *sc, const struct aiNode* nd, bool swapZY) {
  19. int i;
  20. unsigned int n = 0, t;
  21. // draw all meshes assigned to this node
  22. for (; n < nd->mNumMeshes; ++n) {
  23. const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
  24. printf("Importing mesh:%s\n", mesh->mName.data);
  25. //apply_material(sc->mMaterials[mesh->mMaterialIndex]);
  26. for (t = 0; t < mesh->mNumFaces; ++t) {
  27. const struct aiFace* face = &mesh->mFaces[t];
  28. Polycode::Polygon *poly = new Polycode::Polygon();
  29. for(i = 0; i < face->mNumIndices; i++) {
  30. Vertex *vertex = new Vertex();
  31. int index = face->mIndices[i];
  32. if(mesh->mColors[0] != NULL) {
  33. vertex->vertexColor.setColorRGBA(mesh->mColors[0][index].r, mesh->mColors[0][index].g, mesh->mColors[0][index].b, mesh->mColors[0][index].a);
  34. }
  35. if(mesh->mNormals != NULL) {
  36. if(swapZY)
  37. vertex->setNormal(mesh->mNormals[index].x, mesh->mNormals[index].z, -mesh->mNormals[index].y);
  38. else
  39. vertex->setNormal(mesh->mNormals[index].x, mesh->mNormals[index].y, mesh->mNormals[index].z);
  40. }
  41. if(mesh->HasTextureCoords(0))
  42. {
  43. vertex->setTexCoord(mesh->mTextureCoords[0][index].x, mesh->mTextureCoords[0][index].y);
  44. }
  45. for( unsigned int a = 0; a < mesh->mNumBones; a++) {
  46. aiBone* bone = mesh->mBones[a];
  47. unsigned int boneIndex = addBone(bone);
  48. for( unsigned int b = 0; b < bone->mNumWeights; b++) {
  49. if(bone->mWeights[b].mVertexId == index) {
  50. vertex->addBoneAssignment(boneIndex, bone->mWeights[b].mWeight);
  51. hasWeights = true;
  52. }
  53. }
  54. }
  55. if(swapZY)
  56. vertex->set(mesh->mVertices[index].x, mesh->mVertices[index].z, -mesh->mVertices[index].y);
  57. else
  58. vertex->set(mesh->mVertices[index].x, mesh->mVertices[index].y, mesh->mVertices[index].z);
  59. poly->addVertex(vertex);
  60. }
  61. tmesh->addPolygon(poly);
  62. }
  63. }
  64. // draw all children
  65. for (n = 0; n < nd->mNumChildren; ++n) {
  66. addToMesh(tmesh, sc, nd->mChildren[n], swapZY);
  67. }
  68. }
  69. int getBoneID(aiString name) {
  70. for(int i=0; i < bones.size(); i++) {
  71. if(bones[i]->mName == name) {
  72. return i;
  73. }
  74. }
  75. return 666;
  76. }
  77. void addToISkeleton(ISkeleton *skel, IBone *parent, const struct aiScene *sc, const struct aiNode* nd) {
  78. IBone *bone = new IBone();
  79. bone->parent = parent;
  80. bone->name = nd->mName;
  81. bone->t = nd->mTransformation;
  82. for(int i=0; i < bones.size(); i++) {
  83. if(bones[i]->mName == bone->name) {
  84. bone->bindMatrix = bones[i]->mOffsetMatrix;
  85. }
  86. }
  87. for (int n = 0; n < nd->mNumChildren; ++n) {
  88. addToISkeleton(skel, bone, sc, nd->mChildren[n]);
  89. }
  90. skel->addIBone(bone, getBoneID(bone->name));
  91. }
  92. int exportToFile(const char *fileName, bool swapZY) {
  93. String fileNameMesh = String(fileName)+".mesh";
  94. OSFILE *outFile = OSBasics::open(fileNameMesh.c_str(), "wb");
  95. Polycode::Mesh *mesh = new Polycode::Mesh(Mesh::TRI_MESH);
  96. addToMesh(mesh, scene, scene->mRootNode, swapZY);
  97. mesh->saveToFile(outFile);
  98. OSBasics::close(outFile);
  99. if(hasWeights) {
  100. printf("Mesh has weights, exporting skeleton...\n");
  101. String fileNameSkel = String(fileName)+".skeleton";
  102. ISkeleton *skeleton = new ISkeleton();
  103. for (int n = 0; n < scene->mRootNode->mNumChildren; ++n) {
  104. if(scene->mRootNode->mChildren[n]->mNumChildren > 0) {
  105. addToISkeleton(skeleton, NULL, scene, scene->mRootNode->mChildren[n]);
  106. }
  107. }
  108. if(scene->HasAnimations()) {
  109. printf("Importing animations...\n");
  110. for(int i=0; i < scene->mNumAnimations;i++) {
  111. aiAnimation *a = scene->mAnimations[i];
  112. printf("Importing '%s' (%d tracks)\n", a->mName.data, a->mNumChannels);
  113. IAnimation *anim = new IAnimation();
  114. anim->tps = a->mTicksPerSecond;
  115. anim->name = a->mName.data;
  116. anim->numTracks = a->mNumChannels;
  117. anim->length = a->mDuration/a->mTicksPerSecond;
  118. for(int c=0; c < a->mNumChannels; c++) {
  119. aiNodeAnim *nodeAnim = a->mChannels[c];
  120. ITrack *track = new ITrack();
  121. track->nodeAnim = nodeAnim;
  122. anim->tracks.push_back(track);
  123. }
  124. skeleton->addAnimation(anim);
  125. }
  126. } else {
  127. printf("No animations in file...\n");
  128. }
  129. skeleton->saveToFile(fileName, swapZY);
  130. } else {
  131. printf("No weight data, skipping skeleton export...\n");
  132. }
  133. delete mesh;
  134. return 1;
  135. }
  136. int main(int argc, char **argv) {
  137. printf("Polycode import tool v0.8.2\n");
  138. if(argc != 4) {
  139. printf("\n\nInvalid arguments!\n");
  140. printf("usage: polyimport <source_file> <output_file> (Swap Z/Y:<true>/<false>) \n\n");
  141. return 0;
  142. }
  143. PHYSFS_init(argv[0]);
  144. struct aiLogStream stream;
  145. stream = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT,NULL);
  146. aiAttachLogStream(&stream);
  147. printf("Loading %s...\n", argv[1]);
  148. scene = aiImportFile(argv[1],aiProcessPreset_TargetRealtime_Quality);
  149. if(scene) {
  150. exportToFile(argv[2], strcmp(argv[3], "true") == 0);
  151. } else {
  152. printf("Error opening scene...\n");
  153. }
  154. aiReleaseImport(scene);
  155. return 1;
  156. }