3DSLoader.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file 3DSLoader.cpp
  35. * @brief Implementation of the 3ds importer class
  36. *
  37. * http://www.the-labs.com/Blender/3DS-details.html
  38. */
  39. #ifndef ASSIMP_BUILD_NO_3DS_IMPORTER
  40. #include "3DSLoader.h"
  41. #include <assimp/StringComparison.h>
  42. #include <assimp/importerdesc.h>
  43. #include <assimp/scene.h>
  44. #include <assimp/DefaultLogger.hpp>
  45. #include <assimp/IOSystem.hpp>
  46. namespace Assimp {
  47. static constexpr aiImporterDesc desc = {
  48. "Discreet 3DS Importer",
  49. "",
  50. "",
  51. "Limited animation support",
  52. aiImporterFlags_SupportBinaryFlavour,
  53. 0,
  54. 0,
  55. 0,
  56. 0,
  57. "3ds prj"
  58. };
  59. // ------------------------------------------------------------------------------------------------
  60. // Begins a new parsing block
  61. // - Reads the current chunk and validates it
  62. // - computes its length
  63. #define ASSIMP_3DS_BEGIN_CHUNK() \
  64. while (true) { \
  65. if (stream->GetRemainingSizeToLimit() < sizeof(Discreet3DS::Chunk)) { \
  66. return; \
  67. } \
  68. Discreet3DS::Chunk chunk; \
  69. ReadChunk(&chunk); \
  70. int chunkSize = chunk.Size - sizeof(Discreet3DS::Chunk); \
  71. if (chunkSize <= 0) \
  72. continue; \
  73. const unsigned int oldReadLimit = stream->SetReadLimit( \
  74. stream->GetCurrentPos() + chunkSize);
  75. // ------------------------------------------------------------------------------------------------
  76. // End a parsing block
  77. // Must follow at the end of each parsing block, reset chunk end marker to previous value
  78. #define ASSIMP_3DS_END_CHUNK() \
  79. stream->SkipToReadLimit(); \
  80. stream->SetReadLimit(oldReadLimit); \
  81. if (stream->GetRemainingSizeToLimit() == 0) \
  82. return; \
  83. }
  84. // ------------------------------------------------------------------------------------------------
  85. // Constructor to be privately used by Importer
  86. Discreet3DSImporter::Discreet3DSImporter() :
  87. stream(), mLastNodeIndex(), mCurrentNode(), mRootNode(), mScene(), mMasterScale(), bHasBG(), bIsPrj() {
  88. // empty
  89. }
  90. // ------------------------------------------------------------------------------------------------
  91. // Returns whether the class can handle the format of the given file.
  92. bool Discreet3DSImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const {
  93. static const uint16_t token[] = { 0x4d4d, 0x3dc2 /*, 0x3daa */ };
  94. return CheckMagicToken(pIOHandler, pFile, token, AI_COUNT_OF(token), 0, sizeof token[0]);
  95. }
  96. // ------------------------------------------------------------------------------------------------
  97. // Loader registry entry
  98. const aiImporterDesc *Discreet3DSImporter::GetInfo() const {
  99. return &desc;
  100. }
  101. // ------------------------------------------------------------------------------------------------
  102. // Setup configuration properties
  103. void Discreet3DSImporter::SetupProperties(const Importer * /*pImp*/) {
  104. // nothing to be done for the moment
  105. }
  106. // ------------------------------------------------------------------------------------------------
  107. // Imports the given file into the given scene structure.
  108. void Discreet3DSImporter::InternReadFile(const std::string &pFile,
  109. aiScene *pScene, IOSystem *pIOHandler) {
  110. auto theFile = pIOHandler->Open(pFile, "rb");
  111. if (!theFile) {
  112. throw DeadlyImportError("3DS: Could not open ", pFile);
  113. }
  114. StreamReaderLE theStream(theFile);
  115. // We should have at least one chunk
  116. if (theStream.GetRemainingSize() < 16) {
  117. throw DeadlyImportError("3DS file is either empty or corrupt: ", pFile);
  118. }
  119. this->stream = &theStream;
  120. // Allocate our temporary 3DS representation
  121. D3DS::Scene _scene;
  122. mScene = &_scene;
  123. // Initialize members
  124. D3DS::Node _rootNode("UNNAMED");
  125. mLastNodeIndex = -1;
  126. mCurrentNode = &_rootNode;
  127. mRootNode = mCurrentNode;
  128. mRootNode->mHierarchyPos = -1;
  129. mRootNode->mHierarchyIndex = -1;
  130. mRootNode->mParent = nullptr;
  131. mMasterScale = 1.0f;
  132. mBackgroundImage = std::string();
  133. bHasBG = false;
  134. bIsPrj = false;
  135. // Parse the file
  136. ParseMainChunk();
  137. // Process all meshes in the file. First check whether all
  138. // face indices have valid values. The generate our
  139. // internal verbose representation. Finally compute normal
  140. // vectors from the smoothing groups we read from the
  141. // file.
  142. for (auto &mesh : mScene->mMeshes) {
  143. if (mesh.mFaces.size() > 0 && mesh.mPositions.size() == 0) {
  144. throw DeadlyImportError("3DS file contains faces but no vertices: ", pFile);
  145. }
  146. CheckIndices(mesh);
  147. MakeUnique(mesh);
  148. ComputeNormalsWithSmoothingsGroups<D3DS::Face>(mesh);
  149. }
  150. // Replace all occurrences of the default material with a
  151. // valid material. Generate it if no material containing
  152. // DEFAULT in its name has been found in the file
  153. ReplaceDefaultMaterial();
  154. // Convert the scene from our internal representation to an
  155. // aiScene object. This involves copying all meshes, lights
  156. // and cameras to the scene
  157. ConvertScene(pScene);
  158. // Generate the node graph for the scene. This is a little bit
  159. // tricky since we'll need to split some meshes into sub-meshes
  160. GenerateNodeGraph(pScene);
  161. // Now apply the master scaling factor to the scene
  162. ApplyMasterScale(pScene);
  163. // Our internal scene representation and the root
  164. // node will be automatically deleted, so the whole hierarchy will follow
  165. AI_DEBUG_INVALIDATE_PTR(mRootNode);
  166. AI_DEBUG_INVALIDATE_PTR(mScene);
  167. AI_DEBUG_INVALIDATE_PTR(this->stream);
  168. }
  169. // ------------------------------------------------------------------------------------------------
  170. // Applies a master-scaling factor to the imported scene
  171. void Discreet3DSImporter::ApplyMasterScale(aiScene *pScene) {
  172. // There are some 3DS files with a zero scaling factor
  173. if (!mMasterScale)
  174. mMasterScale = 1.0f;
  175. else
  176. mMasterScale = 1.0f / mMasterScale;
  177. // Construct an uniform scaling matrix and multiply with it
  178. pScene->mRootNode->mTransformation *= aiMatrix4x4(
  179. mMasterScale, 0.0f, 0.0f, 0.0f,
  180. 0.0f, mMasterScale, 0.0f, 0.0f,
  181. 0.0f, 0.0f, mMasterScale, 0.0f,
  182. 0.0f, 0.0f, 0.0f, 1.0f);
  183. // Check whether a scaling track is assigned to the root node.
  184. }
  185. // ------------------------------------------------------------------------------------------------
  186. // Reads a new chunk from the file
  187. void Discreet3DSImporter::ReadChunk(Discreet3DS::Chunk *pcOut) {
  188. ai_assert(pcOut != nullptr);
  189. pcOut->Flag = stream->GetI2();
  190. pcOut->Size = stream->GetI4();
  191. if (pcOut->Size - sizeof(Discreet3DS::Chunk) > stream->GetRemainingSize()) {
  192. throw DeadlyImportError("Chunk is too large");
  193. }
  194. if (pcOut->Size - sizeof(Discreet3DS::Chunk) > stream->GetRemainingSizeToLimit()) {
  195. ASSIMP_LOG_ERROR("3DS: Chunk overflow");
  196. }
  197. }
  198. // ------------------------------------------------------------------------------------------------
  199. // Skip a chunk
  200. void Discreet3DSImporter::SkipChunk() {
  201. Discreet3DS::Chunk psChunk;
  202. ReadChunk(&psChunk);
  203. stream->IncPtr(psChunk.Size - sizeof(Discreet3DS::Chunk));
  204. return;
  205. }
  206. // ------------------------------------------------------------------------------------------------
  207. // Process the primary chunk of the file
  208. void Discreet3DSImporter::ParseMainChunk() {
  209. ASSIMP_3DS_BEGIN_CHUNK();
  210. // get chunk type
  211. switch (chunk.Flag) {
  212. case Discreet3DS::CHUNK_PRJ:
  213. bIsPrj = true;
  214. break;
  215. case Discreet3DS::CHUNK_MAIN:
  216. ParseEditorChunk();
  217. break;
  218. };
  219. ASSIMP_3DS_END_CHUNK();
  220. #if defined(__clang__)
  221. #pragma clang diagnostic push
  222. #pragma clang diagnostic ignored "-Wunreachable-code-return"
  223. #endif
  224. // recursively continue processing this hierarchy level
  225. return ParseMainChunk();
  226. #if defined(__clang__)
  227. #pragma clang diagnostic pop
  228. #endif
  229. }
  230. // ------------------------------------------------------------------------------------------------
  231. void Discreet3DSImporter::ParseEditorChunk() {
  232. ASSIMP_3DS_BEGIN_CHUNK();
  233. // get chunk type
  234. switch (chunk.Flag) {
  235. case Discreet3DS::CHUNK_OBJMESH:
  236. ParseObjectChunk();
  237. break;
  238. // NOTE: In several documentations in the internet this
  239. // chunk appears at different locations
  240. case Discreet3DS::CHUNK_KEYFRAMER:
  241. ParseKeyframeChunk();
  242. break;
  243. case Discreet3DS::CHUNK_VERSION: {
  244. // print the version number
  245. char buff[10];
  246. ASSIMP_itoa10(buff, stream->GetI2());
  247. ASSIMP_LOG_INFO("3DS file format version: ", buff);
  248. } break;
  249. };
  250. ASSIMP_3DS_END_CHUNK();
  251. }
  252. // ------------------------------------------------------------------------------------------------
  253. void Discreet3DSImporter::ParseObjectChunk() {
  254. ASSIMP_3DS_BEGIN_CHUNK();
  255. // get chunk type
  256. switch (chunk.Flag) {
  257. case Discreet3DS::CHUNK_OBJBLOCK: {
  258. unsigned int cnt = 0;
  259. const char *sz = (const char *)stream->GetPtr();
  260. // Get the name of the geometry object
  261. while (stream->GetI1())
  262. ++cnt;
  263. ParseChunk(sz, cnt);
  264. } break;
  265. case Discreet3DS::CHUNK_MAT_MATERIAL:
  266. // Add a new material to the list
  267. mScene->mMaterials.emplace_back(std::string("UNNAMED_" + ai_to_string(mScene->mMaterials.size())));
  268. ParseMaterialChunk();
  269. break;
  270. case Discreet3DS::CHUNK_AMBCOLOR:
  271. // This is the ambient base color of the scene.
  272. // We add it to the ambient color of all materials
  273. ParseColorChunk(&mClrAmbient, true);
  274. if (is_qnan(mClrAmbient.r)) {
  275. // We failed to read the ambient base color.
  276. ASSIMP_LOG_ERROR("3DS: Failed to read ambient base color");
  277. mClrAmbient.r = mClrAmbient.g = mClrAmbient.b = 0.0f;
  278. }
  279. break;
  280. case Discreet3DS::CHUNK_BIT_MAP: {
  281. // Specifies the background image. The string should already be
  282. // properly 0 terminated but we need to be sure
  283. unsigned int cnt = 0;
  284. const char *sz = (const char *)stream->GetPtr();
  285. while (stream->GetI1())
  286. ++cnt;
  287. mBackgroundImage = std::string(sz, cnt);
  288. } break;
  289. case Discreet3DS::CHUNK_BIT_MAP_EXISTS:
  290. bHasBG = true;
  291. break;
  292. case Discreet3DS::CHUNK_MASTER_SCALE:
  293. // Scene master scaling factor
  294. mMasterScale = stream->GetF4();
  295. break;
  296. };
  297. ASSIMP_3DS_END_CHUNK();
  298. }
  299. // ------------------------------------------------------------------------------------------------
  300. void Discreet3DSImporter::ParseChunk(const char *name, unsigned int num) {
  301. ASSIMP_3DS_BEGIN_CHUNK();
  302. // IMPLEMENTATION NOTE;
  303. // Cameras or lights define their transformation in their parent node and in the
  304. // corresponding light or camera chunks. However, we read and process the latter
  305. // to be able to return valid cameras/lights even if no scenegraph is given.
  306. // get chunk type
  307. switch (chunk.Flag) {
  308. case Discreet3DS::CHUNK_TRIMESH: {
  309. // this starts a new triangle mesh
  310. mScene->mMeshes.emplace_back(std::string(name, num));
  311. // Read mesh chunks
  312. ParseMeshChunk();
  313. } break;
  314. case Discreet3DS::CHUNK_LIGHT: {
  315. // This starts a new light
  316. aiLight *light = new aiLight();
  317. mScene->mLights.push_back(light);
  318. light->mName.Set(std::string(name, num));
  319. // First read the position of the light
  320. light->mPosition.x = stream->GetF4();
  321. light->mPosition.y = stream->GetF4();
  322. light->mPosition.z = stream->GetF4();
  323. light->mColorDiffuse = aiColor3D(1.f, 1.f, 1.f);
  324. // Now check for further subchunks
  325. if (!bIsPrj) /* fixme */
  326. ParseLightChunk();
  327. // The specular light color is identical the the diffuse light color. The ambient light color
  328. // is equal to the ambient base color of the whole scene.
  329. light->mColorSpecular = light->mColorDiffuse;
  330. light->mColorAmbient = mClrAmbient;
  331. if (light->mType == aiLightSource_UNDEFINED) {
  332. // It must be a point light
  333. light->mType = aiLightSource_POINT;
  334. }
  335. } break;
  336. case Discreet3DS::CHUNK_CAMERA: {
  337. // This starts a new camera
  338. aiCamera *camera = new aiCamera();
  339. mScene->mCameras.push_back(camera);
  340. camera->mName.Set(std::string(name, num));
  341. // First read the position of the camera
  342. camera->mPosition.x = stream->GetF4();
  343. camera->mPosition.y = stream->GetF4();
  344. camera->mPosition.z = stream->GetF4();
  345. // Then the camera target
  346. camera->mLookAt.x = stream->GetF4() - camera->mPosition.x;
  347. camera->mLookAt.y = stream->GetF4() - camera->mPosition.y;
  348. camera->mLookAt.z = stream->GetF4() - camera->mPosition.z;
  349. ai_real len = camera->mLookAt.Length();
  350. if (len < 1e-5) {
  351. // There are some files with lookat == position. Don't know why or whether it's ok or not.
  352. ASSIMP_LOG_ERROR("3DS: Unable to read proper camera look-at vector");
  353. camera->mLookAt = aiVector3D(0.0, 1.0, 0.0);
  354. } else
  355. camera->mLookAt /= len;
  356. // And finally - the camera rotation angle, in counter clockwise direction
  357. const ai_real angle = AI_DEG_TO_RAD(stream->GetF4());
  358. aiQuaternion quat(camera->mLookAt, angle);
  359. camera->mUp = quat.GetMatrix() * aiVector3D(0.0, 1.0, 0.0);
  360. // Read the lense angle
  361. camera->mHorizontalFOV = AI_DEG_TO_RAD(stream->GetF4());
  362. if (camera->mHorizontalFOV < 0.001f) {
  363. camera->mHorizontalFOV = float(AI_DEG_TO_RAD(45.f));
  364. }
  365. // Now check for further subchunks
  366. if (!bIsPrj) /* fixme */ {
  367. ParseCameraChunk();
  368. }
  369. } break;
  370. };
  371. ASSIMP_3DS_END_CHUNK();
  372. }
  373. // ------------------------------------------------------------------------------------------------
  374. void Discreet3DSImporter::ParseLightChunk() {
  375. ASSIMP_3DS_BEGIN_CHUNK();
  376. aiLight *light = mScene->mLights.back();
  377. // get chunk type
  378. switch (chunk.Flag) {
  379. case Discreet3DS::CHUNK_DL_SPOTLIGHT:
  380. // Now we can be sure that the light is a spot light
  381. light->mType = aiLightSource_SPOT;
  382. // We wouldn't need to normalize here, but we do it
  383. light->mDirection.x = stream->GetF4() - light->mPosition.x;
  384. light->mDirection.y = stream->GetF4() - light->mPosition.y;
  385. light->mDirection.z = stream->GetF4() - light->mPosition.z;
  386. light->mDirection.Normalize();
  387. // Now the hotspot and falloff angles - in degrees
  388. light->mAngleInnerCone = AI_DEG_TO_RAD(stream->GetF4());
  389. // FIX: the falloff angle is just an offset
  390. light->mAngleOuterCone = light->mAngleInnerCone + AI_DEG_TO_RAD(stream->GetF4());
  391. break;
  392. // intensity multiplier
  393. case Discreet3DS::CHUNK_DL_MULTIPLIER:
  394. light->mColorDiffuse = light->mColorDiffuse * stream->GetF4();
  395. break;
  396. // light color
  397. case Discreet3DS::CHUNK_RGBF:
  398. case Discreet3DS::CHUNK_LINRGBF:
  399. light->mColorDiffuse.r *= stream->GetF4();
  400. light->mColorDiffuse.g *= stream->GetF4();
  401. light->mColorDiffuse.b *= stream->GetF4();
  402. break;
  403. // light attenuation
  404. case Discreet3DS::CHUNK_DL_ATTENUATE:
  405. light->mAttenuationLinear = stream->GetF4();
  406. break;
  407. };
  408. ASSIMP_3DS_END_CHUNK();
  409. }
  410. // ------------------------------------------------------------------------------------------------
  411. void Discreet3DSImporter::ParseCameraChunk() {
  412. ASSIMP_3DS_BEGIN_CHUNK();
  413. aiCamera *camera = mScene->mCameras.back();
  414. // get chunk type
  415. switch (chunk.Flag) {
  416. // near and far clip plane
  417. case Discreet3DS::CHUNK_CAM_RANGES:
  418. camera->mClipPlaneNear = stream->GetF4();
  419. camera->mClipPlaneFar = stream->GetF4();
  420. break;
  421. }
  422. ASSIMP_3DS_END_CHUNK();
  423. }
  424. // ------------------------------------------------------------------------------------------------
  425. void Discreet3DSImporter::ParseKeyframeChunk() {
  426. ASSIMP_3DS_BEGIN_CHUNK();
  427. // get chunk type
  428. switch (chunk.Flag) {
  429. case Discreet3DS::CHUNK_TRACKCAMTGT:
  430. case Discreet3DS::CHUNK_TRACKSPOTL:
  431. case Discreet3DS::CHUNK_TRACKCAMERA:
  432. case Discreet3DS::CHUNK_TRACKINFO:
  433. case Discreet3DS::CHUNK_TRACKLIGHT:
  434. case Discreet3DS::CHUNK_TRACKLIGTGT:
  435. // this starts a new mesh hierarchy chunk
  436. ParseHierarchyChunk(chunk.Flag);
  437. break;
  438. };
  439. ASSIMP_3DS_END_CHUNK();
  440. }
  441. // ------------------------------------------------------------------------------------------------
  442. // Little helper function for ParseHierarchyChunk
  443. void Discreet3DSImporter::InverseNodeSearch(D3DS::Node *pcNode, D3DS::Node *pcCurrent) {
  444. if (!pcCurrent) {
  445. mRootNode->push_back(pcNode);
  446. return;
  447. }
  448. if (pcCurrent->mHierarchyPos == pcNode->mHierarchyPos) {
  449. if (pcCurrent->mParent) {
  450. pcCurrent->mParent->push_back(pcNode);
  451. } else
  452. pcCurrent->push_back(pcNode);
  453. return;
  454. }
  455. return InverseNodeSearch(pcNode, pcCurrent->mParent);
  456. }
  457. // ------------------------------------------------------------------------------------------------
  458. // Find a node with a specific name in the import hierarchy
  459. D3DS::Node *FindNode(D3DS::Node *root, const std::string &name) {
  460. if (root->mName == name) {
  461. return root;
  462. }
  463. for (std::vector<D3DS::Node *>::iterator it = root->mChildren.begin(); it != root->mChildren.end(); ++it) {
  464. D3DS::Node *nd = FindNode(*it, name);
  465. if (nullptr != nd) {
  466. return nd;
  467. }
  468. }
  469. return nullptr;
  470. }
  471. // ------------------------------------------------------------------------------------------------
  472. // Binary predicate for std::unique()
  473. template <class T>
  474. bool KeyUniqueCompare(const T &first, const T &second) {
  475. return first.mTime == second.mTime;
  476. }
  477. // ------------------------------------------------------------------------------------------------
  478. // Skip some additional import data.
  479. void Discreet3DSImporter::SkipTCBInfo() {
  480. unsigned int flags = stream->GetI2();
  481. if (!flags) {
  482. // Currently we can't do anything with these values. They occur
  483. // quite rare, so it wouldn't be worth the effort implementing
  484. // them. 3DS is not really suitable for complex animations,
  485. // so full support is not required.
  486. ASSIMP_LOG_WARN("3DS: Skipping TCB animation info");
  487. }
  488. if (flags & Discreet3DS::KEY_USE_TENS) {
  489. stream->IncPtr(4);
  490. }
  491. if (flags & Discreet3DS::KEY_USE_BIAS) {
  492. stream->IncPtr(4);
  493. }
  494. if (flags & Discreet3DS::KEY_USE_CONT) {
  495. stream->IncPtr(4);
  496. }
  497. if (flags & Discreet3DS::KEY_USE_EASE_FROM) {
  498. stream->IncPtr(4);
  499. }
  500. if (flags & Discreet3DS::KEY_USE_EASE_TO) {
  501. stream->IncPtr(4);
  502. }
  503. }
  504. // ------------------------------------------------------------------------------------------------
  505. // Read hierarchy and keyframe info
  506. void Discreet3DSImporter::ParseHierarchyChunk(uint16_t parent) {
  507. ASSIMP_3DS_BEGIN_CHUNK();
  508. // get chunk type
  509. switch (chunk.Flag) {
  510. case Discreet3DS::CHUNK_TRACKOBJNAME:
  511. // This is the name of the object to which the track applies. The chunk also
  512. // defines the position of this object in the hierarchy.
  513. {
  514. // First of all: get the name of the object
  515. unsigned int cnt = 0;
  516. const char *sz = (const char *)stream->GetPtr();
  517. while (stream->GetI1())
  518. ++cnt;
  519. std::string name = std::string(sz, cnt);
  520. // Now find out whether we have this node already (target animation channels
  521. // are stored with a separate object ID)
  522. D3DS::Node *pcNode = FindNode(mRootNode, name);
  523. int instanceNumber = 1;
  524. if (pcNode) {
  525. // if the source is not a CHUNK_TRACKINFO block it won't be an object instance
  526. if (parent != Discreet3DS::CHUNK_TRACKINFO) {
  527. mCurrentNode = pcNode;
  528. break;
  529. }
  530. pcNode->mInstanceCount++;
  531. instanceNumber = pcNode->mInstanceCount;
  532. }
  533. pcNode = new D3DS::Node(name);
  534. pcNode->mInstanceNumber = instanceNumber;
  535. // There are two unknown values which we can safely ignore
  536. stream->IncPtr(4);
  537. // Now read the hierarchy position of the object
  538. uint16_t hierarchy = stream->GetI2() + 1;
  539. pcNode->mHierarchyPos = hierarchy;
  540. pcNode->mHierarchyIndex = mLastNodeIndex;
  541. // And find a proper position in the graph for it
  542. if (mCurrentNode && mCurrentNode->mHierarchyPos == hierarchy) {
  543. // add to the parent of the last touched node
  544. mCurrentNode->mParent->push_back(pcNode);
  545. mLastNodeIndex++;
  546. } else if (hierarchy >= mLastNodeIndex) {
  547. // place it at the current position in the hierarchy
  548. mCurrentNode->push_back(pcNode);
  549. mLastNodeIndex = hierarchy;
  550. } else {
  551. // need to go back to the specified position in the hierarchy.
  552. InverseNodeSearch(pcNode, mCurrentNode);
  553. mLastNodeIndex++;
  554. }
  555. // Make this node the current node
  556. mCurrentNode = pcNode;
  557. }
  558. break;
  559. case Discreet3DS::CHUNK_TRACKDUMMYOBJNAME:
  560. // This is the "real" name of a $$$DUMMY object
  561. {
  562. const char *sz = (const char *)stream->GetPtr();
  563. while (stream->GetI1())
  564. ;
  565. // If object name is DUMMY, take this one instead
  566. if (mCurrentNode->mName == "$$$DUMMY") {
  567. mCurrentNode->mName = std::string(sz);
  568. break;
  569. }
  570. }
  571. break;
  572. case Discreet3DS::CHUNK_TRACKPIVOT:
  573. if (Discreet3DS::CHUNK_TRACKINFO != parent) {
  574. ASSIMP_LOG_WARN("3DS: Skipping pivot subchunk for non usual object");
  575. break;
  576. }
  577. // Pivot = origin of rotation and scaling
  578. mCurrentNode->vPivot.x = stream->GetF4();
  579. mCurrentNode->vPivot.y = stream->GetF4();
  580. mCurrentNode->vPivot.z = stream->GetF4();
  581. break;
  582. // ////////////////////////////////////////////////////////////////////
  583. // POSITION KEYFRAME
  584. case Discreet3DS::CHUNK_TRACKPOS: {
  585. stream->IncPtr(10);
  586. const unsigned int numFrames = stream->GetI4();
  587. bool sortKeys = false;
  588. // This could also be meant as the target position for
  589. // (targeted) lights and cameras
  590. std::vector<aiVectorKey> *l;
  591. if (Discreet3DS::CHUNK_TRACKCAMTGT == parent || Discreet3DS::CHUNK_TRACKLIGTGT == parent) {
  592. l = &mCurrentNode->aTargetPositionKeys;
  593. } else
  594. l = &mCurrentNode->aPositionKeys;
  595. l->reserve(numFrames);
  596. for (unsigned int i = 0; i < numFrames; ++i) {
  597. const unsigned int fidx = stream->GetI4();
  598. // Setup a new position key
  599. aiVectorKey v;
  600. v.mTime = (double)fidx;
  601. SkipTCBInfo();
  602. v.mValue.x = stream->GetF4();
  603. v.mValue.y = stream->GetF4();
  604. v.mValue.z = stream->GetF4();
  605. // check whether we'll need to sort the keys
  606. if (!l->empty() && v.mTime <= l->back().mTime)
  607. sortKeys = true;
  608. // Add the new keyframe to the list
  609. l->push_back(v);
  610. }
  611. // Sort all keys with ascending time values and remove duplicates?
  612. if (sortKeys) {
  613. std::stable_sort(l->begin(), l->end());
  614. l->erase(std::unique(l->begin(), l->end(), &KeyUniqueCompare<aiVectorKey>), l->end());
  615. }
  616. }
  617. break;
  618. // ////////////////////////////////////////////////////////////////////
  619. // CAMERA ROLL KEYFRAME
  620. case Discreet3DS::CHUNK_TRACKROLL: {
  621. // roll keys are accepted for cameras only
  622. if (parent != Discreet3DS::CHUNK_TRACKCAMERA) {
  623. ASSIMP_LOG_WARN("3DS: Ignoring roll track for non-camera object");
  624. break;
  625. }
  626. bool sortKeys = false;
  627. std::vector<aiFloatKey> *l = &mCurrentNode->aCameraRollKeys;
  628. stream->IncPtr(10);
  629. const unsigned int numFrames = stream->GetI4();
  630. l->reserve(numFrames);
  631. for (unsigned int i = 0; i < numFrames; ++i) {
  632. const unsigned int fidx = stream->GetI4();
  633. // Setup a new position key
  634. aiFloatKey v;
  635. v.mTime = (double)fidx;
  636. // This is just a single float
  637. SkipTCBInfo();
  638. v.mValue = stream->GetF4();
  639. // Check whether we'll need to sort the keys
  640. if (!l->empty() && v.mTime <= l->back().mTime)
  641. sortKeys = true;
  642. // Add the new keyframe to the list
  643. l->push_back(v);
  644. }
  645. // Sort all keys with ascending time values and remove duplicates?
  646. if (sortKeys) {
  647. std::stable_sort(l->begin(), l->end());
  648. l->erase(std::unique(l->begin(), l->end(), &KeyUniqueCompare<aiFloatKey>), l->end());
  649. }
  650. } break;
  651. // ////////////////////////////////////////////////////////////////////
  652. // CAMERA FOV KEYFRAME
  653. case Discreet3DS::CHUNK_TRACKFOV: {
  654. ASSIMP_LOG_ERROR("3DS: Skipping FOV animation track. "
  655. "This is not supported");
  656. } break;
  657. // ////////////////////////////////////////////////////////////////////
  658. // ROTATION KEYFRAME
  659. case Discreet3DS::CHUNK_TRACKROTATE: {
  660. stream->IncPtr(10);
  661. const unsigned int numFrames = stream->GetI4();
  662. bool sortKeys = false;
  663. std::vector<aiQuatKey> *l = &mCurrentNode->aRotationKeys;
  664. l->reserve(numFrames);
  665. for (unsigned int i = 0; i < numFrames; ++i) {
  666. const unsigned int fidx = stream->GetI4();
  667. SkipTCBInfo();
  668. aiQuatKey v;
  669. v.mTime = (double)fidx;
  670. // The rotation keyframe is given as an axis-angle pair
  671. const float rad = stream->GetF4();
  672. aiVector3D axis;
  673. axis.x = stream->GetF4();
  674. axis.y = stream->GetF4();
  675. axis.z = stream->GetF4();
  676. if (!axis.x && !axis.y && !axis.z)
  677. axis.y = 1.f;
  678. // Construct a rotation quaternion from the axis-angle pair
  679. v.mValue = aiQuaternion(axis, rad);
  680. // Check whether we'll need to sort the keys
  681. if (!l->empty() && v.mTime <= l->back().mTime)
  682. sortKeys = true;
  683. // add the new keyframe to the list
  684. l->push_back(v);
  685. }
  686. // Sort all keys with ascending time values and remove duplicates?
  687. if (sortKeys) {
  688. std::stable_sort(l->begin(), l->end());
  689. l->erase(std::unique(l->begin(), l->end(), &KeyUniqueCompare<aiQuatKey>), l->end());
  690. }
  691. } break;
  692. // ////////////////////////////////////////////////////////////////////
  693. // SCALING KEYFRAME
  694. case Discreet3DS::CHUNK_TRACKSCALE: {
  695. stream->IncPtr(10);
  696. const unsigned int numFrames = stream->GetI2();
  697. stream->IncPtr(2);
  698. bool sortKeys = false;
  699. std::vector<aiVectorKey> *l = &mCurrentNode->aScalingKeys;
  700. l->reserve(numFrames);
  701. for (unsigned int i = 0; i < numFrames; ++i) {
  702. const unsigned int fidx = stream->GetI4();
  703. SkipTCBInfo();
  704. // Setup a new key
  705. aiVectorKey v;
  706. v.mTime = (double)fidx;
  707. // ... and read its value
  708. v.mValue.x = stream->GetF4();
  709. v.mValue.y = stream->GetF4();
  710. v.mValue.z = stream->GetF4();
  711. // check whether we'll need to sort the keys
  712. if (!l->empty() && v.mTime <= l->back().mTime)
  713. sortKeys = true;
  714. // Remove zero-scalings on singular axes - they've been reported to be there erroneously in some strange files
  715. if (!v.mValue.x) v.mValue.x = 1.f;
  716. if (!v.mValue.y) v.mValue.y = 1.f;
  717. if (!v.mValue.z) v.mValue.z = 1.f;
  718. l->push_back(v);
  719. }
  720. // Sort all keys with ascending time values and remove duplicates?
  721. if (sortKeys) {
  722. std::stable_sort(l->begin(), l->end());
  723. l->erase(std::unique(l->begin(), l->end(), &KeyUniqueCompare<aiVectorKey>), l->end());
  724. }
  725. } break;
  726. };
  727. ASSIMP_3DS_END_CHUNK();
  728. }
  729. // ------------------------------------------------------------------------------------------------
  730. // Read a face chunk - it contains smoothing groups and material assignments
  731. void Discreet3DSImporter::ParseFaceChunk() {
  732. ASSIMP_3DS_BEGIN_CHUNK();
  733. // Get the mesh we're currently working on
  734. D3DS::Mesh &mMesh = mScene->mMeshes.back();
  735. // Get chunk type
  736. switch (chunk.Flag) {
  737. case Discreet3DS::CHUNK_SMOOLIST: {
  738. // This is the list of smoothing groups - a bitfield for every face.
  739. // Up to 32 smoothing groups assigned to a single face.
  740. unsigned int num = chunkSize / 4, m = 0;
  741. if (num > mMesh.mFaces.size()) {
  742. throw DeadlyImportError("3DS: More smoothing groups than faces");
  743. }
  744. for (std::vector<D3DS::Face>::iterator i = mMesh.mFaces.begin(); m != num; ++i, ++m) {
  745. // nth bit is set for nth smoothing group
  746. (*i).iSmoothGroup = stream->GetI4();
  747. }
  748. } break;
  749. case Discreet3DS::CHUNK_FACEMAT: {
  750. // at fist an asciiz with the material name
  751. const char *sz = (const char *)stream->GetPtr();
  752. while (stream->GetI1())
  753. ;
  754. // find the index of the material
  755. unsigned int idx = 0xcdcdcdcd, cnt = 0;
  756. for (std::vector<D3DS::Material>::const_iterator i = mScene->mMaterials.begin(); i != mScene->mMaterials.end(); ++i, ++cnt) {
  757. // use case independent comparisons. hopefully it will work.
  758. if ((*i).mName.length() && !ASSIMP_stricmp(sz, (*i).mName.c_str())) {
  759. idx = cnt;
  760. break;
  761. }
  762. }
  763. if (0xcdcdcdcd == idx) {
  764. ASSIMP_LOG_ERROR("3DS: Unknown material: ", sz);
  765. }
  766. // Now continue and read all material indices
  767. cnt = (uint16_t)stream->GetI2();
  768. for (unsigned int i = 0; i < cnt; ++i) {
  769. unsigned int fidx = (uint16_t)stream->GetI2();
  770. // check range
  771. if (fidx >= mMesh.mFaceMaterials.size()) {
  772. ASSIMP_LOG_ERROR("3DS: Invalid face index in face material list");
  773. } else
  774. mMesh.mFaceMaterials[fidx] = idx;
  775. }
  776. } break;
  777. };
  778. ASSIMP_3DS_END_CHUNK();
  779. }
  780. // ------------------------------------------------------------------------------------------------
  781. // Read a mesh chunk. Here's the actual mesh data
  782. void Discreet3DSImporter::ParseMeshChunk() {
  783. ASSIMP_3DS_BEGIN_CHUNK();
  784. // Get the mesh we're currently working on
  785. D3DS::Mesh &mMesh = mScene->mMeshes.back();
  786. // get chunk type
  787. switch (chunk.Flag) {
  788. case Discreet3DS::CHUNK_VERTLIST: {
  789. // This is the list of all vertices in the current mesh
  790. int num = (int)(uint16_t)stream->GetI2();
  791. mMesh.mPositions.reserve(num);
  792. while (num-- > 0) {
  793. aiVector3D v;
  794. v.x = stream->GetF4();
  795. v.y = stream->GetF4();
  796. v.z = stream->GetF4();
  797. mMesh.mPositions.push_back(v);
  798. }
  799. } break;
  800. case Discreet3DS::CHUNK_TRMATRIX: {
  801. // This is the RLEATIVE transformation matrix of the current mesh. Vertices are
  802. // pretransformed by this matrix wonder.
  803. mMesh.mMat.a1 = stream->GetF4();
  804. mMesh.mMat.b1 = stream->GetF4();
  805. mMesh.mMat.c1 = stream->GetF4();
  806. mMesh.mMat.a2 = stream->GetF4();
  807. mMesh.mMat.b2 = stream->GetF4();
  808. mMesh.mMat.c2 = stream->GetF4();
  809. mMesh.mMat.a3 = stream->GetF4();
  810. mMesh.mMat.b3 = stream->GetF4();
  811. mMesh.mMat.c3 = stream->GetF4();
  812. mMesh.mMat.a4 = stream->GetF4();
  813. mMesh.mMat.b4 = stream->GetF4();
  814. mMesh.mMat.c4 = stream->GetF4();
  815. } break;
  816. case Discreet3DS::CHUNK_MAPLIST: {
  817. // This is the list of all UV coords in the current mesh
  818. int num = (int)(uint16_t)stream->GetI2();
  819. mMesh.mTexCoords.reserve(num);
  820. while (num-- > 0) {
  821. aiVector3D v;
  822. v.x = stream->GetF4();
  823. v.y = stream->GetF4();
  824. mMesh.mTexCoords.push_back(v);
  825. }
  826. } break;
  827. case Discreet3DS::CHUNK_FACELIST: {
  828. // This is the list of all faces in the current mesh
  829. int num = (int)(uint16_t)stream->GetI2();
  830. mMesh.mFaces.reserve(num);
  831. while (num-- > 0) {
  832. // 3DS faces are ALWAYS triangles
  833. mMesh.mFaces.emplace_back();
  834. D3DS::Face &sFace = mMesh.mFaces.back();
  835. sFace.mIndices[0] = (uint16_t)stream->GetI2();
  836. sFace.mIndices[1] = (uint16_t)stream->GetI2();
  837. sFace.mIndices[2] = (uint16_t)stream->GetI2();
  838. stream->IncPtr(2); // skip edge visibility flag
  839. }
  840. // Resize the material array (0xcdcdcdcd marks the default material; so if a face is
  841. // not referenced by a material, $$DEFAULT will be assigned to it)
  842. mMesh.mFaceMaterials.resize(mMesh.mFaces.size(), 0xcdcdcdcd);
  843. // Larger 3DS files could have multiple FACE chunks here
  844. chunkSize = (int)stream->GetRemainingSizeToLimit();
  845. if (chunkSize > (int)sizeof(Discreet3DS::Chunk))
  846. ParseFaceChunk();
  847. } break;
  848. };
  849. ASSIMP_3DS_END_CHUNK();
  850. }
  851. // ------------------------------------------------------------------------------------------------
  852. // Read a 3DS material chunk
  853. void Discreet3DSImporter::ParseMaterialChunk() {
  854. ASSIMP_3DS_BEGIN_CHUNK();
  855. switch (chunk.Flag) {
  856. case Discreet3DS::CHUNK_MAT_MATNAME:
  857. {
  858. // The material name string is already zero-terminated, but we need to be sure ...
  859. const char *sz = (const char *)stream->GetPtr();
  860. unsigned int cnt = 0;
  861. while (stream->GetI1())
  862. ++cnt;
  863. if (!cnt) {
  864. // This may not be, we use the default name instead
  865. ASSIMP_LOG_ERROR("3DS: Empty material name");
  866. } else
  867. mScene->mMaterials.back().mName = std::string(sz, cnt);
  868. } break;
  869. case Discreet3DS::CHUNK_MAT_DIFFUSE: {
  870. // This is the diffuse material color
  871. aiColor3D *pc = &mScene->mMaterials.back().mDiffuse;
  872. ParseColorChunk(pc);
  873. if (is_qnan(pc->r)) {
  874. // color chunk is invalid. Simply ignore it
  875. ASSIMP_LOG_ERROR("3DS: Unable to read DIFFUSE chunk");
  876. pc->r = pc->g = pc->b = 1.0f;
  877. }
  878. } break;
  879. case Discreet3DS::CHUNK_MAT_SPECULAR: {
  880. // This is the specular material color
  881. aiColor3D *pc = &mScene->mMaterials.back().mSpecular;
  882. ParseColorChunk(pc);
  883. if (is_qnan(pc->r)) {
  884. // color chunk is invalid. Simply ignore it
  885. ASSIMP_LOG_ERROR("3DS: Unable to read SPECULAR chunk");
  886. pc->r = pc->g = pc->b = 1.0f;
  887. }
  888. } break;
  889. case Discreet3DS::CHUNK_MAT_AMBIENT: {
  890. // This is the ambient material color
  891. aiColor3D *pc = &mScene->mMaterials.back().mAmbient;
  892. ParseColorChunk(pc);
  893. if (is_qnan(pc->r)) {
  894. // color chunk is invalid. Simply ignore it
  895. ASSIMP_LOG_ERROR("3DS: Unable to read AMBIENT chunk");
  896. pc->r = pc->g = pc->b = 0.0f;
  897. }
  898. } break;
  899. case Discreet3DS::CHUNK_MAT_SELF_ILLUM: {
  900. // This is the emissive material color
  901. aiColor3D *pc = &mScene->mMaterials.back().mEmissive;
  902. ParseColorChunk(pc);
  903. if (is_qnan(pc->r)) {
  904. // color chunk is invalid. Simply ignore it
  905. ASSIMP_LOG_ERROR("3DS: Unable to read EMISSIVE chunk");
  906. pc->r = pc->g = pc->b = 0.0f;
  907. }
  908. } break;
  909. case Discreet3DS::CHUNK_MAT_TRANSPARENCY: {
  910. // This is the material's transparency
  911. ai_real *pcf = &mScene->mMaterials.back().mTransparency;
  912. *pcf = ParsePercentageChunk();
  913. // NOTE: transparency, not opacity
  914. if (is_qnan(*pcf))
  915. *pcf = ai_real(1.0);
  916. else
  917. *pcf = ai_real(1.0) - *pcf * (ai_real)0xFFFF / ai_real(100.0);
  918. } break;
  919. case Discreet3DS::CHUNK_MAT_SHADING:
  920. // This is the material shading mode
  921. mScene->mMaterials.back().mShading = (D3DS::Discreet3DS::shadetype3ds)stream->GetI2();
  922. break;
  923. case Discreet3DS::CHUNK_MAT_TWO_SIDE:
  924. // This is the two-sided flag
  925. mScene->mMaterials.back().mTwoSided = true;
  926. break;
  927. case Discreet3DS::CHUNK_MAT_SHININESS: { // This is the shininess of the material
  928. ai_real *pcf = &mScene->mMaterials.back().mSpecularExponent;
  929. *pcf = ParsePercentageChunk();
  930. if (is_qnan(*pcf))
  931. *pcf = 0.0;
  932. else
  933. *pcf *= (ai_real)0xFFFF;
  934. } break;
  935. case Discreet3DS::CHUNK_MAT_SHININESS_PERCENT: { // This is the shininess strength of the material
  936. ai_real *pcf = &mScene->mMaterials.back().mShininessStrength;
  937. *pcf = ParsePercentageChunk();
  938. if (is_qnan(*pcf))
  939. *pcf = ai_real(0.0);
  940. else
  941. *pcf *= (ai_real)0xffff / ai_real(100.0);
  942. } break;
  943. case Discreet3DS::CHUNK_MAT_SELF_ILPCT: { // This is the self illumination strength of the material
  944. ai_real f = ParsePercentageChunk();
  945. if (is_qnan(f))
  946. f = ai_real(0.0);
  947. else
  948. f *= (ai_real)0xFFFF / ai_real(100.0);
  949. mScene->mMaterials.back().mEmissive = aiColor3D(f, f, f);
  950. } break;
  951. // Parse texture chunks
  952. case Discreet3DS::CHUNK_MAT_TEXTURE:
  953. // Diffuse texture
  954. ParseTextureChunk(&mScene->mMaterials.back().sTexDiffuse);
  955. break;
  956. case Discreet3DS::CHUNK_MAT_BUMPMAP:
  957. // Height map
  958. ParseTextureChunk(&mScene->mMaterials.back().sTexBump);
  959. break;
  960. case Discreet3DS::CHUNK_MAT_OPACMAP:
  961. // Opacity texture
  962. ParseTextureChunk(&mScene->mMaterials.back().sTexOpacity);
  963. break;
  964. case Discreet3DS::CHUNK_MAT_MAT_SHINMAP:
  965. // Shininess map
  966. ParseTextureChunk(&mScene->mMaterials.back().sTexShininess);
  967. break;
  968. case Discreet3DS::CHUNK_MAT_SPECMAP:
  969. // Specular map
  970. ParseTextureChunk(&mScene->mMaterials.back().sTexSpecular);
  971. break;
  972. case Discreet3DS::CHUNK_MAT_SELFIMAP:
  973. // Self-illumination (emissive) map
  974. ParseTextureChunk(&mScene->mMaterials.back().sTexEmissive);
  975. break;
  976. case Discreet3DS::CHUNK_MAT_REFLMAP:
  977. // Reflection map
  978. ParseTextureChunk(&mScene->mMaterials.back().sTexReflective);
  979. break;
  980. };
  981. ASSIMP_3DS_END_CHUNK();
  982. }
  983. // ------------------------------------------------------------------------------------------------
  984. void Discreet3DSImporter::ParseTextureChunk(D3DS::Texture *pcOut) {
  985. ASSIMP_3DS_BEGIN_CHUNK();
  986. // get chunk type
  987. switch (chunk.Flag) {
  988. case Discreet3DS::CHUNK_MAPFILE: {
  989. // The material name string is already zero-terminated, but we need to be sure ...
  990. const char *sz = (const char *)stream->GetPtr();
  991. unsigned int cnt = 0;
  992. while (stream->GetI1())
  993. ++cnt;
  994. pcOut->mMapName = std::string(sz, cnt);
  995. } break;
  996. case Discreet3DS::CHUNK_PERCENTD:
  997. // Manually parse the blend factor
  998. pcOut->mTextureBlend = ai_real(stream->GetF8());
  999. break;
  1000. case Discreet3DS::CHUNK_PERCENTF:
  1001. // Manually parse the blend factor
  1002. pcOut->mTextureBlend = stream->GetF4();
  1003. break;
  1004. case Discreet3DS::CHUNK_PERCENTW:
  1005. // Manually parse the blend factor
  1006. pcOut->mTextureBlend = (ai_real)((uint16_t)stream->GetI2()) / ai_real(100.0);
  1007. break;
  1008. case Discreet3DS::CHUNK_MAT_MAP_USCALE:
  1009. // Texture coordinate scaling in the U direction
  1010. pcOut->mScaleU = stream->GetF4();
  1011. if (0.0f == pcOut->mScaleU) {
  1012. ASSIMP_LOG_WARN("Texture coordinate scaling in the x direction is zero. Assuming 1.");
  1013. pcOut->mScaleU = 1.0f;
  1014. }
  1015. break;
  1016. case Discreet3DS::CHUNK_MAT_MAP_VSCALE:
  1017. // Texture coordinate scaling in the V direction
  1018. pcOut->mScaleV = stream->GetF4();
  1019. if (0.0f == pcOut->mScaleV) {
  1020. ASSIMP_LOG_WARN("Texture coordinate scaling in the y direction is zero. Assuming 1.");
  1021. pcOut->mScaleV = 1.0f;
  1022. }
  1023. break;
  1024. case Discreet3DS::CHUNK_MAT_MAP_UOFFSET:
  1025. // Texture coordinate offset in the U direction
  1026. pcOut->mOffsetU = -stream->GetF4();
  1027. break;
  1028. case Discreet3DS::CHUNK_MAT_MAP_VOFFSET:
  1029. // Texture coordinate offset in the V direction
  1030. pcOut->mOffsetV = stream->GetF4();
  1031. break;
  1032. case Discreet3DS::CHUNK_MAT_MAP_ANG:
  1033. // Texture coordinate rotation, CCW in DEGREES
  1034. pcOut->mRotation = -AI_DEG_TO_RAD(stream->GetF4());
  1035. break;
  1036. case Discreet3DS::CHUNK_MAT_MAP_TILING: {
  1037. const uint16_t iFlags = stream->GetI2();
  1038. // Get the mapping mode (for both axes)
  1039. if (iFlags & 0x2u)
  1040. pcOut->mMapMode = aiTextureMapMode_Mirror;
  1041. else if (iFlags & 0x10u)
  1042. pcOut->mMapMode = aiTextureMapMode_Decal;
  1043. // wrapping in all remaining cases
  1044. else
  1045. pcOut->mMapMode = aiTextureMapMode_Wrap;
  1046. } break;
  1047. };
  1048. ASSIMP_3DS_END_CHUNK();
  1049. }
  1050. // ------------------------------------------------------------------------------------------------
  1051. // Read a percentage chunk
  1052. ai_real Discreet3DSImporter::ParsePercentageChunk() {
  1053. Discreet3DS::Chunk chunk;
  1054. ReadChunk(&chunk);
  1055. if (Discreet3DS::CHUNK_PERCENTF == chunk.Flag) {
  1056. return stream->GetF4() * ai_real(100) / ai_real(0xFFFF);
  1057. } else if (Discreet3DS::CHUNK_PERCENTW == chunk.Flag) {
  1058. return (ai_real)((uint16_t)stream->GetI2()) / (ai_real)0xFFFF;
  1059. }
  1060. return get_qnan();
  1061. }
  1062. // ------------------------------------------------------------------------------------------------
  1063. // Read a color chunk. If a percentage chunk is found instead it is read as a grayscale color
  1064. void Discreet3DSImporter::ParseColorChunk(aiColor3D *out, bool acceptPercent) {
  1065. ai_assert(out != nullptr);
  1066. // error return value
  1067. const ai_real qnan = get_qnan();
  1068. static const aiColor3D clrError = aiColor3D(qnan, qnan, qnan);
  1069. Discreet3DS::Chunk chunk;
  1070. ReadChunk(&chunk);
  1071. const unsigned int diff = chunk.Size - sizeof(Discreet3DS::Chunk);
  1072. bool bGamma = false;
  1073. // Get the type of the chunk
  1074. switch (chunk.Flag) {
  1075. case Discreet3DS::CHUNK_LINRGBF:
  1076. bGamma = true;
  1077. // fallthrough
  1078. case Discreet3DS::CHUNK_RGBF:
  1079. if (sizeof(float) * 3 > diff) {
  1080. *out = clrError;
  1081. return;
  1082. }
  1083. out->r = stream->GetF4();
  1084. out->g = stream->GetF4();
  1085. out->b = stream->GetF4();
  1086. break;
  1087. case Discreet3DS::CHUNK_LINRGBB:
  1088. bGamma = true;
  1089. // fallthrough
  1090. case Discreet3DS::CHUNK_RGBB: {
  1091. if (sizeof(char) * 3 > diff) {
  1092. *out = clrError;
  1093. return;
  1094. }
  1095. const ai_real invVal = ai_real(1.0) / ai_real(255.0);
  1096. out->r = (ai_real)(uint8_t)stream->GetI1() * invVal;
  1097. out->g = (ai_real)(uint8_t)stream->GetI1() * invVal;
  1098. out->b = (ai_real)(uint8_t)stream->GetI1() * invVal;
  1099. } break;
  1100. // Percentage chunks are accepted, too.
  1101. case Discreet3DS::CHUNK_PERCENTF:
  1102. if (acceptPercent && 4 <= diff) {
  1103. out->g = out->b = out->r = stream->GetF4();
  1104. break;
  1105. }
  1106. *out = clrError;
  1107. return;
  1108. case Discreet3DS::CHUNK_PERCENTW:
  1109. if (acceptPercent && 1 <= diff) {
  1110. out->g = out->b = out->r = (ai_real)(uint8_t)stream->GetI1() / ai_real(255.0);
  1111. break;
  1112. }
  1113. *out = clrError;
  1114. return;
  1115. default:
  1116. stream->IncPtr(diff);
  1117. // Skip unknown chunks, hope this won't cause any problems.
  1118. return ParseColorChunk(out, acceptPercent);
  1119. };
  1120. (void)bGamma;
  1121. }
  1122. } // namespace Assimp
  1123. #endif // !! ASSIMP_BUILD_NO_3DS_IMPORTER