Model.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #include "Common.h"
  2. #include <cassert>
  3. //==============================================================================
  4. static const aiMesh& getMesh(const Exporter& exporter, unsigned index)
  5. {
  6. assert(index < exporter.scene->mNumMeshes);
  7. return *exporter.scene->mMeshes[index];
  8. }
  9. //==============================================================================
  10. static const aiMaterial& getMaterial(const Exporter& exporter, unsigned index)
  11. {
  12. assert(index < exporter.scene->mNumMaterials);
  13. return *exporter.scene->mMaterials[index];
  14. }
  15. //==============================================================================
  16. static std::string getMeshName(const aiMesh& mesh)
  17. {
  18. return std::string(mesh.mName.C_Str());
  19. }
  20. //==============================================================================
  21. static std::string getMaterialName(const aiMaterial& mtl, bool instanced)
  22. {
  23. aiString ainame;
  24. std::string name;
  25. if(mtl.Get(AI_MATKEY_NAME, ainame) == AI_SUCCESS)
  26. {
  27. name = ainame.C_Str();
  28. if(instanced)
  29. {
  30. name += "_inst";
  31. }
  32. }
  33. else
  34. {
  35. ERROR("Material's name is missing\n");
  36. }
  37. return name;
  38. }
  39. //==============================================================================
  40. std::string getModelName(const Exporter& exporter, const Model& model)
  41. {
  42. std::string name =
  43. getMeshName(getMesh(exporter, model.meshIndex)) + "_"
  44. + getMaterialName(getMaterial(exporter, model.mtlIndex),
  45. model.instanced);
  46. return name;
  47. }
  48. //==============================================================================
  49. void exportMesh(
  50. const Exporter& exporter,
  51. const aiMesh& mesh,
  52. const aiMatrix4x4* transform)
  53. {
  54. std::string name = getMeshName(mesh);
  55. std::fstream file;
  56. LOGI("Exporting mesh %s\n", name.c_str());
  57. uint32_t vertsCount = mesh.mNumVertices;
  58. // Open file
  59. file.open(exporter.outDir + name + ".ankimesh",
  60. std::ios::out | std::ios::binary);
  61. // Write magic word
  62. file.write("ANKIMESH", 8);
  63. // Write the name
  64. uint32_t size = name.size();
  65. file.write((char*)&size, sizeof(uint32_t));
  66. file.write(&name[0], size);
  67. // Write positions
  68. file.write((char*)&vertsCount, sizeof(uint32_t));
  69. for(uint32_t i = 0; i < mesh.mNumVertices; i++)
  70. {
  71. aiVector3D pos = mesh.mVertices[i];
  72. // Transform
  73. if(transform)
  74. {
  75. pos = (*transform) * pos;
  76. }
  77. // flip
  78. if(exporter.flipyz)
  79. {
  80. static const aiMatrix4x4 toLefthanded(
  81. 1, 0, 0, 0,
  82. 0, 0, 1, 0,
  83. 0, -1, 0, 0,
  84. 0, 0, 0, 1);
  85. pos = toLefthanded * pos;
  86. }
  87. for(uint32_t j = 0; j < 3; j++)
  88. {
  89. file.write((char*)&pos[j], sizeof(float));
  90. }
  91. }
  92. // Write the indices
  93. file.write((char*)&mesh.mNumFaces, sizeof(uint32_t));
  94. for(uint32_t i = 0; i < mesh.mNumFaces; i++)
  95. {
  96. const aiFace& face = mesh.mFaces[i];
  97. if(face.mNumIndices != 3)
  98. {
  99. ERROR("For some reason the assimp didn't triangulate\n");
  100. }
  101. for(uint32_t j = 0; j < 3; j++)
  102. {
  103. uint32_t index = face.mIndices[j];
  104. file.write((char*)&index, sizeof(uint32_t));
  105. }
  106. }
  107. // Write the tex coords
  108. file.write((char*)&vertsCount, sizeof(uint32_t));
  109. // For all channels
  110. for(uint32_t ch = 0; ch < mesh.GetNumUVChannels(); ch++)
  111. {
  112. if(mesh.mNumUVComponents[ch] != 2)
  113. {
  114. ERROR("Incorrect number of UV components\n");
  115. }
  116. // For all tex coords of this channel
  117. for(uint32_t i = 0; i < vertsCount; i++)
  118. {
  119. aiVector3D texCoord = mesh.mTextureCoords[ch][i];
  120. for(uint32_t j = 0; j < 2; j++)
  121. {
  122. file.write((char*)&texCoord[j], sizeof(float));
  123. }
  124. }
  125. }
  126. // Write bone weigths count
  127. if(mesh.HasBones())
  128. {
  129. #if 0
  130. // Write file
  131. file.write((char*)&vertsCount, sizeof(uint32_t));
  132. // Gather info for each vertex
  133. std::vector<Vw> vw;
  134. vw.resize(vertsCount);
  135. memset(&vw[0], 0, sizeof(Vw) * vertsCount);
  136. // For all bones
  137. for(uint32_t i = 0; i < mesh.mNumBones; i++)
  138. {
  139. const aiBone& bone = *mesh.mBones[i];
  140. // for every weights of the bone
  141. for(uint32_t j = 0; j < bone.mWeightsCount; j++)
  142. {
  143. const aiVertexWeight& weigth = bone.mWeights[j];
  144. // Sanity check
  145. if(weight.mVertexId >= vertCount)
  146. {
  147. ERROR("Out of bounds vert ID");
  148. }
  149. Vm& a = vm[weight.mVertexId];
  150. // Check out of bounds
  151. if(a.bonesCount >= MAX_BONES_PER_VERTEX)
  152. {
  153. LOGW("Too many bones for vertex %d\n", weigth.mVertexId);
  154. continue;
  155. }
  156. // Write to vertex
  157. a.boneIds[a.bonesCount] = i;
  158. a.weigths[a.bonesCount] = weigth.mWeigth;
  159. ++a.bonesCount;
  160. }
  161. // Now write the file
  162. }
  163. #endif
  164. }
  165. else
  166. {
  167. uint32_t num = 0;
  168. file.write((char*)&num, sizeof(uint32_t));
  169. }
  170. }
  171. //==============================================================================
  172. void exportSkeleton(const Exporter& exporter, const aiMesh& mesh)
  173. {
  174. assert(mesh.HasBones());
  175. std::string name = mesh.mName.C_Str();
  176. std::fstream file;
  177. LOGI("Exporting skeleton %s\n", name.c_str());
  178. // Open file
  179. file.open(exporter.outDir + name + ".skel", std::ios::out);
  180. file << XML_HEADER << "\n";
  181. file << "<skeleton>\n";
  182. file << "\t<bones>\n";
  183. bool rootBoneFound = false;
  184. for(uint32_t i = 0; i < mesh.mNumBones; i++)
  185. {
  186. const aiBone& bone = *mesh.mBones[i];
  187. file << "\t\t<bone>\n";
  188. // <name>
  189. file << "\t\t\t<name>" << bone.mName.C_Str() << "</name>\n";
  190. if(strcmp(bone.mName.C_Str(), "root") == 0)
  191. {
  192. rootBoneFound = true;
  193. }
  194. // <transform>
  195. file << "\t\t\t<transform>";
  196. for(uint32_t j = 0; j < 16; j++)
  197. {
  198. file << bone.mOffsetMatrix[j] << " ";
  199. }
  200. file << "</transform>\n";
  201. file << "\t\t</bone>\n";
  202. }
  203. if(!rootBoneFound)
  204. {
  205. ERROR("There should be one bone named \"root\"\n");
  206. }
  207. file << "\t</bones>\n";
  208. file << "</skeleton>\n";
  209. }
  210. //==============================================================================
  211. void exportMaterial(
  212. const Exporter& exporter,
  213. const aiMaterial& mtl,
  214. bool instanced)
  215. {
  216. std::string diffTex;
  217. std::string normTex;
  218. std::string name = getMaterialName(mtl, instanced);
  219. LOGI("Exporting material %s\n", name.c_str());
  220. // Diffuse texture
  221. if(mtl.GetTextureCount(aiTextureType_DIFFUSE) < 1)
  222. {
  223. ERROR("Material has no diffuse textures\n");
  224. }
  225. aiString path;
  226. if(mtl.GetTexture(aiTextureType_DIFFUSE, 0, &path) == AI_SUCCESS)
  227. {
  228. diffTex = getFilename(path.C_Str());
  229. }
  230. else
  231. {
  232. ERROR("Failed to retrieve texture\n");
  233. }
  234. // Normal texture
  235. if(mtl.GetTextureCount(aiTextureType_NORMALS) > 0)
  236. {
  237. if(mtl.GetTexture(aiTextureType_NORMALS, 0, &path) == AI_SUCCESS)
  238. {
  239. normTex = getFilename(path.C_Str());
  240. }
  241. else
  242. {
  243. ERROR("Failed to retrieve texture\n");
  244. }
  245. }
  246. // Write file
  247. static const char* diffMtlStr =
  248. #include "diffTemplateMtl.h"
  249. ;
  250. static const char* diffNormMtlStr =
  251. #include "diffNormTemplateMtl.h"
  252. ;
  253. std::fstream file;
  254. file.open(exporter.outDir + name + ".ankimtl", std::ios::out);
  255. // Chose the correct template
  256. std::string str;
  257. if(normTex.size() == 0)
  258. {
  259. str = diffMtlStr;
  260. }
  261. else
  262. {
  263. str = replaceAllString(diffNormMtlStr, "%normalMap%",
  264. exporter.texrpath + normTex);
  265. }
  266. aiColor3D specCol = {0.0, 0.0, 0.0};
  267. mtl.Get(AI_MATKEY_COLOR_SPECULAR, specCol);
  268. float shininess = 0.0;
  269. mtl.Get(AI_MATKEY_SHININESS, shininess);
  270. str = replaceAllString(str, "%specularColor%",
  271. std::to_string((specCol[0] + specCol[1] + specCol[2]) / 3.0));
  272. str = replaceAllString(str, "%specularPower%",
  273. std::to_string(shininess));
  274. str = replaceAllString(str, "%instanced%", (instanced) ? "1" : "0");
  275. str = replaceAllString(str, "%diffuseMap%", exporter.texrpath + diffTex);
  276. file << str;
  277. }
  278. //==============================================================================
  279. void exportModel(const Exporter& exporter, const Model& model)
  280. {
  281. std::string name = getModelName(exporter, model);
  282. LOGI("Exporting model %s\n", name.c_str());
  283. std::fstream file;
  284. file.open(exporter.outDir + name + ".ankimdl", std::ios::out);
  285. file << XML_HEADER << '\n';
  286. file << "<model>\n";
  287. file << "\t<modelPatches>\n";
  288. // start
  289. file << "\t\t<modelPatch>\n";
  290. // Write mesh
  291. file << "\t\t\t<mesh>" << exporter.rpath
  292. << getMeshName(getMesh(exporter, model.meshIndex))
  293. << ".ankimesh</mesh>\n";
  294. // Write material
  295. file << "\t\t\t<material>" << exporter.rpath
  296. << getMaterialName(getMaterial(exporter, model.mtlIndex),
  297. model.instanced)
  298. << ".ankimtl</material>\n";
  299. // end
  300. file << "\t\t</modelPatch>\n";
  301. file << "\t</modelPatches>\n";
  302. file << "</model>\n";
  303. }