MDLMaterialLoader.cpp 24 KB

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