3DSLoader.cpp 41 KB

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