polyimport.cpp 5.7 KB

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