MDLMaterialLoader.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2019, 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 Implementation of the material part of the MDL importer class */
  35. #ifndef ASSIMP_BUILD_NO_MDL_IMPORTER
  36. // internal headers
  37. #include "MDLLoader.h"
  38. #include "MDLDefaultColorMap.h"
  39. #include <assimp/StringUtils.h>
  40. #include <assimp/texture.h>
  41. #include <assimp/IOSystem.hpp>
  42. #include <assimp/DefaultLogger.hpp>
  43. #include <assimp/scene.h>
  44. #include <assimp/Defines.h>
  45. #include <assimp/qnan.h>
  46. #include <memory>
  47. using namespace Assimp;
  48. static aiTexel* const bad_texel = reinterpret_cast<aiTexel*>(SIZE_MAX);
  49. // ------------------------------------------------------------------------------------------------
  50. // Find a suitable palette file or take the default one
  51. void MDLImporter::SearchPalette(const unsigned char** pszColorMap)
  52. {
  53. // now try to find the color map in the current directory
  54. IOStream* pcStream = pIOHandler->Open(configPalette,"rb");
  55. const unsigned char* szColorMap = (const unsigned char*)::g_aclrDefaultColorMap;
  56. if(pcStream)
  57. {
  58. if (pcStream->FileSize() >= 768)
  59. {
  60. size_t len = 256 * 3;
  61. unsigned char* colorMap = new unsigned char[len];
  62. szColorMap = colorMap;
  63. pcStream->Read(colorMap, len,1);
  64. ASSIMP_LOG_INFO("Found valid colormap.lmp in directory. "
  65. "It will be used to decode embedded textures in palletized formats.");
  66. }
  67. delete pcStream;
  68. pcStream = NULL;
  69. }
  70. *pszColorMap = szColorMap;
  71. }
  72. // ------------------------------------------------------------------------------------------------
  73. // Free the palette again
  74. void MDLImporter::FreePalette(const unsigned char* szColorMap)
  75. {
  76. if (szColorMap != (const unsigned char*)::g_aclrDefaultColorMap)
  77. delete[] szColorMap;
  78. }
  79. // ------------------------------------------------------------------------------------------------
  80. // Check whether we can replace a texture with a single color
  81. aiColor4D MDLImporter::ReplaceTextureWithColor(const aiTexture* pcTexture)
  82. {
  83. ai_assert(NULL != pcTexture);
  84. aiColor4D clrOut;
  85. clrOut.r = get_qnan();
  86. if (!pcTexture->mHeight || !pcTexture->mWidth)
  87. return clrOut;
  88. const unsigned int iNumPixels = pcTexture->mHeight*pcTexture->mWidth;
  89. const aiTexel* pcTexel = pcTexture->pcData+1;
  90. const aiTexel* const pcTexelEnd = &pcTexture->pcData[iNumPixels];
  91. while (pcTexel != pcTexelEnd)
  92. {
  93. if (*pcTexel != *(pcTexel-1))
  94. {
  95. pcTexel = NULL;
  96. break;
  97. }
  98. ++pcTexel;
  99. }
  100. if (pcTexel)
  101. {
  102. clrOut.r = pcTexture->pcData->r / 255.0f;
  103. clrOut.g = pcTexture->pcData->g / 255.0f;
  104. clrOut.b = pcTexture->pcData->b / 255.0f;
  105. clrOut.a = pcTexture->pcData->a / 255.0f;
  106. }
  107. return clrOut;
  108. }
  109. // ------------------------------------------------------------------------------------------------
  110. // Read a texture from a MDL3 file
  111. void MDLImporter::CreateTextureARGB8_3DGS_MDL3(const unsigned char* szData)
  112. {
  113. const MDL::Header *pcHeader = (const MDL::Header*)mBuffer; //the endianness is already corrected in the InternReadFile_3DGS_MDL345 function
  114. VALIDATE_FILE_SIZE(szData + pcHeader->skinwidth *
  115. pcHeader->skinheight);
  116. // allocate a new texture object
  117. aiTexture* pcNew = new aiTexture();
  118. pcNew->mWidth = pcHeader->skinwidth;
  119. pcNew->mHeight = pcHeader->skinheight;
  120. pcNew->pcData = new aiTexel[pcNew->mWidth * pcNew->mHeight];
  121. const unsigned char* szColorMap;
  122. this->SearchPalette(&szColorMap);
  123. // copy texture data
  124. for (unsigned int i = 0; i < pcNew->mWidth*pcNew->mHeight;++i)
  125. {
  126. const unsigned char val = szData[i];
  127. const unsigned char* sz = &szColorMap[val*3];
  128. pcNew->pcData[i].a = 0xFF;
  129. pcNew->pcData[i].r = *sz++;
  130. pcNew->pcData[i].g = *sz++;
  131. pcNew->pcData[i].b = *sz;
  132. }
  133. FreePalette(szColorMap);
  134. // store the texture
  135. aiTexture** pc = this->pScene->mTextures;
  136. this->pScene->mTextures = new aiTexture*[pScene->mNumTextures+1];
  137. for (unsigned int i = 0; i <pScene->mNumTextures;++i)
  138. pScene->mTextures[i] = pc[i];
  139. pScene->mTextures[this->pScene->mNumTextures] = pcNew;
  140. pScene->mNumTextures++;
  141. delete[] pc;
  142. return;
  143. }
  144. // ------------------------------------------------------------------------------------------------
  145. // Read a texture from a MDL4 file
  146. void MDLImporter::CreateTexture_3DGS_MDL4(const unsigned char* szData,
  147. unsigned int iType,
  148. unsigned int* piSkip)
  149. {
  150. ai_assert(NULL != piSkip);
  151. const MDL::Header *pcHeader = (const MDL::Header*)mBuffer; //the endianness is already corrected in the InternReadFile_3DGS_MDL345 function
  152. if (iType == 1 || iType > 3)
  153. {
  154. ASSIMP_LOG_ERROR("Unsupported texture file format");
  155. return;
  156. }
  157. const bool bNoRead = *piSkip == UINT_MAX;
  158. // allocate a new texture object
  159. aiTexture* pcNew = new aiTexture();
  160. pcNew->mWidth = pcHeader->skinwidth;
  161. pcNew->mHeight = pcHeader->skinheight;
  162. if (bNoRead)pcNew->pcData = bad_texel;
  163. ParseTextureColorData(szData,iType,piSkip,pcNew);
  164. // store the texture
  165. if (!bNoRead)
  166. {
  167. if (!this->pScene->mNumTextures)
  168. {
  169. pScene->mNumTextures = 1;
  170. pScene->mTextures = new aiTexture*[1];
  171. pScene->mTextures[0] = pcNew;
  172. }
  173. else
  174. {
  175. aiTexture** pc = pScene->mTextures;
  176. pScene->mTextures = new aiTexture*[pScene->mNumTextures+1];
  177. for (unsigned int i = 0; i < this->pScene->mNumTextures;++i)
  178. pScene->mTextures[i] = pc[i];
  179. pScene->mTextures[pScene->mNumTextures] = pcNew;
  180. pScene->mNumTextures++;
  181. delete[] pc;
  182. }
  183. }
  184. else {
  185. pcNew->pcData = NULL;
  186. delete pcNew;
  187. }
  188. return;
  189. }
  190. // ------------------------------------------------------------------------------------------------
  191. // Load color data of a texture and convert it to our output format
  192. void MDLImporter::ParseTextureColorData(const unsigned char* szData,
  193. unsigned int iType,
  194. unsigned int* piSkip,
  195. aiTexture* pcNew)
  196. {
  197. const bool do_read = bad_texel != pcNew->pcData;
  198. // allocate storage for the texture image
  199. if (do_read) {
  200. pcNew->pcData = new aiTexel[pcNew->mWidth * pcNew->mHeight];
  201. }
  202. // R5G6B5 format (with or without MIPs)
  203. // ****************************************************************
  204. if (2 == iType || 10 == iType)
  205. {
  206. VALIDATE_FILE_SIZE(szData + pcNew->mWidth*pcNew->mHeight*2);
  207. // copy texture data
  208. unsigned int i;
  209. if (do_read)
  210. {
  211. for (i = 0; i < pcNew->mWidth*pcNew->mHeight;++i)
  212. {
  213. MDL::RGB565 val = ((MDL::RGB565*)szData)[i];
  214. AI_SWAP2(val);
  215. pcNew->pcData[i].a = 0xFF;
  216. pcNew->pcData[i].r = (unsigned char)val.b << 3;
  217. pcNew->pcData[i].g = (unsigned char)val.g << 2;
  218. pcNew->pcData[i].b = (unsigned char)val.r << 3;
  219. }
  220. }
  221. else i = pcNew->mWidth*pcNew->mHeight;
  222. *piSkip = i * 2;
  223. // apply MIP maps
  224. if (10 == iType)
  225. {
  226. *piSkip += ((i >> 2) + (i >> 4) + (i >> 6)) << 1;
  227. VALIDATE_FILE_SIZE(szData + *piSkip);
  228. }
  229. }
  230. // ARGB4 format (with or without MIPs)
  231. // ****************************************************************
  232. else if (3 == iType || 11 == iType)
  233. {
  234. VALIDATE_FILE_SIZE(szData + pcNew->mWidth*pcNew->mHeight*4);
  235. // copy texture data
  236. unsigned int i;
  237. if (do_read)
  238. {
  239. for (i = 0; i < pcNew->mWidth*pcNew->mHeight;++i)
  240. {
  241. MDL::ARGB4 val = ((MDL::ARGB4*)szData)[i];
  242. AI_SWAP2(val);
  243. pcNew->pcData[i].a = (unsigned char)val.a << 4;
  244. pcNew->pcData[i].r = (unsigned char)val.r << 4;
  245. pcNew->pcData[i].g = (unsigned char)val.g << 4;
  246. pcNew->pcData[i].b = (unsigned char)val.b << 4;
  247. }
  248. }
  249. else i = pcNew->mWidth*pcNew->mHeight;
  250. *piSkip = i * 2;
  251. // apply MIP maps
  252. if (11 == iType)
  253. {
  254. *piSkip += ((i >> 2) + (i >> 4) + (i >> 6)) << 1;
  255. VALIDATE_FILE_SIZE(szData + *piSkip);
  256. }
  257. }
  258. // RGB8 format (with or without MIPs)
  259. // ****************************************************************
  260. else if (4 == iType || 12 == iType)
  261. {
  262. VALIDATE_FILE_SIZE(szData + pcNew->mWidth*pcNew->mHeight*3);
  263. // copy texture data
  264. unsigned int i;
  265. if (do_read)
  266. {
  267. for (i = 0; i < pcNew->mWidth*pcNew->mHeight;++i)
  268. {
  269. const unsigned char* _szData = &szData[i*3];
  270. pcNew->pcData[i].a = 0xFF;
  271. pcNew->pcData[i].b = *_szData++;
  272. pcNew->pcData[i].g = *_szData++;
  273. pcNew->pcData[i].r = *_szData;
  274. }
  275. }
  276. else i = pcNew->mWidth*pcNew->mHeight;
  277. // apply MIP maps
  278. *piSkip = i * 3;
  279. if (12 == iType)
  280. {
  281. *piSkip += ((i >> 2) + (i >> 4) + (i >> 6)) *3;
  282. VALIDATE_FILE_SIZE(szData + *piSkip);
  283. }
  284. }
  285. // ARGB8 format (with ir without MIPs)
  286. // ****************************************************************
  287. else if (5 == iType || 13 == iType)
  288. {
  289. VALIDATE_FILE_SIZE(szData + pcNew->mWidth*pcNew->mHeight*4);
  290. // copy texture data
  291. unsigned int i;
  292. if (do_read)
  293. {
  294. for (i = 0; i < pcNew->mWidth*pcNew->mHeight;++i)
  295. {
  296. const unsigned char* _szData = &szData[i*4];
  297. pcNew->pcData[i].b = *_szData++;
  298. pcNew->pcData[i].g = *_szData++;
  299. pcNew->pcData[i].r = *_szData++;
  300. pcNew->pcData[i].a = *_szData;
  301. }
  302. }
  303. else i = pcNew->mWidth*pcNew->mHeight;
  304. // apply MIP maps
  305. *piSkip = i << 2;
  306. if (13 == iType)
  307. {
  308. *piSkip += ((i >> 2) + (i >> 4) + (i >> 6)) << 2;
  309. }
  310. }
  311. // palletized 8 bit texture. As for Quake 1
  312. // ****************************************************************
  313. else if (0 == iType)
  314. {
  315. VALIDATE_FILE_SIZE(szData + pcNew->mWidth*pcNew->mHeight);
  316. // copy texture data
  317. unsigned int i;
  318. if (do_read)
  319. {
  320. const unsigned char* szColorMap;
  321. SearchPalette(&szColorMap);
  322. for (i = 0; i < pcNew->mWidth*pcNew->mHeight;++i)
  323. {
  324. const unsigned char val = szData[i];
  325. const unsigned char* sz = &szColorMap[val*3];
  326. pcNew->pcData[i].a = 0xFF;
  327. pcNew->pcData[i].r = *sz++;
  328. pcNew->pcData[i].g = *sz++;
  329. pcNew->pcData[i].b = *sz;
  330. }
  331. this->FreePalette(szColorMap);
  332. }
  333. else i = pcNew->mWidth*pcNew->mHeight;
  334. *piSkip = i;
  335. // FIXME: Also support for MIP maps?
  336. }
  337. }
  338. // ------------------------------------------------------------------------------------------------
  339. // Get a texture from a MDL5 file
  340. void MDLImporter::CreateTexture_3DGS_MDL5(const unsigned char* szData,
  341. unsigned int iType,
  342. unsigned int* piSkip)
  343. {
  344. ai_assert(NULL != piSkip);
  345. bool bNoRead = *piSkip == UINT_MAX;
  346. // allocate a new texture object
  347. aiTexture* pcNew = new aiTexture();
  348. VALIDATE_FILE_SIZE(szData+8);
  349. // first read the size of the texture
  350. pcNew->mWidth = *((uint32_t*)szData);
  351. AI_SWAP4(pcNew->mWidth);
  352. szData += sizeof(uint32_t);
  353. pcNew->mHeight = *((uint32_t*)szData);
  354. AI_SWAP4(pcNew->mHeight);
  355. szData += sizeof(uint32_t);
  356. if (bNoRead) {
  357. pcNew->pcData = bad_texel;
  358. }
  359. // this should not occur - at least the docs say it shouldn't.
  360. // however, one can easily try out what MED does if you have
  361. // a model with a DDS texture and export it to MDL5 ...
  362. // yeah, it embedds the DDS file.
  363. if (6 == iType)
  364. {
  365. // this is a compressed texture in DDS format
  366. *piSkip = pcNew->mWidth;
  367. VALIDATE_FILE_SIZE(szData + *piSkip);
  368. if (!bNoRead)
  369. {
  370. // place a hint and let the application know that this is a DDS file
  371. pcNew->mHeight = 0;
  372. pcNew->achFormatHint[0] = 'd';
  373. pcNew->achFormatHint[1] = 'd';
  374. pcNew->achFormatHint[2] = 's';
  375. pcNew->achFormatHint[3] = '\0';
  376. pcNew->pcData = (aiTexel*) new unsigned char[pcNew->mWidth];
  377. ::memcpy(pcNew->pcData,szData,pcNew->mWidth);
  378. }
  379. }
  380. else
  381. {
  382. // parse the color data of the texture
  383. ParseTextureColorData(szData,iType,piSkip,pcNew);
  384. }
  385. *piSkip += sizeof(uint32_t) * 2;
  386. if (!bNoRead)
  387. {
  388. // store the texture
  389. if (!this->pScene->mNumTextures)
  390. {
  391. pScene->mNumTextures = 1;
  392. pScene->mTextures = new aiTexture*[1];
  393. pScene->mTextures[0] = pcNew;
  394. }
  395. else
  396. {
  397. aiTexture** pc = pScene->mTextures;
  398. pScene->mTextures = new aiTexture*[pScene->mNumTextures+1];
  399. for (unsigned int i = 0; i < pScene->mNumTextures;++i)
  400. this->pScene->mTextures[i] = pc[i];
  401. pScene->mTextures[pScene->mNumTextures] = pcNew;
  402. pScene->mNumTextures++;
  403. delete[] pc;
  404. }
  405. }
  406. else {
  407. pcNew->pcData = NULL;
  408. delete pcNew;
  409. }
  410. return;
  411. }
  412. // ------------------------------------------------------------------------------------------------
  413. // Get a skin from a MDL7 file - more complex than all other subformats
  414. void MDLImporter::ParseSkinLump_3DGS_MDL7(
  415. const unsigned char* szCurrent,
  416. const unsigned char** szCurrentOut,
  417. aiMaterial* pcMatOut,
  418. unsigned int iType,
  419. unsigned int iWidth,
  420. unsigned int iHeight)
  421. {
  422. std::unique_ptr<aiTexture> pcNew;
  423. // get the type of the skin
  424. unsigned int iMasked = (unsigned int)(iType & 0xF);
  425. if (0x1 == iMasked)
  426. {
  427. // ***** REFERENCE TO ANOTHER SKIN INDEX *****
  428. int referrer = (int)iWidth;
  429. pcMatOut->AddProperty<int>(&referrer,1,AI_MDL7_REFERRER_MATERIAL);
  430. }
  431. else if (0x6 == iMasked)
  432. {
  433. // ***** EMBEDDED DDS FILE *****
  434. if (1 != iHeight)
  435. {
  436. ASSIMP_LOG_WARN("Found a reference to an embedded DDS texture, "
  437. "but texture height is not equal to 1, which is not supported by MED");
  438. }
  439. pcNew.reset(new aiTexture());
  440. pcNew->mHeight = 0;
  441. pcNew->mWidth = iWidth;
  442. // place a proper format hint
  443. pcNew->achFormatHint[0] = 'd';
  444. pcNew->achFormatHint[1] = 'd';
  445. pcNew->achFormatHint[2] = 's';
  446. pcNew->achFormatHint[3] = '\0';
  447. pcNew->pcData = (aiTexel*) new unsigned char[pcNew->mWidth];
  448. memcpy(pcNew->pcData,szCurrent,pcNew->mWidth);
  449. szCurrent += iWidth;
  450. }
  451. else if (0x7 == iMasked)
  452. {
  453. // ***** REFERENCE TO EXTERNAL FILE *****
  454. if (1 != iHeight)
  455. {
  456. ASSIMP_LOG_WARN("Found a reference to an external texture, "
  457. "but texture height is not equal to 1, which is not supported by MED");
  458. }
  459. aiString szFile;
  460. const size_t iLen = strlen((const char*)szCurrent);
  461. size_t iLen2 = iLen+1;
  462. iLen2 = iLen2 > MAXLEN ? MAXLEN : iLen2;
  463. memcpy(szFile.data,(const char*)szCurrent,iLen2);
  464. szFile.length = iLen;
  465. szCurrent += iLen2;
  466. // place this as diffuse texture
  467. pcMatOut->AddProperty(&szFile,AI_MATKEY_TEXTURE_DIFFUSE(0));
  468. }
  469. else if (iMasked || !iType || (iType && iWidth && iHeight))
  470. {
  471. pcNew.reset(new aiTexture());
  472. if (!iHeight || !iWidth)
  473. {
  474. ASSIMP_LOG_WARN("Found embedded texture, but its width "
  475. "an height are both 0. Is this a joke?");
  476. // generate an empty chess pattern
  477. pcNew->mWidth = pcNew->mHeight = 8;
  478. pcNew->pcData = new aiTexel[64];
  479. for (unsigned int x = 0; x < 8;++x)
  480. {
  481. for (unsigned int y = 0; y < 8;++y)
  482. {
  483. const bool bSet = ((0 == x % 2 && 0 != y % 2) ||
  484. (0 != x % 2 && 0 == y % 2));
  485. aiTexel* pc = &pcNew->pcData[y * 8 + x];
  486. pc->r = pc->b = pc->g = (bSet?0xFF:0);
  487. pc->a = 0xFF;
  488. }
  489. }
  490. }
  491. else
  492. {
  493. // it is a standard color texture. Fill in width and height
  494. // and call the same function we used for loading MDL5 files
  495. pcNew->mWidth = iWidth;
  496. pcNew->mHeight = iHeight;
  497. unsigned int iSkip = 0;
  498. ParseTextureColorData(szCurrent,iMasked,&iSkip,pcNew.get());
  499. // skip length of texture data
  500. szCurrent += iSkip;
  501. }
  502. }
  503. // sometimes there are MDL7 files which have a monochrome
  504. // texture instead of material colors ... posssible they have
  505. // been converted to MDL7 from other formats, such as MDL5
  506. aiColor4D clrTexture;
  507. if (pcNew)clrTexture = ReplaceTextureWithColor(pcNew.get());
  508. else clrTexture.r = get_qnan();
  509. // check whether a material definition is contained in the skin
  510. if (iType & AI_MDL7_SKINTYPE_MATERIAL)
  511. {
  512. BE_NCONST MDL::Material_MDL7* pcMatIn = (BE_NCONST MDL::Material_MDL7*)szCurrent;
  513. szCurrent = (unsigned char*)(pcMatIn+1);
  514. VALIDATE_FILE_SIZE(szCurrent);
  515. aiColor3D clrTemp;
  516. #define COLOR_MULTIPLY_RGB() \
  517. if (is_not_qnan(clrTexture.r)) \
  518. { \
  519. clrTemp.r *= clrTexture.r; \
  520. clrTemp.g *= clrTexture.g; \
  521. clrTemp.b *= clrTexture.b; \
  522. }
  523. // read diffuse color
  524. clrTemp.r = pcMatIn->Diffuse.r;
  525. AI_SWAP4(clrTemp.r);
  526. clrTemp.g = pcMatIn->Diffuse.g;
  527. AI_SWAP4(clrTemp.g);
  528. clrTemp.b = pcMatIn->Diffuse.b;
  529. AI_SWAP4(clrTemp.b);
  530. COLOR_MULTIPLY_RGB();
  531. pcMatOut->AddProperty<aiColor3D>(&clrTemp,1,AI_MATKEY_COLOR_DIFFUSE);
  532. // read specular color
  533. clrTemp.r = pcMatIn->Specular.r;
  534. AI_SWAP4(clrTemp.r);
  535. clrTemp.g = pcMatIn->Specular.g;
  536. AI_SWAP4(clrTemp.g);
  537. clrTemp.b = pcMatIn->Specular.b;
  538. AI_SWAP4(clrTemp.b);
  539. COLOR_MULTIPLY_RGB();
  540. pcMatOut->AddProperty<aiColor3D>(&clrTemp,1,AI_MATKEY_COLOR_SPECULAR);
  541. // read ambient color
  542. clrTemp.r = pcMatIn->Ambient.r;
  543. AI_SWAP4(clrTemp.r);
  544. clrTemp.g = pcMatIn->Ambient.g;
  545. AI_SWAP4(clrTemp.g);
  546. clrTemp.b = pcMatIn->Ambient.b;
  547. AI_SWAP4(clrTemp.b);
  548. COLOR_MULTIPLY_RGB();
  549. pcMatOut->AddProperty<aiColor3D>(&clrTemp,1,AI_MATKEY_COLOR_AMBIENT);
  550. // read emissive color
  551. clrTemp.r = pcMatIn->Emissive.r;
  552. AI_SWAP4(clrTemp.r);
  553. clrTemp.g = pcMatIn->Emissive.g;
  554. AI_SWAP4(clrTemp.g);
  555. clrTemp.b = pcMatIn->Emissive.b;
  556. AI_SWAP4(clrTemp.b);
  557. pcMatOut->AddProperty<aiColor3D>(&clrTemp,1,AI_MATKEY_COLOR_EMISSIVE);
  558. #undef COLOR_MULITPLY_RGB
  559. // FIX: Take the opacity from the ambient color.
  560. // The doc say something else, but it is fact that MED exports the
  561. // opacity like this .... oh well.
  562. clrTemp.r = pcMatIn->Ambient.a;
  563. AI_SWAP4(clrTemp.r);
  564. if (is_not_qnan(clrTexture.r)) {
  565. clrTemp.r *= clrTexture.a;
  566. }
  567. pcMatOut->AddProperty<ai_real>(&clrTemp.r,1,AI_MATKEY_OPACITY);
  568. // read phong power
  569. int iShadingMode = (int)aiShadingMode_Gouraud;
  570. AI_SWAP4(pcMatIn->Power);
  571. if (0.0f != pcMatIn->Power)
  572. {
  573. iShadingMode = (int)aiShadingMode_Phong;
  574. // pcMatIn is packed, we can't form pointers to its members
  575. float power = pcMatIn->Power;
  576. pcMatOut->AddProperty<float>(&power,1,AI_MATKEY_SHININESS);
  577. }
  578. pcMatOut->AddProperty<int>(&iShadingMode,1,AI_MATKEY_SHADING_MODEL);
  579. }
  580. else if (is_not_qnan(clrTexture.r))
  581. {
  582. pcMatOut->AddProperty<aiColor4D>(&clrTexture,1,AI_MATKEY_COLOR_DIFFUSE);
  583. pcMatOut->AddProperty<aiColor4D>(&clrTexture,1,AI_MATKEY_COLOR_SPECULAR);
  584. }
  585. // if the texture could be replaced by a single material color
  586. // we don't need the texture anymore
  587. if (is_not_qnan(clrTexture.r))
  588. {
  589. pcNew.reset();
  590. }
  591. // If an ASCII effect description (HLSL?) is contained in the file,
  592. // we can simply ignore it ...
  593. if (iType & AI_MDL7_SKINTYPE_MATERIAL_ASCDEF)
  594. {
  595. VALIDATE_FILE_SIZE(szCurrent);
  596. int32_t iMe = *((int32_t*)szCurrent);
  597. AI_SWAP4(iMe);
  598. szCurrent += sizeof(char) * iMe + sizeof(int32_t);
  599. VALIDATE_FILE_SIZE(szCurrent);
  600. }
  601. // If an embedded texture has been loaded setup the corresponding
  602. // data structures in the aiScene instance
  603. if (pcNew && pScene->mNumTextures <= 999)
  604. {
  605. // place this as diffuse texture
  606. char szCurrent[5];
  607. ai_snprintf(szCurrent,5,"*%i",this->pScene->mNumTextures);
  608. aiString szFile;
  609. const size_t iLen = strlen((const char*)szCurrent);
  610. ::memcpy(szFile.data,(const char*)szCurrent,iLen+1);
  611. szFile.length = iLen;
  612. pcMatOut->AddProperty(&szFile,AI_MATKEY_TEXTURE_DIFFUSE(0));
  613. // store the texture
  614. if (!pScene->mNumTextures)
  615. {
  616. pScene->mNumTextures = 1;
  617. pScene->mTextures = new aiTexture*[1];
  618. pScene->mTextures[0] = pcNew.release();
  619. }
  620. else
  621. {
  622. aiTexture** pc = pScene->mTextures;
  623. pScene->mTextures = new aiTexture*[pScene->mNumTextures+1];
  624. for (unsigned int i = 0; i < pScene->mNumTextures;++i) {
  625. pScene->mTextures[i] = pc[i];
  626. }
  627. pScene->mTextures[pScene->mNumTextures] = pcNew.release();
  628. pScene->mNumTextures++;
  629. delete[] pc;
  630. }
  631. }
  632. VALIDATE_FILE_SIZE(szCurrent);
  633. *szCurrentOut = szCurrent;
  634. }
  635. // ------------------------------------------------------------------------------------------------
  636. // Skip a skin lump
  637. void MDLImporter::SkipSkinLump_3DGS_MDL7(
  638. const unsigned char* szCurrent,
  639. const unsigned char** szCurrentOut,
  640. unsigned int iType,
  641. unsigned int iWidth,
  642. unsigned int iHeight)
  643. {
  644. // get the type of the skin
  645. const unsigned int iMasked = (unsigned int)(iType & 0xF);
  646. if (0x6 == iMasked)
  647. {
  648. szCurrent += iWidth;
  649. }
  650. if (0x7 == iMasked)
  651. {
  652. const size_t iLen = ::strlen((const char*)szCurrent);
  653. szCurrent += iLen+1;
  654. }
  655. else if (iMasked || !iType)
  656. {
  657. if (iMasked || !iType || (iType && iWidth && iHeight))
  658. {
  659. // ParseTextureColorData(..., aiTexture::pcData == bad_texel) will simply
  660. // return the size of the color data in bytes in iSkip
  661. unsigned int iSkip = 0;
  662. aiTexture tex;
  663. tex.pcData = bad_texel;
  664. tex.mHeight = iHeight;
  665. tex.mWidth = iWidth;
  666. ParseTextureColorData(szCurrent,iMasked,&iSkip,&tex);
  667. // FIX: Important, otherwise the destructor will crash
  668. tex.pcData = NULL;
  669. // skip length of texture data
  670. szCurrent += iSkip;
  671. }
  672. }
  673. // check whether a material definition is contained in the skin
  674. if (iType & AI_MDL7_SKINTYPE_MATERIAL)
  675. {
  676. BE_NCONST MDL::Material_MDL7* pcMatIn = (BE_NCONST MDL::Material_MDL7*)szCurrent;
  677. szCurrent = (unsigned char*)(pcMatIn+1);
  678. }
  679. // if an ASCII effect description (HLSL?) is contained in the file,
  680. // we can simply ignore it ...
  681. if (iType & AI_MDL7_SKINTYPE_MATERIAL_ASCDEF)
  682. {
  683. int32_t iMe = *((int32_t*)szCurrent);
  684. AI_SWAP4(iMe);
  685. szCurrent += sizeof(char) * iMe + sizeof(int32_t);
  686. }
  687. *szCurrentOut = szCurrent;
  688. }
  689. // ------------------------------------------------------------------------------------------------
  690. void MDLImporter::ParseSkinLump_3DGS_MDL7(
  691. const unsigned char* szCurrent,
  692. const unsigned char** szCurrentOut,
  693. std::vector<aiMaterial*>& pcMats)
  694. {
  695. ai_assert(NULL != szCurrent);
  696. ai_assert(NULL != szCurrentOut);
  697. *szCurrentOut = szCurrent;
  698. BE_NCONST MDL::Skin_MDL7* pcSkin = (BE_NCONST MDL::Skin_MDL7*)szCurrent;
  699. AI_SWAP4(pcSkin->width);
  700. AI_SWAP4(pcSkin->height);
  701. szCurrent += 12;
  702. // allocate an output material
  703. aiMaterial* pcMatOut = new aiMaterial();
  704. pcMats.push_back(pcMatOut);
  705. // skip length of file name
  706. szCurrent += AI_MDL7_MAX_TEXNAMESIZE;
  707. ParseSkinLump_3DGS_MDL7(szCurrent,szCurrentOut,pcMatOut,
  708. pcSkin->typ,pcSkin->width,pcSkin->height);
  709. // place the name of the skin in the material
  710. if (pcSkin->texture_name[0])
  711. {
  712. // the 0 termination could be there or not - we can't know
  713. aiString szFile;
  714. ::memcpy(szFile.data,pcSkin->texture_name,sizeof(pcSkin->texture_name));
  715. szFile.data[sizeof(pcSkin->texture_name)] = '\0';
  716. szFile.length = ::strlen(szFile.data);
  717. pcMatOut->AddProperty(&szFile,AI_MATKEY_NAME);
  718. }
  719. }
  720. #endif // !! ASSIMP_BUILD_NO_MDL_IMPORTER