polyimport.cpp 5.0 KB

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