MDLMaterialLoader.cpp 23 KB

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