MDLMaterialLoader.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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 {
  174. pcNew->pcData = NULL;
  175. delete pcNew;
  176. }
  177. return;
  178. }
  179. // ------------------------------------------------------------------------------------------------
  180. // Load color data of a texture and convert it to our output format
  181. void MDLImporter::ParseTextureColorData(const unsigned char* szData,
  182. unsigned int iType,
  183. unsigned int* piSkip,
  184. aiTexture* pcNew)
  185. {
  186. // allocate storage for the texture image
  187. if ((aiTexel*)0xffffffff != pcNew->pcData)
  188. pcNew->pcData = new aiTexel[pcNew->mWidth * pcNew->mHeight];
  189. // R5G6B5 format (with or without MIPs)
  190. // ****************************************************************
  191. if (2 == iType || 10 == iType)
  192. {
  193. VALIDATE_FILE_SIZE(szData + pcNew->mWidth*pcNew->mHeight*2);
  194. // copy texture data
  195. unsigned int i;
  196. if ((aiTexel*)0xffffffff != pcNew->pcData)
  197. {
  198. for (i = 0; i < pcNew->mWidth*pcNew->mHeight;++i)
  199. {
  200. MDL::RGB565 val = ((MDL::RGB565*)szData)[i];
  201. AI_SWAP2(val);
  202. pcNew->pcData[i].a = 0xFF;
  203. pcNew->pcData[i].r = (unsigned char)val.b << 3;
  204. pcNew->pcData[i].g = (unsigned char)val.g << 2;
  205. pcNew->pcData[i].b = (unsigned char)val.r << 3;
  206. }
  207. }
  208. else i = pcNew->mWidth*pcNew->mHeight;
  209. *piSkip = i * 2;
  210. // apply MIP maps
  211. if (10 == iType)
  212. {
  213. *piSkip += ((i >> 2) + (i >> 4) + (i >> 6)) << 1;
  214. VALIDATE_FILE_SIZE(szData + *piSkip);
  215. }
  216. }
  217. // ARGB4 format (with or without MIPs)
  218. // ****************************************************************
  219. else if (3 == iType || 11 == iType)
  220. {
  221. VALIDATE_FILE_SIZE(szData + pcNew->mWidth*pcNew->mHeight*4);
  222. // copy texture data
  223. unsigned int i;
  224. if ((aiTexel*)0xffffffff != pcNew->pcData)
  225. {
  226. for (i = 0; i < pcNew->mWidth*pcNew->mHeight;++i)
  227. {
  228. MDL::ARGB4 val = ((MDL::ARGB4*)szData)[i];
  229. AI_SWAP2(val);
  230. pcNew->pcData[i].a = (unsigned char)val.a << 4;
  231. pcNew->pcData[i].r = (unsigned char)val.r << 4;
  232. pcNew->pcData[i].g = (unsigned char)val.g << 4;
  233. pcNew->pcData[i].b = (unsigned char)val.b << 4;
  234. }
  235. }
  236. else i = pcNew->mWidth*pcNew->mHeight;
  237. *piSkip = i * 2;
  238. // apply MIP maps
  239. if (11 == iType)
  240. {
  241. *piSkip += ((i >> 2) + (i >> 4) + (i >> 6)) << 1;
  242. VALIDATE_FILE_SIZE(szData + *piSkip);
  243. }
  244. }
  245. // RGB8 format (with or without MIPs)
  246. // ****************************************************************
  247. else if (4 == iType || 12 == iType)
  248. {
  249. VALIDATE_FILE_SIZE(szData + pcNew->mWidth*pcNew->mHeight*3);
  250. // copy texture data
  251. unsigned int i;
  252. if ((aiTexel*)0xffffffff != pcNew->pcData)
  253. {
  254. for (i = 0; i < pcNew->mWidth*pcNew->mHeight;++i)
  255. {
  256. const unsigned char* _szData = &szData[i*3];
  257. pcNew->pcData[i].a = 0xFF;
  258. pcNew->pcData[i].b = *_szData++;
  259. pcNew->pcData[i].g = *_szData++;
  260. pcNew->pcData[i].r = *_szData;
  261. }
  262. }
  263. else i = pcNew->mWidth*pcNew->mHeight;
  264. // apply MIP maps
  265. *piSkip = i * 3;
  266. if (12 == iType)
  267. {
  268. *piSkip += ((i >> 2) + (i >> 4) + (i >> 6)) *3;
  269. VALIDATE_FILE_SIZE(szData + *piSkip);
  270. }
  271. }
  272. // ARGB8 format (with ir without MIPs)
  273. // ****************************************************************
  274. else if (5 == iType || 13 == iType)
  275. {
  276. VALIDATE_FILE_SIZE(szData + pcNew->mWidth*pcNew->mHeight*4);
  277. // copy texture data
  278. unsigned int i;
  279. if ((aiTexel*)0xffffffff != pcNew->pcData)
  280. {
  281. for (i = 0; i < pcNew->mWidth*pcNew->mHeight;++i)
  282. {
  283. const unsigned char* _szData = &szData[i*4];
  284. pcNew->pcData[i].b = *_szData++;
  285. pcNew->pcData[i].g = *_szData++;
  286. pcNew->pcData[i].r = *_szData++;
  287. pcNew->pcData[i].a = *_szData;
  288. }
  289. }
  290. else i = pcNew->mWidth*pcNew->mHeight;
  291. // apply MIP maps
  292. *piSkip = i << 2;
  293. if (13 == iType)
  294. {
  295. *piSkip += ((i >> 2) + (i >> 4) + (i >> 6)) << 2;
  296. }
  297. }
  298. // palletized 8 bit texture. As for Quake 1
  299. // ****************************************************************
  300. else if (0 == iType)
  301. {
  302. VALIDATE_FILE_SIZE(szData + pcNew->mWidth*pcNew->mHeight);
  303. // copy texture data
  304. unsigned int i;
  305. if ((aiTexel*)0xffffffff != pcNew->pcData)
  306. {
  307. const unsigned char* szColorMap;
  308. this->SearchPalette(&szColorMap);
  309. for (i = 0; i < pcNew->mWidth*pcNew->mHeight;++i)
  310. {
  311. const unsigned char val = szData[i];
  312. const unsigned char* sz = &szColorMap[val*3];
  313. pcNew->pcData[i].a = 0xFF;
  314. pcNew->pcData[i].r = *sz++;
  315. pcNew->pcData[i].g = *sz++;
  316. pcNew->pcData[i].b = *sz;
  317. }
  318. this->FreePalette(szColorMap);
  319. }
  320. else i = pcNew->mWidth*pcNew->mHeight;
  321. *piSkip = i;
  322. // FIXME: Also support for MIP maps?
  323. }
  324. }
  325. // ------------------------------------------------------------------------------------------------
  326. // Get a texture from a MDL5 file
  327. void MDLImporter::CreateTexture_3DGS_MDL5(const unsigned char* szData,
  328. unsigned int iType,
  329. unsigned int* piSkip)
  330. {
  331. ai_assert(NULL != piSkip);
  332. bool bNoRead = *piSkip == 0xffffffff;
  333. // allocate a new texture object
  334. aiTexture* pcNew = new aiTexture();
  335. VALIDATE_FILE_SIZE(szData+8);
  336. // first read the size of the texture
  337. pcNew->mWidth = *((uint32_t*)szData);
  338. AI_SWAP4(pcNew->mWidth);
  339. szData += sizeof(uint32_t);
  340. pcNew->mHeight = *((uint32_t*)szData);
  341. AI_SWAP4(pcNew->mHeight);
  342. szData += sizeof(uint32_t);
  343. if (bNoRead)pcNew->pcData = (aiTexel*)0xffffffff;
  344. // this should not occur - at least the docs say it shouldn't
  345. // however, you can easily try out what MED does if you have
  346. // a model with a DDS texture and export it to MDL5 ...
  347. // yes, you're right. It embedds the DDS texture ... :cry:
  348. if (6 == iType)
  349. {
  350. // this is a compressed texture in DDS format
  351. *piSkip = pcNew->mWidth;
  352. VALIDATE_FILE_SIZE(szData + *piSkip);
  353. if (!bNoRead)
  354. {
  355. // place a hint and let the application know that it's
  356. // a DDS file
  357. pcNew->mHeight = 0;
  358. pcNew->achFormatHint[0] = 'd';
  359. pcNew->achFormatHint[1] = 'd';
  360. pcNew->achFormatHint[2] = 's';
  361. pcNew->achFormatHint[3] = '\0';
  362. pcNew->pcData = (aiTexel*) new unsigned char[pcNew->mWidth];
  363. ::memcpy(pcNew->pcData,szData,pcNew->mWidth);
  364. }
  365. }
  366. else
  367. {
  368. // parse the color data of the texture
  369. ParseTextureColorData(szData,iType,
  370. piSkip,pcNew);
  371. }
  372. *piSkip += sizeof(uint32_t) * 2;
  373. if (!bNoRead)
  374. {
  375. // store the texture
  376. if (!this->pScene->mNumTextures)
  377. {
  378. pScene->mNumTextures = 1;
  379. pScene->mTextures = new aiTexture*[1];
  380. pScene->mTextures[0] = pcNew;
  381. }
  382. else
  383. {
  384. aiTexture** pc = pScene->mTextures;
  385. pScene->mTextures = new aiTexture*[pScene->mNumTextures+1];
  386. for (unsigned int i = 0; i < pScene->mNumTextures;++i)
  387. this->pScene->mTextures[i] = pc[i];
  388. pScene->mTextures[pScene->mNumTextures] = pcNew;
  389. pScene->mNumTextures++;
  390. delete[] pc;
  391. }
  392. }
  393. else delete pcNew;
  394. return;
  395. }
  396. // ------------------------------------------------------------------------------------------------
  397. // Get a skin from a MDL7 file - more complex than all other subformats
  398. void MDLImporter::ParseSkinLump_3DGS_MDL7(
  399. const unsigned char* szCurrent,
  400. const unsigned char** szCurrentOut,
  401. MaterialHelper* pcMatOut,
  402. unsigned int iType,
  403. unsigned int iWidth,
  404. unsigned int iHeight)
  405. {
  406. aiTexture* pcNew = NULL;
  407. // get the type of the skin
  408. unsigned int iMasked = (unsigned int)(iType & 0xF);
  409. if (0x1 == iMasked)
  410. {
  411. // ***** REFERENCE TO ANOTHER SKIN INDEX *****
  412. int referrer = (int)iWidth;
  413. pcMatOut->AddProperty<int>(&referrer,1,AI_MDL7_REFERRER_MATERIAL);
  414. }
  415. else if (0x6 == iMasked)
  416. {
  417. // ***** EMBEDDED DDS FILE *****
  418. if (1 != iHeight)
  419. {
  420. DefaultLogger::get()->warn("Found a reference to an embedded DDS texture, "
  421. "but texture height is not equal to 1, which is not supported by MED");
  422. }
  423. pcNew = new aiTexture();
  424. pcNew->mHeight = 0;
  425. pcNew->mWidth = iWidth;
  426. // place a proper format hint
  427. pcNew->achFormatHint[0] = 'd';
  428. pcNew->achFormatHint[1] = 'd';
  429. pcNew->achFormatHint[2] = 's';
  430. pcNew->achFormatHint[3] = '\0';
  431. pcNew->pcData = (aiTexel*) new unsigned char[pcNew->mWidth];
  432. memcpy(pcNew->pcData,szCurrent,pcNew->mWidth);
  433. szCurrent += iWidth;
  434. }
  435. if (0x7 == iMasked)
  436. {
  437. // ***** REFERENCE TO EXTERNAL FILE *****
  438. if (1 != iHeight)
  439. {
  440. DefaultLogger::get()->warn("Found a reference to an external texture, "
  441. "but texture height is not equal to 1, which is not supported by MED");
  442. }
  443. aiString szFile;
  444. const size_t iLen = strlen((const char*)szCurrent);
  445. size_t iLen2 = iLen+1;
  446. iLen2 = iLen2 > MAXLEN ? MAXLEN : iLen2;
  447. memcpy(szFile.data,(const char*)szCurrent,iLen2);
  448. szFile.length = iLen;
  449. szCurrent += iLen2;
  450. // place this as diffuse texture
  451. pcMatOut->AddProperty(&szFile,AI_MATKEY_TEXTURE_DIFFUSE(0));
  452. }
  453. else if (iMasked || !iType || (iType && iWidth && iHeight))
  454. {
  455. // ***** STANDARD COLOR TEXTURE *****
  456. pcNew = new aiTexture();
  457. if (!iHeight || !iWidth)
  458. {
  459. DefaultLogger::get()->warn("Found embedded texture, but its width "
  460. "an height are both 0. Is this a joke?");
  461. // generate an empty chess pattern
  462. pcNew->mWidth = pcNew->mHeight = 8;
  463. pcNew->pcData = new aiTexel[64];
  464. for (unsigned int x = 0; x < 8;++x)
  465. {
  466. for (unsigned int y = 0; y < 8;++y)
  467. {
  468. const bool bSet = ((0 == x % 2 && 0 != y % 2) ||
  469. (0 != x % 2 && 0 == y % 2));
  470. aiTexel* pc = &pcNew->pcData[y * 8 + x];
  471. pc->r = pc->b = pc->g = (bSet?0xFF:0);
  472. pc->a = 0xFF;
  473. }
  474. }
  475. }
  476. else
  477. {
  478. // it is a standard color texture. Fill in width and height
  479. // and call the same function we used for loading MDL5 files
  480. pcNew->mWidth = iWidth;
  481. pcNew->mHeight = iHeight;
  482. unsigned int iSkip = 0;
  483. this->ParseTextureColorData(szCurrent,iMasked,&iSkip,pcNew);
  484. // skip length of texture data
  485. szCurrent += iSkip;
  486. }
  487. }
  488. // sometimes there are MDL7 files which have a monochrome
  489. // texture instead of material colors ... posssible they have
  490. // been converted to MDL7 from other formats, such as MDL5
  491. aiColor4D clrTexture;
  492. if (pcNew)clrTexture = this->ReplaceTextureWithColor(pcNew);
  493. else clrTexture.r = get_qnan();
  494. // check whether a material definition is contained in the skin
  495. if (iType & AI_MDL7_SKINTYPE_MATERIAL)
  496. {
  497. BE_NCONST MDL::Material_MDL7* pcMatIn = (BE_NCONST MDL::Material_MDL7*)szCurrent;
  498. szCurrent = (unsigned char*)(pcMatIn+1);
  499. VALIDATE_FILE_SIZE(szCurrent);
  500. aiColor3D clrTemp;
  501. #define COLOR_MULTIPLY_RGB() \
  502. if (is_not_qnan(clrTexture.r)) \
  503. { \
  504. clrTemp.r *= clrTexture.r; \
  505. clrTemp.g *= clrTexture.g; \
  506. clrTemp.b *= clrTexture.b; \
  507. }
  508. // read diffuse color
  509. clrTemp.r = pcMatIn->Diffuse.r;
  510. AI_SWAP4(clrTemp.r);
  511. clrTemp.g = pcMatIn->Diffuse.g;
  512. AI_SWAP4(clrTemp.g);
  513. clrTemp.b = pcMatIn->Diffuse.b;
  514. AI_SWAP4(clrTemp.b);
  515. COLOR_MULTIPLY_RGB();
  516. pcMatOut->AddProperty<aiColor3D>(&clrTemp,1,AI_MATKEY_COLOR_DIFFUSE);
  517. // read specular color
  518. clrTemp.r = pcMatIn->Specular.r;
  519. AI_SWAP4(clrTemp.r);
  520. clrTemp.g = pcMatIn->Specular.g;
  521. AI_SWAP4(clrTemp.g);
  522. clrTemp.b = pcMatIn->Specular.b;
  523. AI_SWAP4(clrTemp.b);
  524. COLOR_MULTIPLY_RGB();
  525. pcMatOut->AddProperty<aiColor3D>(&clrTemp,1,AI_MATKEY_COLOR_SPECULAR);
  526. // read ambient color
  527. clrTemp.r = pcMatIn->Ambient.r;
  528. AI_SWAP4(clrTemp.r);
  529. clrTemp.g = pcMatIn->Ambient.g;
  530. AI_SWAP4(clrTemp.g);
  531. clrTemp.b = pcMatIn->Ambient.b;
  532. AI_SWAP4(clrTemp.b);
  533. COLOR_MULTIPLY_RGB();
  534. pcMatOut->AddProperty<aiColor3D>(&clrTemp,1,AI_MATKEY_COLOR_AMBIENT);
  535. // read emissive color
  536. clrTemp.r = pcMatIn->Emissive.r;
  537. AI_SWAP4(clrTemp.r);
  538. clrTemp.g = pcMatIn->Emissive.g;
  539. AI_SWAP4(clrTemp.g);
  540. clrTemp.b = pcMatIn->Emissive.b;
  541. AI_SWAP4(clrTemp.b);
  542. pcMatOut->AddProperty<aiColor3D>(&clrTemp,1,AI_MATKEY_COLOR_EMISSIVE);
  543. #undef COLOR_MULITPLY_RGB
  544. // FIX: Take the opacity from the ambient color
  545. // the doc says something else, but it is fact that MED exports the
  546. // opacity like this .... ARRRGGHH!
  547. clrTemp.r = pcMatIn->Ambient.a;
  548. AI_SWAP4(clrTemp.r);
  549. if (is_not_qnan(clrTexture.r))clrTemp.r *= clrTexture.a;
  550. pcMatOut->AddProperty<float>(&clrTemp.r,1,AI_MATKEY_OPACITY);
  551. // read phong power
  552. int iShadingMode = (int)aiShadingMode_Gouraud;
  553. AI_SWAP4(pcMatIn->Power);
  554. if (0.0f != pcMatIn->Power)
  555. {
  556. iShadingMode = (int)aiShadingMode_Phong;
  557. pcMatOut->AddProperty<float>(&pcMatIn->Power,1,AI_MATKEY_SHININESS);
  558. }
  559. pcMatOut->AddProperty<int>(&iShadingMode,1,AI_MATKEY_SHADING_MODEL);
  560. }
  561. else if (is_not_qnan(clrTexture.r))
  562. {
  563. pcMatOut->AddProperty<aiColor4D>(&clrTexture,1,AI_MATKEY_COLOR_DIFFUSE);
  564. pcMatOut->AddProperty<aiColor4D>(&clrTexture,1,AI_MATKEY_COLOR_SPECULAR);
  565. }
  566. // if the texture could be replaced by a single material color
  567. // we don't need the texture anymore
  568. if (is_not_qnan(clrTexture.r))
  569. {
  570. delete pcNew;
  571. pcNew = NULL;
  572. }
  573. // if an ASCII effect description (HLSL?) is contained in the file,
  574. // we can simply ignore it ...
  575. if (iType & AI_MDL7_SKINTYPE_MATERIAL_ASCDEF)
  576. {
  577. VALIDATE_FILE_SIZE(szCurrent);
  578. int32_t iMe = *((int32_t*)szCurrent);
  579. AI_SWAP4(iMe);
  580. szCurrent += sizeof(char) * iMe + sizeof(int32_t);
  581. VALIDATE_FILE_SIZE(szCurrent);
  582. }
  583. // If an embedded texture has been loaded setup the corresponding
  584. // data structures in the aiScene instance
  585. if (pcNew && this->pScene->mNumTextures <= 999)
  586. {
  587. // place this as diffuse texture
  588. char szCurrent[5];
  589. ::sprintf(szCurrent,"*%i",this->pScene->mNumTextures);
  590. aiString szFile;
  591. const size_t iLen = strlen((const char*)szCurrent);
  592. ::memcpy(szFile.data,(const char*)szCurrent,iLen+1);
  593. szFile.length = iLen;
  594. pcMatOut->AddProperty(&szFile,AI_MATKEY_TEXTURE_DIFFUSE(0));
  595. // store the texture
  596. if (!this->pScene->mNumTextures)
  597. {
  598. this->pScene->mNumTextures = 1;
  599. this->pScene->mTextures = new aiTexture*[1];
  600. this->pScene->mTextures[0] = pcNew;
  601. }
  602. else
  603. {
  604. aiTexture** pc = this->pScene->mTextures;
  605. this->pScene->mTextures = new aiTexture*[this->pScene->mNumTextures+1];
  606. for (unsigned int i = 0; i < this->pScene->mNumTextures;++i)
  607. this->pScene->mTextures[i] = pc[i];
  608. this->pScene->mTextures[this->pScene->mNumTextures] = pcNew;
  609. this->pScene->mNumTextures++;
  610. delete[] pc;
  611. }
  612. }
  613. VALIDATE_FILE_SIZE(szCurrent);
  614. *szCurrentOut = szCurrent;
  615. }
  616. // ------------------------------------------------------------------------------------------------
  617. // Skip a skin lump
  618. void MDLImporter::SkipSkinLump_3DGS_MDL7(
  619. const unsigned char* szCurrent,
  620. const unsigned char** szCurrentOut,
  621. unsigned int iType,
  622. unsigned int iWidth,
  623. unsigned int iHeight)
  624. {
  625. // get the type of the skin
  626. unsigned int iMasked = (unsigned int)(iType & 0xF);
  627. if (0x6 == iMasked)
  628. {
  629. szCurrent += iWidth;
  630. }
  631. if (0x7 == iMasked)
  632. {
  633. const size_t iLen = ::strlen((const char*)szCurrent);
  634. szCurrent += iLen+1;
  635. }
  636. else if (iMasked || !iType)
  637. {
  638. if (iMasked || !iType || (iType && iWidth && iHeight))
  639. {
  640. // ParseTextureColorData(..., aiTexture::pcData == 0xffffffff) will simply
  641. // return the size of the color data in bytes in iSkip
  642. unsigned int iSkip = 0;
  643. aiTexture tex;
  644. tex.pcData = reinterpret_cast<aiTexel*>(0xffffffff);
  645. tex.mHeight = iHeight;
  646. tex.mWidth = iWidth;
  647. this->ParseTextureColorData(szCurrent,iMasked,&iSkip,&tex);
  648. // FIX: Important, otherwise the destructor will crash
  649. tex.pcData = NULL;
  650. // skip length of texture data
  651. szCurrent += iSkip;
  652. }
  653. }
  654. // check whether a material definition is contained in the skin
  655. if (iType & AI_MDL7_SKINTYPE_MATERIAL)
  656. {
  657. BE_NCONST MDL::Material_MDL7* pcMatIn = (BE_NCONST MDL::Material_MDL7*)szCurrent;
  658. szCurrent = (unsigned char*)(pcMatIn+1);
  659. }
  660. // if an ASCII effect description (HLSL?) is contained in the file,
  661. // we can simply ignore it ...
  662. if (iType & AI_MDL7_SKINTYPE_MATERIAL_ASCDEF)
  663. {
  664. int32_t iMe = *((int32_t*)szCurrent);
  665. AI_SWAP4(iMe);
  666. szCurrent += sizeof(char) * iMe + sizeof(int32_t);
  667. }
  668. *szCurrentOut = szCurrent;
  669. }
  670. // ------------------------------------------------------------------------------------------------
  671. // What the fuck does this function do? Can't remember
  672. void MDLImporter::ParseSkinLump_3DGS_MDL7(
  673. const unsigned char* szCurrent,
  674. const unsigned char** szCurrentOut,
  675. std::vector<MaterialHelper*>& pcMats)
  676. {
  677. ai_assert(NULL != szCurrent);
  678. ai_assert(NULL != szCurrentOut);
  679. *szCurrentOut = szCurrent;
  680. BE_NCONST MDL::Skin_MDL7* pcSkin = (BE_NCONST MDL::Skin_MDL7*)szCurrent;
  681. AI_SWAP4(pcSkin->width);
  682. AI_SWAP4(pcSkin->height);
  683. szCurrent += 12;
  684. // allocate an output material
  685. MaterialHelper* pcMatOut = new MaterialHelper();
  686. pcMats.push_back(pcMatOut);
  687. // skip length of file name
  688. szCurrent += AI_MDL7_MAX_TEXNAMESIZE;
  689. ParseSkinLump_3DGS_MDL7(szCurrent,szCurrentOut,pcMatOut,
  690. pcSkin->typ,pcSkin->width,pcSkin->height);
  691. // place the name of the skin in the material
  692. if (pcSkin->texture_name[0])
  693. {
  694. // the 0 termination could be there or not - we can't know
  695. aiString szFile;
  696. ::memcpy(szFile.data,pcSkin->texture_name,sizeof(pcSkin->texture_name));
  697. szFile.data[sizeof(pcSkin->texture_name)] = '\0';
  698. szFile.length = ::strlen(szFile.data);
  699. pcMatOut->AddProperty(&szFile,AI_MATKEY_NAME);
  700. }
  701. }
  702. #endif // !! ASSIMP_BUILD_NO_MDL_IMPORTER