3DSLoader.cpp 37 KB

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