3DSLoader.cpp 39 KB

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