Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

1403 Zeilen
40 KiB

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