3DSLoader.cpp 35 KB

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