MDLMaterialLoader.cpp 27 KB

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