3DSLoader.cpp 39 KB

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