3DSExporter.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #ifndef ASSIMP_BUILD_NO_EXPORT
  34. #ifndef ASSIMP_BUILD_NO_3DS_EXPORTER
  35. #include "AssetLib/3DS/3DSExporter.h"
  36. #include "AssetLib/3DS/3DSHelper.h"
  37. #include "AssetLib/3DS/3DSLoader.h"
  38. #include "PostProcessing/SplitLargeMeshes.h"
  39. #include <assimp/SceneCombiner.h>
  40. #include <assimp/StringComparison.h>
  41. #include <assimp/DefaultLogger.hpp>
  42. #include <assimp/Exporter.hpp>
  43. #include <assimp/Exceptional.h>
  44. #include <assimp/IOSystem.hpp>
  45. #include <memory>
  46. namespace Assimp {
  47. using namespace D3DS;
  48. namespace {
  49. //////////////////////////////////////////////////////////////////////////////////////
  50. // Scope utility to write a 3DS file chunk.
  51. //
  52. // Upon construction, the chunk header is written with the chunk type (flags)
  53. // filled out, but the chunk size left empty. Upon destruction, the correct chunk
  54. // size based on the then-position of the output stream cursor is filled in.
  55. class ChunkWriter {
  56. enum {
  57. CHUNK_SIZE_NOT_SET = 0xdeadbeef,
  58. SIZE_OFFSET = 2
  59. };
  60. public:
  61. ChunkWriter(StreamWriterLE &writer, uint16_t chunk_type) :
  62. writer(writer) {
  63. chunk_start_pos = writer.GetCurrentPos();
  64. writer.PutU2(chunk_type);
  65. writer.PutU4((uint32_t)CHUNK_SIZE_NOT_SET);
  66. }
  67. ~ChunkWriter() {
  68. std::size_t head_pos = writer.GetCurrentPos();
  69. ai_assert(head_pos > chunk_start_pos);
  70. const std::size_t chunk_size = head_pos - chunk_start_pos;
  71. writer.SetCurrentPos(chunk_start_pos + SIZE_OFFSET);
  72. writer.PutU4(static_cast<uint32_t>(chunk_size));
  73. writer.SetCurrentPos(head_pos);
  74. }
  75. private:
  76. StreamWriterLE &writer;
  77. std::size_t chunk_start_pos;
  78. };
  79. // Return an unique name for a given |mesh| attached to |node| that
  80. // preserves the mesh's given name if it has one. |index| is the index
  81. // of the mesh in |aiScene::mMeshes|.
  82. std::string GetMeshName(const aiMesh &mesh, unsigned int index, const aiNode &node) {
  83. static constexpr char underscore = '_';
  84. char postfix[10] = { 0 };
  85. ASSIMP_itoa10(postfix, index);
  86. std::string result = node.mName.C_Str();
  87. if (mesh.mName.length > 0) {
  88. result += underscore;
  89. result += mesh.mName.C_Str();
  90. }
  91. return result + underscore + postfix;
  92. }
  93. // Return an unique name for a given |mat| with original position |index|
  94. // in |aiScene::mMaterials|. The name preserves the original material
  95. // name if possible.
  96. std::string GetMaterialName(const aiMaterial &mat, unsigned int index) {
  97. static const std::string underscore = "_";
  98. char postfix[10] = { 0 };
  99. ASSIMP_itoa10(postfix, index);
  100. aiString mat_name;
  101. if (AI_SUCCESS == mat.Get(AI_MATKEY_NAME, mat_name)) {
  102. return mat_name.C_Str() + underscore + postfix;
  103. }
  104. return "Material" + underscore + postfix;
  105. }
  106. // Collect world transformations for each node
  107. void CollectTrafos(const aiNode *node, std::map<const aiNode *, aiMatrix4x4> &trafos) {
  108. const aiMatrix4x4 &parent = node->mParent ? trafos[node->mParent] : aiMatrix4x4();
  109. trafos[node] = parent * node->mTransformation;
  110. for (unsigned int i = 0; i < node->mNumChildren; ++i) {
  111. CollectTrafos(node->mChildren[i], trafos);
  112. }
  113. }
  114. // Generate a flat list of the meshes (by index) assigned to each node
  115. void CollectMeshes(const aiNode *node, std::multimap<const aiNode *, unsigned int> &meshes) {
  116. for (unsigned int i = 0; i < node->mNumMeshes; ++i) {
  117. meshes.insert(std::make_pair(node, node->mMeshes[i]));
  118. }
  119. for (unsigned int i = 0; i < node->mNumChildren; ++i) {
  120. CollectMeshes(node->mChildren[i], meshes);
  121. }
  122. }
  123. } // namespace
  124. // ------------------------------------------------------------------------------------------------
  125. // Worker function for exporting a scene to 3DS. Prototyped and registered in Exporter.cpp
  126. void ExportScene3DS(const char *pFile, IOSystem *pIOSystem, const aiScene *pScene, const ExportProperties * /*pProperties*/) {
  127. std::shared_ptr<IOStream> outfile(pIOSystem->Open(pFile, "wb"));
  128. if (!outfile) {
  129. throw DeadlyExportError("Could not open output .3ds file: " + std::string(pFile));
  130. }
  131. // TODO: This extra copy should be avoided and all of this made a preprocess
  132. // requirement of the 3DS exporter.
  133. //
  134. // 3DS meshes can be max 0xffff (16 Bit) vertices and faces, respectively.
  135. // SplitLargeMeshes can do this, but it requires the correct limit to be set
  136. // which is not possible with the current way of specifying preprocess steps
  137. // in |Exporter::ExportFormatEntry|.
  138. aiScene *scenecopy_tmp;
  139. SceneCombiner::CopyScene(&scenecopy_tmp, pScene);
  140. std::unique_ptr<aiScene> scenecopy(scenecopy_tmp);
  141. SplitLargeMeshesProcess_Triangle tri_splitter;
  142. tri_splitter.SetLimit(0xffff);
  143. tri_splitter.Execute(scenecopy.get());
  144. SplitLargeMeshesProcess_Vertex vert_splitter;
  145. vert_splitter.SetLimit(0xffff);
  146. vert_splitter.Execute(scenecopy.get());
  147. // Invoke the actual exporter
  148. Discreet3DSExporter exporter(outfile, scenecopy.get());
  149. }
  150. } // end of namespace Assimp
  151. // ------------------------------------------------------------------------------------------------
  152. Discreet3DSExporter::Discreet3DSExporter(std::shared_ptr<IOStream> &outfile, const aiScene *scene) :
  153. scene(scene), writer(outfile) {
  154. CollectTrafos(scene->mRootNode, trafos);
  155. CollectMeshes(scene->mRootNode, meshes);
  156. ChunkWriter curRootChunk(writer, Discreet3DS::CHUNK_MAIN);
  157. {
  158. ChunkWriter curChunk(writer, Discreet3DS::CHUNK_OBJMESH);
  159. WriteMaterials();
  160. WriteMeshes();
  161. {
  162. ChunkWriter curChunk1(writer, Discreet3DS::CHUNK_MASTER_SCALE);
  163. writer.PutF4(1.0f);
  164. }
  165. }
  166. {
  167. ChunkWriter curChunk(writer, Discreet3DS::CHUNK_KEYFRAMER);
  168. WriteHierarchy(*scene->mRootNode, -1, -1);
  169. }
  170. }
  171. // ------------------------------------------------------------------------------------------------
  172. int Discreet3DSExporter::WriteHierarchy(const aiNode &node, int seq, int sibling_level) {
  173. // 3DS scene hierarchy is serialized as in http://www.martinreddy.net/gfx/3d/3DS.spec
  174. {
  175. ChunkWriter curRootChunk(writer, Discreet3DS::CHUNK_TRACKINFO);
  176. {
  177. ChunkWriter curChunk(writer, Discreet3DS::CHUNK_TRACKOBJNAME);
  178. // Assimp node names are unique and distinct from all mesh-node
  179. // names we generate; thus we can use them as-is
  180. WriteString(node.mName);
  181. // Two unknown int16 values - it is even unclear if 0 is a safe value
  182. // but luckily importers do not know better either.
  183. writer.PutI4(0);
  184. int16_t hierarchy_pos = static_cast<int16_t>(seq);
  185. if (sibling_level != -1) {
  186. hierarchy_pos = (uint16_t)sibling_level;
  187. }
  188. // Write the hierarchy position
  189. writer.PutI2(hierarchy_pos);
  190. }
  191. }
  192. // TODO: write transformation chunks
  193. ++seq;
  194. sibling_level = seq;
  195. // Write all children
  196. for (unsigned int i = 0; i < node.mNumChildren; ++i) {
  197. seq = WriteHierarchy(*node.mChildren[i], seq, i == 0 ? -1 : sibling_level);
  198. }
  199. // Write all meshes as separate nodes to be able to reference the meshes by name
  200. for (unsigned int i = 0; i < node.mNumMeshes; ++i) {
  201. const bool first_child = node.mNumChildren == 0 && i == 0;
  202. const unsigned int mesh_idx = node.mMeshes[i];
  203. const aiMesh &mesh = *scene->mMeshes[mesh_idx];
  204. ChunkWriter curChunk(writer, Discreet3DS::CHUNK_TRACKINFO);
  205. {
  206. ChunkWriter chunk(writer, Discreet3DS::CHUNK_TRACKOBJNAME);
  207. WriteString(GetMeshName(mesh, mesh_idx, node));
  208. writer.PutI4(0);
  209. writer.PutI2(static_cast<int16_t>(first_child ? seq : sibling_level));
  210. ++seq;
  211. }
  212. }
  213. return seq;
  214. }
  215. // ------------------------------------------------------------------------------------------------
  216. void Discreet3DSExporter::WriteMaterials() {
  217. for (unsigned int i = 0; i < scene->mNumMaterials; ++i) {
  218. ChunkWriter curRootChunk(writer, Discreet3DS::CHUNK_MAT_MATERIAL);
  219. const aiMaterial &mat = *scene->mMaterials[i];
  220. {
  221. ChunkWriter chunk(writer, Discreet3DS::CHUNK_MAT_MATNAME);
  222. const std::string &name = GetMaterialName(mat, i);
  223. WriteString(name);
  224. }
  225. aiColor3D color;
  226. if (mat.Get(AI_MATKEY_COLOR_DIFFUSE, color) == AI_SUCCESS) {
  227. ChunkWriter curChunk(writer, Discreet3DS::CHUNK_MAT_DIFFUSE);
  228. WriteColor(color);
  229. }
  230. if (mat.Get(AI_MATKEY_COLOR_SPECULAR, color) == AI_SUCCESS) {
  231. ChunkWriter curChunk(writer, Discreet3DS::CHUNK_MAT_SPECULAR);
  232. WriteColor(color);
  233. }
  234. if (mat.Get(AI_MATKEY_COLOR_AMBIENT, color) == AI_SUCCESS) {
  235. ChunkWriter curChunk(writer, Discreet3DS::CHUNK_MAT_AMBIENT);
  236. WriteColor(color);
  237. }
  238. float f;
  239. if (mat.Get(AI_MATKEY_OPACITY, f) == AI_SUCCESS) {
  240. ChunkWriter chunk(writer, Discreet3DS::CHUNK_MAT_TRANSPARENCY);
  241. WritePercentChunk(1.0f - f);
  242. }
  243. if (mat.Get(AI_MATKEY_COLOR_EMISSIVE, color) == AI_SUCCESS) {
  244. ChunkWriter curChunk(writer, Discreet3DS::CHUNK_MAT_SELF_ILLUM);
  245. WriteColor(color);
  246. }
  247. aiShadingMode shading_mode = aiShadingMode_Flat;
  248. if (mat.Get(AI_MATKEY_SHADING_MODEL, shading_mode) == AI_SUCCESS) {
  249. ChunkWriter chunk(writer, Discreet3DS::CHUNK_MAT_SHADING);
  250. Discreet3DS::shadetype3ds shading_mode_out;
  251. switch (shading_mode) {
  252. case aiShadingMode_Flat:
  253. case aiShadingMode_NoShading:
  254. shading_mode_out = Discreet3DS::Flat;
  255. break;
  256. case aiShadingMode_Gouraud:
  257. case aiShadingMode_Toon:
  258. case aiShadingMode_OrenNayar:
  259. case aiShadingMode_Minnaert:
  260. shading_mode_out = Discreet3DS::Gouraud;
  261. break;
  262. case aiShadingMode_Phong:
  263. case aiShadingMode_Blinn:
  264. case aiShadingMode_CookTorrance:
  265. case aiShadingMode_Fresnel:
  266. case aiShadingMode_PBR_BRDF: // Possibly should be Discreet3DS::Metal in some cases but this is undocumented
  267. shading_mode_out = Discreet3DS::Phong;
  268. break;
  269. default:
  270. shading_mode_out = Discreet3DS::Flat;
  271. ai_assert(false);
  272. };
  273. writer.PutU2(static_cast<uint16_t>(shading_mode_out));
  274. }
  275. if (mat.Get(AI_MATKEY_SHININESS, f) == AI_SUCCESS) {
  276. ChunkWriter chunk(writer, Discreet3DS::CHUNK_MAT_SHININESS);
  277. WritePercentChunk(f);
  278. }
  279. if (mat.Get(AI_MATKEY_SHININESS_STRENGTH, f) == AI_SUCCESS) {
  280. ChunkWriter chunk(writer, Discreet3DS::CHUNK_MAT_SHININESS_PERCENT);
  281. WritePercentChunk(f);
  282. }
  283. int twosided;
  284. if (mat.Get(AI_MATKEY_TWOSIDED, twosided) == AI_SUCCESS && twosided != 0) {
  285. ChunkWriter chunk(writer, Discreet3DS::CHUNK_MAT_TWO_SIDE);
  286. writer.PutI2(1);
  287. }
  288. // Fallback to BASE_COLOR if no DIFFUSE
  289. if (!WriteTexture(mat, aiTextureType_DIFFUSE, Discreet3DS::CHUNK_MAT_TEXTURE))
  290. WriteTexture(mat, aiTextureType_BASE_COLOR, Discreet3DS::CHUNK_MAT_TEXTURE);
  291. WriteTexture(mat, aiTextureType_HEIGHT, Discreet3DS::CHUNK_MAT_BUMPMAP);
  292. WriteTexture(mat, aiTextureType_OPACITY, Discreet3DS::CHUNK_MAT_OPACMAP);
  293. WriteTexture(mat, aiTextureType_SHININESS, Discreet3DS::CHUNK_MAT_MAT_SHINMAP);
  294. WriteTexture(mat, aiTextureType_SPECULAR, Discreet3DS::CHUNK_MAT_SPECMAP);
  295. WriteTexture(mat, aiTextureType_EMISSIVE, Discreet3DS::CHUNK_MAT_SELFIMAP);
  296. WriteTexture(mat, aiTextureType_REFLECTION, Discreet3DS::CHUNK_MAT_REFLMAP);
  297. }
  298. }
  299. // ------------------------------------------------------------------------------------------------
  300. // returns true if the texture existed
  301. bool Discreet3DSExporter::WriteTexture(const aiMaterial &mat, aiTextureType type, uint16_t chunk_flags) {
  302. aiString path;
  303. aiTextureMapMode map_mode[2] = {
  304. aiTextureMapMode_Wrap, aiTextureMapMode_Wrap
  305. };
  306. ai_real blend = 1.0;
  307. if (mat.GetTexture(type, 0, &path, nullptr, nullptr, &blend, nullptr, map_mode) != AI_SUCCESS || !path.length) {
  308. return false;
  309. }
  310. // TODO: handle embedded textures properly
  311. if (path.data[0] == '*') {
  312. ASSIMP_LOG_ERROR("Ignoring embedded texture for export: ", path.C_Str());
  313. return false;
  314. }
  315. ChunkWriter chunk(writer, chunk_flags);
  316. {
  317. ChunkWriter curChunk(writer, Discreet3DS::CHUNK_MAPFILE);
  318. WriteString(path);
  319. }
  320. WritePercentChunk(blend);
  321. {
  322. ChunkWriter curChunk(writer, Discreet3DS::CHUNK_MAT_MAP_TILING);
  323. uint16_t val = 0; // WRAP
  324. if (map_mode[0] == aiTextureMapMode_Mirror) {
  325. val = 0x2;
  326. } else if (map_mode[0] == aiTextureMapMode_Decal) {
  327. val = 0x10;
  328. }
  329. writer.PutU2(val);
  330. }
  331. // TODO: export texture transformation (i.e. UV offset, scale, rotation)
  332. return true;
  333. }
  334. // ------------------------------------------------------------------------------------------------
  335. void Discreet3DSExporter::WriteMeshes() {
  336. // NOTE: 3DS allows for instances. However:
  337. // i) not all importers support reading them
  338. // ii) instances are not as flexible as they are in assimp, in particular,
  339. // nodes can carry (and instance) only one mesh.
  340. //
  341. // This exporter currently deep clones all instanced meshes, i.e. for each mesh
  342. // attached to a node a full TRIMESH chunk is written to the file.
  343. //
  344. // Furthermore, the TRIMESH is transformed into world space so that it will
  345. // appear correctly if importers don't read the scene hierarchy at all.
  346. for (MeshesByNodeMap::const_iterator it = meshes.begin(); it != meshes.end(); ++it) {
  347. const aiNode &node = *(*it).first;
  348. const unsigned int mesh_idx = (*it).second;
  349. const aiMesh &mesh = *scene->mMeshes[mesh_idx];
  350. // This should not happen if the SLM step is correctly executed
  351. // before the scene is handed to the exporter
  352. ai_assert(mesh.mNumVertices <= 0xffff);
  353. ai_assert(mesh.mNumFaces <= 0xffff);
  354. const aiMatrix4x4 &trafo = trafos[&node];
  355. ChunkWriter chunk(writer, Discreet3DS::CHUNK_OBJBLOCK);
  356. // Mesh name is tied to the node it is attached to so it can later be referenced
  357. const std::string &name = GetMeshName(mesh, mesh_idx, node);
  358. WriteString(name);
  359. // TRIMESH chunk
  360. ChunkWriter chunk2(writer, Discreet3DS::CHUNK_TRIMESH);
  361. // Vertices in world space
  362. {
  363. ChunkWriter curChunk(writer, Discreet3DS::CHUNK_VERTLIST);
  364. const uint16_t count = static_cast<uint16_t>(mesh.mNumVertices);
  365. writer.PutU2(count);
  366. for (unsigned int i = 0; i < mesh.mNumVertices; ++i) {
  367. const aiVector3D &v = mesh.mVertices[i];
  368. writer.PutF4(v.x);
  369. writer.PutF4(v.y);
  370. writer.PutF4(v.z);
  371. }
  372. }
  373. // UV coordinates
  374. if (mesh.HasTextureCoords(0)) {
  375. ChunkWriter curChunk(writer, Discreet3DS::CHUNK_MAPLIST);
  376. const uint16_t count = static_cast<uint16_t>(mesh.mNumVertices);
  377. writer.PutU2(count);
  378. for (unsigned int i = 0; i < mesh.mNumVertices; ++i) {
  379. const aiVector3D &v = mesh.mTextureCoords[0][i];
  380. writer.PutF4(v.x);
  381. writer.PutF4(v.y);
  382. }
  383. }
  384. // Faces (indices)
  385. {
  386. ChunkWriter curChunk(writer, Discreet3DS::CHUNK_FACELIST);
  387. ai_assert(mesh.mNumFaces <= 0xffff);
  388. // Count triangles, discard lines and points
  389. uint16_t count = 0;
  390. for (unsigned int i = 0; i < mesh.mNumFaces; ++i) {
  391. const aiFace &f = mesh.mFaces[i];
  392. if (f.mNumIndices < 3) {
  393. continue;
  394. }
  395. // TRIANGULATE step is a pre-requisite so we should not see polys here
  396. ai_assert(f.mNumIndices == 3);
  397. ++count;
  398. }
  399. writer.PutU2(count);
  400. for (unsigned int i = 0; i < mesh.mNumFaces; ++i) {
  401. const aiFace &f = mesh.mFaces[i];
  402. if (f.mNumIndices < 3) {
  403. continue;
  404. }
  405. for (unsigned int j = 0; j < 3; ++j) {
  406. ai_assert(f.mIndices[j] <= 0xffff);
  407. writer.PutI2(static_cast<uint16_t>(f.mIndices[j]));
  408. }
  409. // Edge visibility flag
  410. writer.PutI2(0x0);
  411. }
  412. // TODO: write smoothing groups (CHUNK_SMOOLIST)
  413. WriteFaceMaterialChunk(mesh);
  414. }
  415. // Transformation matrix by which the mesh vertices have been pre-transformed with.
  416. {
  417. ChunkWriter curChunk(writer, Discreet3DS::CHUNK_TRMATRIX);
  418. // Store rotation 3x3 matrix row wise
  419. for (unsigned int r = 0; r < 3; ++r) {
  420. for (unsigned int c = 0; c < 3; ++c) {
  421. writer.PutF4(trafo[r][c]);
  422. }
  423. }
  424. // Store translation sub vector column wise
  425. for (unsigned int r = 0; r < 3; ++r) {
  426. writer.PutF4(trafo[r][3]);
  427. }
  428. }
  429. }
  430. }
  431. // ------------------------------------------------------------------------------------------------
  432. void Discreet3DSExporter::WriteFaceMaterialChunk(const aiMesh &mesh) {
  433. ChunkWriter curChunk(writer, Discreet3DS::CHUNK_FACEMAT);
  434. const std::string &name = GetMaterialName(*scene->mMaterials[mesh.mMaterialIndex], mesh.mMaterialIndex);
  435. WriteString(name);
  436. // Because assimp splits meshes by material, only a single
  437. // FACEMAT chunk needs to be written
  438. ai_assert(mesh.mNumFaces <= 0xffff);
  439. const uint16_t count = static_cast<uint16_t>(mesh.mNumFaces);
  440. writer.PutU2(count);
  441. for (unsigned int i = 0; i < mesh.mNumFaces; ++i) {
  442. writer.PutU2(static_cast<uint16_t>(i));
  443. }
  444. }
  445. // ------------------------------------------------------------------------------------------------
  446. void Discreet3DSExporter::WriteString(const std::string &s) {
  447. for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) {
  448. writer.PutI1(*it);
  449. }
  450. writer.PutI1('\0');
  451. }
  452. // ------------------------------------------------------------------------------------------------
  453. void Discreet3DSExporter::WriteString(const aiString &s) {
  454. for (std::size_t i = 0; i < s.length; ++i) {
  455. writer.PutI1(s.data[i]);
  456. }
  457. writer.PutI1('\0');
  458. }
  459. // ------------------------------------------------------------------------------------------------
  460. void Discreet3DSExporter::WriteColor(const aiColor3D &color) {
  461. ChunkWriter curChunk(writer, Discreet3DS::CHUNK_RGBF);
  462. writer.PutF4(color.r);
  463. writer.PutF4(color.g);
  464. writer.PutF4(color.b);
  465. }
  466. // ------------------------------------------------------------------------------------------------
  467. void Discreet3DSExporter::WritePercentChunk(float f) {
  468. ChunkWriter curChunk(writer, Discreet3DS::CHUNK_PERCENTF);
  469. writer.PutF4(f);
  470. }
  471. // ------------------------------------------------------------------------------------------------
  472. void Discreet3DSExporter::WritePercentChunk(double f) {
  473. ChunkWriter ccurChunkhunk(writer, Discreet3DS::CHUNK_PERCENTD);
  474. writer.PutF8(f);
  475. }
  476. #endif // ASSIMP_BUILD_NO_3DS_EXPORTER
  477. #endif // ASSIMP_BUILD_NO_EXPORT