MDLMaterialLoader.cpp 28 KB

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