3DSLoader.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 Implementation of the 3ds importer class */
  35. #include "AssimpPCH.h"
  36. // internal headers
  37. #include "3DSLoader.h"
  38. #include "MaterialSystem.h"
  39. #include "TextureTransform.h"
  40. #include "StringComparison.h"
  41. #include "qnan.h"
  42. using namespace Assimp;
  43. // begin a chunk: parse it, validate its length, get a pointer to its end
  44. #define ASSIMP_3DS_BEGIN_CHUNK() \
  45. const Dot3DSFile::Chunk* psChunk; \
  46. this->ReadChunk(&psChunk); \
  47. const unsigned char* pcCur = this->mCurrent; \
  48. const unsigned char* pcCurNext = pcCur + (psChunk->Size \
  49. - sizeof(Dot3DSFile::Chunk));
  50. // process the end of a chunk and return if the end of the file is reached
  51. #define ASSIMP_3DS_END_CHUNK() \
  52. this->mCurrent = pcCurNext; \
  53. piRemaining -= psChunk->Size; \
  54. if (0 >= piRemaining)return;
  55. // check whether the size of all subordinate chunks of a chunks is
  56. // not larger than the size of the chunk itself
  57. #ifdef _DEBUG
  58. # define ASSIMP_3DS_WARN_CHUNK_OVERFLOW_MSG \
  59. "Size of chunk data plus size of subordinate chunks is " \
  60. "larger than the size specified in the top-level chunk header."
  61. # define ASSIMP_3DS_VALIDATE_CHUNK_SIZE() \
  62. if (pcCurNext < this->mCurrent) \
  63. { \
  64. DefaultLogger::get()->warn(ASSIMP_3DS_WARN_CHUNK_OVERFLOW_MSG); \
  65. pcCurNext = this->mCurrent; \
  66. }
  67. #else
  68. # define ASSIMP_3DS_VALIDATE_CHUNK_SIZE()
  69. #endif
  70. // ------------------------------------------------------------------------------------------------
  71. // Constructor to be privately used by Importer
  72. Dot3DSImporter::Dot3DSImporter()
  73. {
  74. }
  75. // ------------------------------------------------------------------------------------------------
  76. // Destructor, private as well
  77. Dot3DSImporter::~Dot3DSImporter()
  78. {
  79. }
  80. // ------------------------------------------------------------------------------------------------
  81. // Returns whether the class can handle the format of the given file.
  82. bool Dot3DSImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  83. {
  84. // simple check of file extension is enough for the moment
  85. std::string::size_type pos = pFile.find_last_of('.');
  86. // no file extension - can't read
  87. if( pos == std::string::npos)
  88. return false;
  89. std::string extension = pFile.substr( pos);
  90. // not brillant but working ;-)
  91. if( extension == ".3ds" || extension == ".3DS" ||
  92. extension == ".3Ds" || extension == ".3dS")
  93. return true;
  94. return false;
  95. }
  96. // ------------------------------------------------------------------------------------------------
  97. // Setup configuration properties
  98. void Dot3DSImporter::SetupProperties(const Importer* pImp)
  99. {
  100. this->configSkipPivot = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_3DS_IGNORE_PIVOT,0) ? true : false;
  101. }
  102. // ------------------------------------------------------------------------------------------------
  103. // Imports the given file into the given scene structure.
  104. void Dot3DSImporter::InternReadFile(
  105. const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
  106. {
  107. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
  108. // Check whether we can read from the file
  109. if( file.get() == NULL)
  110. throw new ImportErrorException( "Failed to open 3DS file " + pFile + ".");
  111. // check whether the .3ds file is large enough to contain
  112. // at least one chunk.
  113. size_t fileSize = file->FileSize();
  114. if( fileSize < 16)
  115. throw new ImportErrorException( "3DS File is too small.");
  116. this->mScene = new Dot3DS::Scene();
  117. // allocate storage and copy the contents of the file to a memory buffer
  118. std::vector<unsigned char> mBuffer2(fileSize);
  119. file->Read( &mBuffer2[0], 1, fileSize);
  120. this->mCurrent = this->mBuffer = &mBuffer2[0];
  121. this->mLast = this->mBuffer+fileSize;
  122. // initialize members
  123. this->mLastNodeIndex = -1;
  124. this->mCurrentNode = new Dot3DS::Node();
  125. this->mRootNode = this->mCurrentNode;
  126. this->mRootNode->mHierarchyPos = -1;
  127. this->mRootNode->mHierarchyIndex = -1;
  128. this->mRootNode->mParent = NULL;
  129. this->mMasterScale = 1.0f;
  130. this->mBackgroundImage = "";
  131. this->bHasBG = false;
  132. int iRemaining = (unsigned int)fileSize;
  133. this->ParseMainChunk(iRemaining);
  134. // Generate an unique set of vertices/indices for
  135. // all meshes contained in the file
  136. for (std::vector<Dot3DS::Mesh>::iterator
  137. i = this->mScene->mMeshes.begin();
  138. i != this->mScene->mMeshes.end();++i)
  139. {
  140. // TODO: see function body
  141. this->CheckIndices(*i);
  142. this->MakeUnique(*i);
  143. // first generate normals for the mesh
  144. ComputeNormalsWithSmoothingsGroups<Dot3DS::Face>(*i);
  145. }
  146. // Apply scaling and offsets to all texture coordinates
  147. TextureTransform::ApplyScaleNOffset(this->mScene->mMaterials);
  148. // Replace all occurences of the default material with a valid material.
  149. // Generate it if no material containing DEFAULT in its name has been
  150. // found in the file
  151. this->ReplaceDefaultMaterial();
  152. // Convert the scene from our internal representation to an aiScene object
  153. this->ConvertScene(pScene);
  154. // Generate the node graph for the scene. This is a little bit
  155. // tricky since we'll need to split some meshes into submeshes
  156. this->GenerateNodeGraph(pScene);
  157. // Now apply a master scaling factor to the scene
  158. this->ApplyMasterScale(pScene);
  159. }
  160. // ------------------------------------------------------------------------------------------------
  161. void Dot3DSImporter::ApplyMasterScale(aiScene* pScene)
  162. {
  163. if (!this->mMasterScale)this->mMasterScale = 1.0f;
  164. else this->mMasterScale = 1.0f / this->mMasterScale;
  165. // construct an uniform scaling matrix and multiply with it
  166. pScene->mRootNode->mTransformation *= aiMatrix4x4(
  167. this->mMasterScale,0.0f, 0.0f, 0.0f,
  168. 0.0f, this->mMasterScale,0.0f, 0.0f,
  169. 0.0f, 0.0f, this->mMasterScale,0.0f,
  170. 0.0f, 0.0f, 0.0f, 1.0f);
  171. }
  172. // ------------------------------------------------------------------------------------------------
  173. void Dot3DSImporter::ReadChunk(const Dot3DSFile::Chunk** p_ppcOut)
  174. {
  175. ai_assert(p_ppcOut != NULL);
  176. // read chunk
  177. if (this->mCurrent >= this->mLast)
  178. throw new ImportErrorException("Unexpected end of file, can't read chunk header");
  179. const uintptr_t iDiff = this->mLast - this->mCurrent;
  180. if (iDiff < sizeof(Dot3DSFile::Chunk))
  181. {
  182. *p_ppcOut = NULL;
  183. return;
  184. }
  185. *p_ppcOut = (const Dot3DSFile::Chunk*) this->mCurrent;
  186. if ((**p_ppcOut).Size + this->mCurrent > this->mLast)
  187. throw new ImportErrorException("Unexpected end of file, can't read chunk footer");
  188. this->mCurrent += sizeof(Dot3DSFile::Chunk);
  189. return;
  190. }
  191. // ------------------------------------------------------------------------------------------------
  192. void Dot3DSImporter::ParseMainChunk(int& piRemaining)
  193. {
  194. ASSIMP_3DS_BEGIN_CHUNK();
  195. // get chunk type
  196. int iRemaining = (psChunk->Size - sizeof(Dot3DSFile::Chunk));
  197. switch (psChunk->Flag)
  198. {
  199. case Dot3DSFile::CHUNK_MAIN:
  200. this->ParseEditorChunk(iRemaining);
  201. break;
  202. };
  203. ASSIMP_3DS_VALIDATE_CHUNK_SIZE();
  204. ASSIMP_3DS_END_CHUNK();
  205. return this->ParseMainChunk(piRemaining);
  206. }
  207. // ------------------------------------------------------------------------------------------------
  208. void Dot3DSImporter::ParseEditorChunk(int& piRemaining)
  209. {
  210. ASSIMP_3DS_BEGIN_CHUNK();
  211. // get chunk type
  212. int iRemaining = (psChunk->Size - sizeof(Dot3DSFile::Chunk));
  213. switch (psChunk->Flag)
  214. {
  215. case Dot3DSFile::CHUNK_OBJMESH:
  216. this->ParseObjectChunk(iRemaining);
  217. break;
  218. // NOTE: In several documentations in the internet this
  219. // chunk appears at different locations
  220. case Dot3DSFile::CHUNK_KEYFRAMER:
  221. this->ParseKeyframeChunk(iRemaining);
  222. break;
  223. case Dot3DSFile::CHUNK_VERSION:
  224. if (psChunk->Size >= 2+sizeof(Dot3DSFile::Chunk))
  225. {
  226. // print the version number
  227. char szBuffer[128];
  228. ::sprintf(szBuffer,"3DS file version chunk: %i",
  229. (int) *((uint16_t*)this->mCurrent));
  230. DefaultLogger::get()->info(szBuffer);
  231. }
  232. else
  233. {
  234. DefaultLogger::get()->warn("Invalid version chunk in 3DS file");
  235. }
  236. break;
  237. };
  238. ASSIMP_3DS_VALIDATE_CHUNK_SIZE();
  239. ASSIMP_3DS_END_CHUNK();
  240. return this->ParseEditorChunk(piRemaining);
  241. }
  242. // ------------------------------------------------------------------------------------------------
  243. void Dot3DSImporter::ParseObjectChunk(int& piRemaining)
  244. {
  245. ASSIMP_3DS_BEGIN_CHUNK();
  246. const unsigned char* sz = this->mCurrent;
  247. unsigned int iCnt = 0;
  248. // get chunk type
  249. int iRemaining = (psChunk->Size - sizeof(Dot3DSFile::Chunk));
  250. switch (psChunk->Flag)
  251. {
  252. case Dot3DSFile::CHUNK_OBJBLOCK:
  253. this->mScene->mMeshes.push_back(Dot3DS::Mesh());
  254. // at first we need to parse the name of the
  255. // geometry object
  256. while (*sz++ != '\0')
  257. {
  258. if (sz > pcCurNext-1)break;
  259. ++iCnt;
  260. }
  261. this->mScene->mMeshes.back().mName = std::string(
  262. (const char*)this->mCurrent,iCnt);
  263. ++iCnt;
  264. this->mCurrent += iCnt;
  265. iRemaining -= iCnt;
  266. this->ParseChunk(iRemaining);
  267. break;
  268. case Dot3DSFile::CHUNK_MAT_MATERIAL:
  269. this->mScene->mMaterials.push_back(Dot3DS::Material());
  270. this->ParseMaterialChunk(iRemaining);
  271. break;
  272. case Dot3DSFile::CHUNK_AMBCOLOR:
  273. // This is the ambient base color of the scene.
  274. // We add it to the ambient color of all materials
  275. this->ParseColorChunk(&this->mClrAmbient,true);
  276. if (is_qnan(this->mClrAmbient.r))
  277. {
  278. this->mClrAmbient.r = 0.0f;
  279. this->mClrAmbient.g = 0.0f;
  280. this->mClrAmbient.b = 0.0f;
  281. }
  282. break;
  283. case Dot3DSFile::CHUNK_BIT_MAP:
  284. this->mBackgroundImage = std::string((const char*)this->mCurrent);
  285. break;
  286. case Dot3DSFile::CHUNK_BIT_MAP_EXISTS:
  287. bHasBG = true;
  288. break;
  289. case Dot3DSFile::CHUNK_MASTER_SCALE:
  290. this->mMasterScale = *((float*)this->mCurrent);
  291. this->mCurrent += sizeof(float);
  292. break;
  293. // NOTE: In several documentations in the internet this
  294. // chunk appears at different locations
  295. case Dot3DSFile::CHUNK_KEYFRAMER:
  296. this->ParseKeyframeChunk(iRemaining);
  297. break;
  298. };
  299. ASSIMP_3DS_VALIDATE_CHUNK_SIZE();
  300. ASSIMP_3DS_END_CHUNK();
  301. return this->ParseObjectChunk(piRemaining);
  302. }
  303. // ------------------------------------------------------------------------------------------------
  304. void Dot3DSImporter::SkipChunk()
  305. {
  306. const Dot3DSFile::Chunk* psChunk;
  307. this->ReadChunk(&psChunk);
  308. this->mCurrent += psChunk->Size - sizeof(Dot3DSFile::Chunk);
  309. return;
  310. }
  311. // ------------------------------------------------------------------------------------------------
  312. void Dot3DSImporter::ParseChunk(int& piRemaining)
  313. {
  314. ASSIMP_3DS_BEGIN_CHUNK();
  315. // get chunk type
  316. int iRemaining = (psChunk->Size - sizeof(Dot3DSFile::Chunk));
  317. switch (psChunk->Flag)
  318. {
  319. case Dot3DSFile::CHUNK_TRIMESH:
  320. // this starts a new mesh
  321. this->ParseMeshChunk(iRemaining);
  322. break;
  323. };
  324. ASSIMP_3DS_VALIDATE_CHUNK_SIZE();
  325. ASSIMP_3DS_END_CHUNK();
  326. return this->ParseChunk(piRemaining);
  327. }
  328. // ------------------------------------------------------------------------------------------------
  329. void Dot3DSImporter::ParseKeyframeChunk(int& piRemaining)
  330. {
  331. ASSIMP_3DS_BEGIN_CHUNK();
  332. // get chunk type
  333. int iRemaining = (psChunk->Size - sizeof(Dot3DSFile::Chunk));
  334. switch (psChunk->Flag)
  335. {
  336. case Dot3DSFile::CHUNK_TRACKINFO:
  337. // this starts a new mesh
  338. this->ParseHierarchyChunk(iRemaining);
  339. break;
  340. };
  341. ASSIMP_3DS_VALIDATE_CHUNK_SIZE();
  342. ASSIMP_3DS_END_CHUNK();
  343. return this->ParseKeyframeChunk(piRemaining);
  344. }
  345. // ------------------------------------------------------------------------------------------------
  346. void Dot3DSImporter::InverseNodeSearch(Dot3DS::Node* pcNode,Dot3DS::Node* pcCurrent)
  347. {
  348. if (NULL == pcCurrent)
  349. {
  350. this->mRootNode->push_back(pcNode);
  351. return;
  352. }
  353. if (pcCurrent->mHierarchyPos == pcNode->mHierarchyPos)
  354. {
  355. if(pcCurrent->mParent)pcCurrent->mParent->push_back(pcNode);
  356. else pcCurrent->push_back(pcNode);
  357. return;
  358. }
  359. return this->InverseNodeSearch(pcNode,pcCurrent->mParent);
  360. }
  361. // ------------------------------------------------------------------------------------------------
  362. void Dot3DSImporter::ParseHierarchyChunk(int& piRemaining)
  363. {
  364. ASSIMP_3DS_BEGIN_CHUNK();
  365. // get chunk type
  366. const unsigned char* sz = (unsigned char*)this->mCurrent;
  367. unsigned int iCnt = 0;
  368. uint16_t iHierarchy;
  369. Dot3DS::Node* pcNode;
  370. switch (psChunk->Flag)
  371. {
  372. case Dot3DSFile::CHUNK_TRACKOBJNAME:
  373. // get object name
  374. while (*sz++ != '\0')
  375. {
  376. if (sz > pcCurNext-1)break;
  377. ++iCnt;
  378. }
  379. pcNode = new Dot3DS::Node();
  380. pcNode->mName = std::string((const char*)this->mCurrent,iCnt);
  381. iCnt++;
  382. // there are two unknown values which we can safely ignore
  383. this->mCurrent += iCnt + sizeof(uint16_t)*2;
  384. iHierarchy = *((uint16_t*)this->mCurrent);
  385. iHierarchy++;
  386. pcNode->mHierarchyPos = iHierarchy;
  387. pcNode->mHierarchyIndex = this->mLastNodeIndex;
  388. if (this->mCurrentNode && this->mCurrentNode->mHierarchyPos == iHierarchy)
  389. {
  390. // add to the parent of the last touched node
  391. this->mCurrentNode->mParent->push_back(pcNode);
  392. this->mLastNodeIndex++;
  393. }
  394. else if(iHierarchy >= this->mLastNodeIndex)
  395. {
  396. // place it at the current position in the hierarchy
  397. this->mCurrentNode->push_back(pcNode);
  398. this->mLastNodeIndex = iHierarchy;
  399. }
  400. else
  401. {
  402. // need to go back to the specified position in the hierarchy.
  403. this->InverseNodeSearch(pcNode,this->mCurrentNode);
  404. this->mLastNodeIndex++;
  405. }
  406. this->mCurrentNode = pcNode;
  407. break;
  408. case Dot3DSFile::CHUNK_TRACKPIVOT:
  409. // pivot = origin of rotation and scaling
  410. this->mCurrentNode->vPivot = *((const aiVector3D*)this->mCurrent);
  411. //std::swap((float&)mCurrentNode->vPivot.y,(float&)mCurrentNode->vPivot.z);
  412. mCurrentNode->vPivot.y *= -1.f;
  413. this->mCurrent += sizeof(aiVector3D);
  414. break;
  415. #ifdef AI_3DS_KEYFRAME_ANIMATION
  416. case Dot3DSFile::CHUNK_TRACKPOS:
  417. /*
  418. +2 short flags;
  419. +8 short unknown[4];
  420. +2 short keys;
  421. +2 short unknown;
  422. struct {
  423. +2 short framenum;
  424. +4 long unknown;
  425. float pos_x, pos_y, pos_z;
  426. } pos[keys];
  427. */
  428. this->mCurrent += 10;
  429. iTemp = *((const uint16_t*)mCurrent);
  430. this->mCurrent += sizeof(uint16_t) * 2;
  431. for (unsigned int i = 0; i < (unsigned int)iTemp;++i)
  432. {
  433. uint16_t sNum = *((const uint16_t*)mCurrent);
  434. this->mCurrent += sizeof(uint16_t);
  435. aiVectorKey v;v.mTime = (double)sNum;
  436. this->mCurrent += sizeof(uint32_t);
  437. v.mValue = *((const aiVector3D*)this->mCurrent);
  438. this->mCurrent += sizeof(aiVector3D);
  439. // check whether we do already have this keyframe
  440. for (std::vector<aiVectorKey>::const_iterator
  441. i = this->mCurrentNode->aPositionKeys.begin();
  442. i != this->mCurrentNode->aPositionKeys.end();++i)
  443. {
  444. if ((*i).mTime == v.mTime){v.mTime = -10e10f;break;}
  445. }
  446. // add the new keyframe
  447. if (v.mTime != -10e10f)
  448. this->mCurrentNode->aPositionKeys.push_back(v);
  449. }
  450. break;
  451. case Dot3DSFile::CHUNK_TRACKROTATE:
  452. /*
  453. +2 short flags;
  454. +8 short unknown[4];
  455. +2 short keys;
  456. +2 short unknown;
  457. struct {
  458. +2 short framenum;
  459. +4 long unknown;
  460. float rad , pos_x, pos_y, pos_z;
  461. } pos[keys];
  462. */
  463. this->mCurrent += 10;
  464. iTemp = *((const uint16_t*)mCurrent);
  465. this->mCurrent += sizeof(uint16_t) * 2;
  466. for (unsigned int i = 0; i < (unsigned int)iTemp;++i)
  467. {
  468. uint16_t sNum = *((const uint16_t*)mCurrent);
  469. this->mCurrent += sizeof(uint16_t);
  470. aiQuatKey v;v.mTime = (double)sNum;
  471. this->mCurrent += sizeof(uint32_t);
  472. float fRadians = *((const float*)this->mCurrent);
  473. this->mCurrent += sizeof(float);
  474. aiVector3D vAxis = *((const aiVector3D*)this->mCurrent);
  475. this->mCurrent += sizeof(aiVector3D);
  476. // construct a rotation quaternion from the axis-angle pair
  477. v.mValue = aiQuaternion(vAxis,fRadians);
  478. // check whether we do already have this keyframe
  479. for (std::vector<aiQuatKey>::const_iterator
  480. i = this->mCurrentNode->aRotationKeys.begin();
  481. i != this->mCurrentNode->aRotationKeys.end();++i)
  482. {
  483. if ((*i).mTime == v.mTime){v.mTime = -10e10f;break;}
  484. }
  485. // add the new keyframe
  486. if (v.mTime != -10e10f)
  487. this->mCurrentNode->aRotationKeys.push_back(v);
  488. }
  489. break;
  490. case Dot3DSFile::CHUNK_TRACKSCALE:
  491. /*
  492. +2 short flags;
  493. +8 short unknown[4];
  494. +2 short keys;
  495. +2 short unknown;
  496. struct {
  497. +2 short framenum;
  498. +4 long unknown;
  499. float pos_x, pos_y, pos_z;
  500. } pos[keys];
  501. */
  502. this->mCurrent += 10;
  503. iTemp = *((const uint16_t*)mCurrent);
  504. this->mCurrent += sizeof(uint16_t) * 2;
  505. for (unsigned int i = 0; i < (unsigned int)iTemp;++i)
  506. {
  507. uint16_t sNum = *((const uint16_t*)mCurrent);
  508. this->mCurrent += sizeof(uint16_t);
  509. aiVectorKey v;
  510. v.mTime = (double)sNum;
  511. this->mCurrent += sizeof(uint32_t);
  512. v.mValue = *((const aiVector3D*)this->mCurrent);
  513. this->mCurrent += sizeof(aiVector3D);
  514. // check whether we do already have this keyframe
  515. for (std::vector<aiVectorKey>::const_iterator
  516. i = this->mCurrentNode->aScalingKeys.begin();
  517. i != this->mCurrentNode->aScalingKeys.end();++i)
  518. {
  519. if ((*i).mTime == v.mTime){v.mTime = -10e10f;break;}
  520. }
  521. // add the new keyframe
  522. if (v.mTime != -10e10f)this->mCurrentNode->aScalingKeys.push_back(v);
  523. if (v.mValue.x && v.mValue.y && v.mValue.z)
  524. {
  525. DefaultLogger::get()->warn("Found zero scaled axis in scaling keyframe");
  526. ++iCnt;
  527. }
  528. }
  529. // there are 3DS files that have only zero scalings
  530. if (iTemp == iCnt)
  531. {
  532. DefaultLogger::get()->warn("All scaling keys are zero. They will be removed");
  533. this->mCurrentNode->aScalingKeys.clear();
  534. }
  535. break;
  536. #endif
  537. };
  538. ASSIMP_3DS_VALIDATE_CHUNK_SIZE();
  539. ASSIMP_3DS_END_CHUNK();
  540. return this->ParseHierarchyChunk(piRemaining);
  541. }
  542. // ------------------------------------------------------------------------------------------------
  543. void Dot3DSImporter::ParseFaceChunk(int& piRemaining)
  544. {
  545. ASSIMP_3DS_BEGIN_CHUNK();
  546. Dot3DS::Mesh& mMesh = this->mScene->mMeshes.back();
  547. // get chunk type
  548. const unsigned char* sz = this->mCurrent;
  549. uint32_t iCnt = 0,iTemp;
  550. switch (psChunk->Flag)
  551. {
  552. case Dot3DSFile::CHUNK_SMOOLIST:
  553. // one int32 for each face
  554. for (std::vector<Dot3DS::Face>::iterator
  555. i = mMesh.mFaces.begin();
  556. i != mMesh.mFaces.end();++i)
  557. {
  558. // nth bit is set for nth smoothing group
  559. (*i).iSmoothGroup = *((uint32_t*)this->mCurrent);
  560. this->mCurrent += sizeof(uint32_t);
  561. }
  562. break;
  563. case Dot3DSFile::CHUNK_FACEMAT:
  564. // at fist an asciiz with the material name
  565. while (*sz++)
  566. {
  567. // make sure we don't run over the end of the chunk
  568. if (sz > pcCurNext-1)break;
  569. }
  570. // find the index of the material
  571. unsigned int iIndex = 0xFFFFFFFF;
  572. iCnt = 0;
  573. for (std::vector<Dot3DS::Material>::const_iterator
  574. i = this->mScene->mMaterials.begin();
  575. i != this->mScene->mMaterials.end();++i,++iCnt)
  576. {
  577. // compare case-independent to be sure it works
  578. if (0 == ASSIMP_stricmp((const char*)this->mCurrent,
  579. (const char*)((*i).mName.c_str())))
  580. {
  581. iIndex = iCnt;
  582. break;
  583. }
  584. }
  585. if (0xFFFFFFFF == iIndex)
  586. {
  587. // this material is not known. Ignore this. We will later
  588. // assign the default material to all faces using *this*
  589. // material. Use 0xcdcdcdcd as special value to indicate
  590. // this.
  591. iIndex = 0xcdcdcdcd;
  592. }
  593. this->mCurrent = sz;
  594. iCnt = (int)(*((uint16_t*)this->mCurrent));
  595. this->mCurrent += sizeof(uint16_t);
  596. for (unsigned int i = 0; i < iCnt;++i)
  597. {
  598. iTemp = (uint16_t)*((uint16_t*)this->mCurrent);
  599. // check range
  600. if (iTemp >= mMesh.mFaceMaterials.size())
  601. {
  602. DefaultLogger::get()->error("Invalid face index in face material list");
  603. mMesh.mFaceMaterials[mMesh.mFaceMaterials.size()-1] = iIndex;
  604. }
  605. else
  606. {
  607. mMesh.mFaceMaterials[iTemp] = iIndex;
  608. }
  609. this->mCurrent += sizeof(uint16_t);
  610. }
  611. break;
  612. };
  613. ASSIMP_3DS_VALIDATE_CHUNK_SIZE();
  614. ASSIMP_3DS_END_CHUNK();
  615. return ParseFaceChunk(piRemaining);
  616. }
  617. // ------------------------------------------------------------------------------------------------
  618. void Dot3DSImporter::ParseMeshChunk(int& piRemaining)
  619. {
  620. ASSIMP_3DS_BEGIN_CHUNK();
  621. Dot3DS::Mesh& mMesh = this->mScene->mMeshes.back();
  622. // get chunk type
  623. int iRemaining;
  624. uint16_t iNum = 0;
  625. float* pf;
  626. switch (psChunk->Flag)
  627. {
  628. case Dot3DSFile::CHUNK_VERTLIST:
  629. iNum = *((short*)this->mCurrent);
  630. this->mCurrent += sizeof(short);
  631. while (iNum-- > 0)
  632. {
  633. mMesh.mPositions.push_back(*((aiVector3D*)this->mCurrent));
  634. aiVector3D& v = mMesh.mPositions.back();
  635. //std::swap( (float&)v.y, (float&)v.z);
  636. v.y *= -1.0f;
  637. this->mCurrent += sizeof(aiVector3D);
  638. }
  639. break;
  640. case Dot3DSFile::CHUNK_TRMATRIX:
  641. {
  642. pf = (float*)this->mCurrent;
  643. this->mCurrent += 12 * sizeof(float);
  644. mMesh.mMat.a1 = pf[0];
  645. mMesh.mMat.b1 = pf[1];
  646. mMesh.mMat.c1 = pf[2];
  647. mMesh.mMat.a2 = pf[3];
  648. mMesh.mMat.b2 = pf[4];
  649. mMesh.mMat.c2 = pf[5];
  650. mMesh.mMat.a3 = pf[6];
  651. mMesh.mMat.b3 = pf[7];
  652. mMesh.mMat.c3 = pf[8];
  653. mMesh.mMat.a4 = pf[9];
  654. mMesh.mMat.b4 = pf[10];
  655. mMesh.mMat.c4 = pf[11];
  656. // now check whether the matrix has got a negative determinant
  657. // If yes, we need to flip all vertices' x axis ....
  658. // From lib3ds, mesh.c
  659. if (mMesh.mMat.Determinant() < 0.0f)
  660. {
  661. aiMatrix4x4 mInv = mMesh.mMat;
  662. mInv.Inverse();
  663. aiMatrix4x4 mMe = mMesh.mMat;
  664. mMe.a1 *= -1.0f;
  665. mMe.b1 *= -1.0f;
  666. mMe.c1 *= -1.0f;
  667. mMe.d1 *= -1.0f;
  668. mInv = mInv * mMe;
  669. for (register unsigned int i = 0; i < mMesh.mPositions.size();++i)
  670. {
  671. aiVector3D a,c;
  672. a = mMesh.mPositions[i];
  673. c[0]= mInv[0][0]*a[0] + mInv[1][0]*a[1] + mInv[2][0]*a[2] + mInv[3][0];
  674. c[1]= mInv[0][1]*a[0] + mInv[1][1]*a[1] + mInv[2][1]*a[2] + mInv[3][1];
  675. c[2]= mInv[0][2]*a[0] + mInv[1][2]*a[1] + mInv[2][2]*a[2] + mInv[3][2];
  676. mMesh.mPositions[i] = c;
  677. }
  678. }
  679. }
  680. break;
  681. case Dot3DSFile::CHUNK_MAPLIST:
  682. iNum = *((uint16_t*)this->mCurrent);
  683. this->mCurrent += sizeof(uint16_t);
  684. while (iNum-- > 0)
  685. {
  686. mMesh.mTexCoords.push_back(*((aiVector2D*)this->mCurrent));
  687. this->mCurrent += sizeof(aiVector2D);
  688. }
  689. break;
  690. case Dot3DSFile::CHUNK_FACELIST:
  691. iNum = *((uint16_t*)this->mCurrent);
  692. this->mCurrent += sizeof(uint16_t);
  693. while (iNum-- > 0)
  694. {
  695. Dot3DS::Face sFace;
  696. sFace.mIndices[0] = *((uint16_t*)this->mCurrent);
  697. this->mCurrent += sizeof(uint16_t);
  698. sFace.mIndices[1] = *((uint16_t*)this->mCurrent);
  699. this->mCurrent += sizeof(uint16_t);
  700. sFace.mIndices[2] = *((uint16_t*)this->mCurrent);
  701. this->mCurrent += 2*sizeof(uint16_t);
  702. mMesh.mFaces.push_back(sFace);
  703. }
  704. // resize the material array (0xcdcdcdcd marks the
  705. // default material; so if a face is not referenced
  706. // by a material $$DEFAULT will be assigned to it)
  707. mMesh.mFaceMaterials.resize(mMesh.mFaces.size(),0xcdcdcdcd);
  708. iRemaining = (int)(pcCurNext - this->mCurrent);
  709. if (iRemaining > 0)this->ParseFaceChunk(iRemaining);
  710. break;
  711. };
  712. ASSIMP_3DS_VALIDATE_CHUNK_SIZE();
  713. ASSIMP_3DS_END_CHUNK();
  714. return ParseMeshChunk(piRemaining);
  715. }
  716. // ------------------------------------------------------------------------------------------------
  717. void Dot3DSImporter::ParseMaterialChunk(int& piRemaining)
  718. {
  719. ASSIMP_3DS_BEGIN_CHUNK();
  720. // get chunk type
  721. const unsigned char* sz = this->mCurrent;
  722. unsigned int iCnt = 0;
  723. int iRemaining;
  724. aiColor3D* pc;
  725. float* pcf;
  726. switch (psChunk->Flag)
  727. {
  728. case Dot3DSFile::CHUNK_MAT_MATNAME:
  729. // string in file is zero-terminated,
  730. // this should be no problem. However, validate whether it overlaps
  731. // the end of the chunk, if yes we should truncate it.
  732. while (*sz++ != '\0')
  733. {
  734. if (sz > pcCurNext-1)
  735. {
  736. DefaultLogger::get()->error("Material name string is too long");
  737. break;
  738. }
  739. ++iCnt;
  740. }
  741. this->mScene->mMaterials.back().mName = std::string((const char*)this->mCurrent,iCnt);
  742. break;
  743. case Dot3DSFile::CHUNK_MAT_DIFFUSE:
  744. pc = &this->mScene->mMaterials.back().mDiffuse;
  745. this->ParseColorChunk(pc);
  746. if (is_qnan(pc->r))
  747. {
  748. // color chunk is invalid. Simply ignore it
  749. DefaultLogger::get()->error("Unable to read DIFFUSE chunk");
  750. pc->r = pc->g = pc->b = 1.0f;
  751. }
  752. break;
  753. case Dot3DSFile::CHUNK_MAT_SPECULAR:
  754. pc = &this->mScene->mMaterials.back().mSpecular;
  755. this->ParseColorChunk(pc);
  756. if (is_qnan(pc->r))
  757. {
  758. // color chunk is invalid. Simply ignore it
  759. DefaultLogger::get()->error("Unable to read SPECULAR chunk");
  760. pc->r = pc->g = pc->b = 1.0f;
  761. }
  762. break;
  763. case Dot3DSFile::CHUNK_MAT_AMBIENT:
  764. pc = &this->mScene->mMaterials.back().mAmbient;
  765. this->ParseColorChunk(pc);
  766. if (is_qnan(pc->r))
  767. {
  768. // color chunk is invalid. Simply ignore it
  769. DefaultLogger::get()->error("Unable to read AMBIENT chunk");
  770. pc->r = pc->g = pc->b = 1.0f;
  771. }
  772. break;
  773. case Dot3DSFile::CHUNK_MAT_SELF_ILLUM:
  774. pc = &this->mScene->mMaterials.back().mEmissive;
  775. this->ParseColorChunk(pc);
  776. if (is_qnan(pc->r))
  777. {
  778. // color chunk is invalid. Simply ignore it
  779. // EMISSSIVE TO 0|0|0
  780. DefaultLogger::get()->error("Unable to read EMISSIVE chunk");
  781. pc->r = pc->g = pc->b = 0.0f;
  782. }
  783. break;
  784. case Dot3DSFile::CHUNK_MAT_TRANSPARENCY:
  785. pcf = &this->mScene->mMaterials.back().mTransparency;
  786. *pcf = this->ParsePercentageChunk();
  787. // NOTE: transparency, not opacity
  788. if (is_qnan(*pcf))*pcf = 1.0f;
  789. else *pcf = 1.0f - *pcf * (float)0xFFFF / 100.0f;
  790. break;
  791. case Dot3DSFile::CHUNK_MAT_SHADING:
  792. this->mScene->mMaterials.back().mShading =
  793. (Dot3DS::Dot3DSFile::shadetype3ds)*((uint16_t*)this->mCurrent);
  794. this->mCurrent += sizeof(uint16_t);
  795. break;
  796. case Dot3DSFile::CHUNK_MAT_TWO_SIDE:
  797. this->mScene->mMaterials.back().mTwoSided = true;
  798. break;
  799. case Dot3DSFile::CHUNK_MAT_SHININESS:
  800. pcf = &this->mScene->mMaterials.back().mSpecularExponent;
  801. *pcf = this->ParsePercentageChunk();
  802. if (is_qnan(*pcf))*pcf = 0.0f;
  803. else *pcf *= (float)0xFFFF;
  804. break;
  805. case Dot3DSFile::CHUNK_MAT_SHININESS_PERCENT:
  806. pcf = &this->mScene->mMaterials.back().mShininessStrength;
  807. *pcf = this->ParsePercentageChunk();
  808. if (is_qnan(*pcf))*pcf = 0.0f;
  809. else *pcf *= (float)0xffff / 100.0f;
  810. break;
  811. case Dot3DSFile::CHUNK_MAT_SELF_ILPCT:
  812. // TODO: need to multiply with emissive base color?
  813. pcf = &this->mScene->mMaterials.back().sTexEmissive.mTextureBlend;
  814. *pcf = this->ParsePercentageChunk();
  815. if (is_qnan(*pcf))*pcf = 0.0f;
  816. else *pcf = *pcf * (float)0xFFFF / 100.0f;
  817. break;
  818. // parse texture chunks
  819. case Dot3DSFile::CHUNK_MAT_TEXTURE:
  820. iRemaining = (psChunk->Size - sizeof(Dot3DSFile::Chunk));
  821. this->ParseTextureChunk(iRemaining,&this->mScene->mMaterials.back().sTexDiffuse);
  822. break;
  823. case Dot3DSFile::CHUNK_MAT_BUMPMAP:
  824. iRemaining = (psChunk->Size - sizeof(Dot3DSFile::Chunk));
  825. this->ParseTextureChunk(iRemaining,&this->mScene->mMaterials.back().sTexBump);
  826. break;
  827. case Dot3DSFile::CHUNK_MAT_OPACMAP:
  828. iRemaining = (psChunk->Size - sizeof(Dot3DSFile::Chunk));
  829. this->ParseTextureChunk(iRemaining,&this->mScene->mMaterials.back().sTexOpacity);
  830. break;
  831. case Dot3DSFile::CHUNK_MAT_MAT_SHINMAP:
  832. iRemaining = (psChunk->Size - sizeof(Dot3DSFile::Chunk));
  833. this->ParseTextureChunk(iRemaining,&this->mScene->mMaterials.back().sTexShininess);
  834. break;
  835. case Dot3DSFile::CHUNK_MAT_SPECMAP:
  836. iRemaining = (psChunk->Size - sizeof(Dot3DSFile::Chunk));
  837. this->ParseTextureChunk(iRemaining,&this->mScene->mMaterials.back().sTexSpecular);
  838. break;
  839. case Dot3DSFile::CHUNK_MAT_SELFIMAP:
  840. iRemaining = (psChunk->Size - sizeof(Dot3DSFile::Chunk));
  841. this->ParseTextureChunk(iRemaining,&this->mScene->mMaterials.back().sTexEmissive);
  842. break;
  843. };
  844. ASSIMP_3DS_VALIDATE_CHUNK_SIZE();
  845. ASSIMP_3DS_END_CHUNK();
  846. return ParseMaterialChunk(piRemaining);
  847. }
  848. // ------------------------------------------------------------------------------------------------
  849. void Dot3DSImporter::ParseTextureChunk(int& piRemaining,Dot3DS::Texture* pcOut)
  850. {
  851. ASSIMP_3DS_BEGIN_CHUNK();
  852. // get chunk type
  853. const unsigned char* sz = this->mCurrent;
  854. unsigned int iCnt = 0;
  855. switch (psChunk->Flag)
  856. {
  857. case Dot3DSFile::CHUNK_MAPFILE:
  858. // string in file is zero-terminated,
  859. // this should be no problem. However, validate whether
  860. // it overlaps the end of the chunk, if yes we should
  861. // truncate it.
  862. while (*sz++ != '\0')
  863. {
  864. if (sz > pcCurNext-1)break;
  865. ++iCnt;
  866. }
  867. pcOut->mMapName = std::string((const char*)this->mCurrent,iCnt);
  868. break;
  869. // manually parse the blend factor
  870. case Dot3DSFile::CHUNK_PERCENTF:
  871. pcOut->mTextureBlend = *((float*)this->mCurrent);
  872. break;
  873. // manually parse the blend factor
  874. case Dot3DSFile::CHUNK_PERCENTW:
  875. pcOut->mTextureBlend = (float)(*((short*)this->mCurrent)) / 100.0f;
  876. break;
  877. case Dot3DSFile::CHUNK_MAT_MAP_USCALE:
  878. pcOut->mScaleU = *((float*)this->mCurrent);
  879. if (0.0f == pcOut->mScaleU)
  880. {
  881. DefaultLogger::get()->warn("Texture coordinate scaling in the "
  882. "x direction is zero. Assuming this should be 1.0 ... ");
  883. pcOut->mScaleU = 1.0f;
  884. }
  885. break;
  886. case Dot3DSFile::CHUNK_MAT_MAP_VSCALE:
  887. pcOut->mScaleV = *((float*)this->mCurrent);
  888. if (0.0f == pcOut->mScaleV)
  889. {
  890. DefaultLogger::get()->warn("Texture coordinate scaling in the "
  891. "y direction is zero. Assuming this should be 1.0 ... ");
  892. pcOut->mScaleV = 1.0f;
  893. }
  894. break;
  895. case Dot3DSFile::CHUNK_MAT_MAP_UOFFSET:
  896. pcOut->mOffsetU = *((float*)this->mCurrent);
  897. break;
  898. case Dot3DSFile::CHUNK_MAT_MAP_VOFFSET:
  899. pcOut->mOffsetV = *((float*)this->mCurrent);
  900. break;
  901. case Dot3DSFile::CHUNK_MAT_MAP_ANG:
  902. pcOut->mRotation = *((float*)this->mCurrent);
  903. break;
  904. case Dot3DSFile::CHUNK_MAT_MAP_TILING:
  905. uint16_t iFlags = *((uint16_t*)this->mCurrent);
  906. // check whether the mirror flag is set
  907. if (iFlags & 0x2u)
  908. {
  909. pcOut->mMapMode = aiTextureMapMode_Mirror;
  910. }
  911. // assume that "decal" means clamping ...
  912. else if (iFlags & 0x10u && iFlags & 0x1u)
  913. {
  914. pcOut->mMapMode = aiTextureMapMode_Clamp;
  915. }
  916. break;
  917. };
  918. ASSIMP_3DS_VALIDATE_CHUNK_SIZE();
  919. ASSIMP_3DS_END_CHUNK();
  920. return ParseTextureChunk(piRemaining,pcOut);
  921. }
  922. // ------------------------------------------------------------------------------------------------
  923. float Dot3DSImporter::ParsePercentageChunk()
  924. {
  925. const Dot3DSFile::Chunk* psChunk;
  926. this->ReadChunk(&psChunk);
  927. if (NULL == psChunk)return std::numeric_limits<float>::quiet_NaN();
  928. if (Dot3DSFile::CHUNK_PERCENTF == psChunk->Flag)
  929. {
  930. if (sizeof(float) > psChunk->Size)
  931. return std::numeric_limits<float>::quiet_NaN();
  932. return *((float*)this->mCurrent);
  933. }
  934. else if (Dot3DSFile::CHUNK_PERCENTW == psChunk->Flag)
  935. {
  936. if (2 > psChunk->Size)
  937. return std::numeric_limits<float>::quiet_NaN();
  938. return (float)(*((short*)this->mCurrent)) / (float)0xFFFF;
  939. }
  940. this->mCurrent += psChunk->Size - sizeof(Dot3DSFile::Chunk);
  941. return std::numeric_limits<float>::quiet_NaN();
  942. }
  943. // ------------------------------------------------------------------------------------------------
  944. void Dot3DSImporter::ParseColorChunk(aiColor3D* p_pcOut,
  945. bool p_bAcceptPercent)
  946. {
  947. ai_assert(p_pcOut != NULL);
  948. // error return value
  949. static const aiColor3D clrError = aiColor3D(std::numeric_limits<float>::quiet_NaN(),
  950. std::numeric_limits<float>::quiet_NaN(),
  951. std::numeric_limits<float>::quiet_NaN());
  952. const Dot3DSFile::Chunk* psChunk;
  953. this->ReadChunk(&psChunk);
  954. if (!psChunk)
  955. {
  956. *p_pcOut = clrError;
  957. return;
  958. }
  959. const unsigned int diff = psChunk->Size - sizeof(Dot3DSFile::Chunk);
  960. const unsigned char* pcCur = this->mCurrent;
  961. this->mCurrent += diff;
  962. bool bGamma = false;
  963. switch(psChunk->Flag)
  964. {
  965. case Dot3DSFile::CHUNK_LINRGBF:
  966. bGamma = true;
  967. case Dot3DSFile::CHUNK_RGBF:
  968. if (sizeof(float) * 3 > diff)
  969. {
  970. *p_pcOut = clrError;
  971. return;
  972. }
  973. p_pcOut->r = ((float*)pcCur)[0];
  974. p_pcOut->g = ((float*)pcCur)[1];
  975. p_pcOut->b = ((float*)pcCur)[2];
  976. break;
  977. case Dot3DSFile::CHUNK_LINRGBB:
  978. bGamma = true;
  979. case Dot3DSFile::CHUNK_RGBB:
  980. if (sizeof(char) * 3 > diff)
  981. {
  982. *p_pcOut = clrError;
  983. return;
  984. }
  985. p_pcOut->r = (float)pcCur[0] / 255.0f;
  986. p_pcOut->g = (float)pcCur[1] / 255.0f;
  987. p_pcOut->b = (float)pcCur[2] / 255.0f;
  988. break;
  989. // percentage chunks: accepted to be compatible with various
  990. // .3ds files with very curious content
  991. case Dot3DSFile::CHUNK_PERCENTF:
  992. if (p_bAcceptPercent && 4 <= diff)
  993. {
  994. p_pcOut->r = *((float*)pcCur);
  995. p_pcOut->g = *((float*)pcCur);
  996. p_pcOut->b = *((float*)pcCur);
  997. break;
  998. }
  999. *p_pcOut = clrError;
  1000. return;
  1001. case Dot3DSFile::CHUNK_PERCENTW:
  1002. if (p_bAcceptPercent && 1 <= diff)
  1003. {
  1004. p_pcOut->r = (float)pcCur[0] / 255.0f;
  1005. p_pcOut->g = (float)pcCur[0] / 255.0f;
  1006. p_pcOut->b = (float)pcCur[0] / 255.0f;
  1007. break;
  1008. }
  1009. *p_pcOut = clrError;
  1010. return;
  1011. default:
  1012. // skip unknown chunks, hope this won't cause any problems.
  1013. return this->ParseColorChunk(p_pcOut,p_bAcceptPercent);
  1014. };
  1015. if (bGamma)
  1016. {
  1017. p_pcOut->r = powf(p_pcOut->r, 1.0f / 2.2f);
  1018. p_pcOut->g = powf(p_pcOut->g, 1.0f / 2.2f);
  1019. p_pcOut->b = powf(p_pcOut->b, 1.0f / 2.2f);
  1020. }
  1021. return;
  1022. }