polyimport.cpp 5.1 KB

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