assimp/code/AssetLib/MDL/MDLMaterialLoader.cpp

777 lines
27 KiB
C++
Raw Normal View History

/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
2022-01-10 20:13:43 +00:00
Copyright (c) 2006-2022, assimp team
2018-01-28 18:42:05 +00:00
All rights reserved.
2015-05-19 03:52:10 +00:00
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the following
conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
2015-05-19 03:52:10 +00:00
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2015-05-19 03:52:10 +00:00
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2015-05-19 03:52:10 +00:00
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2015-05-19 03:52:10 +00:00
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------
*/
/** @file Implementation of the material part of the MDL importer class */
#ifndef ASSIMP_BUILD_NO_MDL_IMPORTER
#include "MDLDefaultColorMap.h"
2020-03-22 13:21:24 +00:00
#include "MDLLoader.h"
2021-09-13 20:38:20 +00:00
#include <assimp/StringUtils.h>
2020-03-22 13:21:24 +00:00
#include <assimp/qnan.h>
#include <assimp/scene.h>
2016-06-06 20:04:29 +00:00
#include <assimp/texture.h>
#include <assimp/DefaultLogger.hpp>
2020-03-22 13:21:24 +00:00
#include <assimp/IOSystem.hpp>
#include <memory>
using namespace Assimp;
2020-03-22 13:21:24 +00:00
static aiTexel *const bad_texel = reinterpret_cast<aiTexel *>(SIZE_MAX);
// ------------------------------------------------------------------------------------------------
2018-04-26 12:10:18 +00:00
// Find a suitable palette file or take the default one
2020-03-22 13:21:24 +00:00
void MDLImporter::SearchPalette(const unsigned char **pszColorMap) {
2015-05-19 03:57:13 +00:00
// now try to find the color map in the current directory
2020-03-22 13:21:24 +00:00
IOStream *pcStream = mIOHandler->Open(configPalette, "rb");
2015-05-19 03:57:13 +00:00
2020-03-22 13:21:24 +00:00
const unsigned char *szColorMap = (const unsigned char *)::g_aclrDefaultColorMap;
if (pcStream) {
if (pcStream->FileSize() >= 768) {
2018-04-26 13:05:49 +00:00
size_t len = 256 * 3;
2020-03-22 13:21:24 +00:00
unsigned char *colorMap = new unsigned char[len];
2015-05-19 03:57:13 +00:00
szColorMap = colorMap;
2020-03-22 13:21:24 +00:00
pcStream->Read(colorMap, len, 1);
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_INFO("Found valid colormap.lmp in directory. "
2020-03-22 13:21:24 +00:00
"It will be used to decode embedded textures in palletized formats.");
2015-05-19 03:57:13 +00:00
}
delete pcStream;
2020-03-22 13:21:24 +00:00
pcStream = nullptr;
2015-05-19 03:57:13 +00:00
}
*pszColorMap = szColorMap;
}
// ------------------------------------------------------------------------------------------------
// Free the palette again
2020-03-22 13:21:24 +00:00
void MDLImporter::FreePalette(const unsigned char *szColorMap) {
if (szColorMap != (const unsigned char *)::g_aclrDefaultColorMap) {
2015-05-19 03:57:13 +00:00
delete[] szColorMap;
2020-03-22 13:21:24 +00:00
}
}
// ------------------------------------------------------------------------------------------------
// Check whether we can replace a texture with a single color
2020-03-22 13:21:24 +00:00
aiColor4D MDLImporter::ReplaceTextureWithColor(const aiTexture *pcTexture) {
ai_assert(nullptr != pcTexture);
2015-05-19 03:57:13 +00:00
aiColor4D clrOut;
clrOut.r = get_qnan();
if (!pcTexture->mHeight || !pcTexture->mWidth)
return clrOut;
2020-03-22 13:21:24 +00:00
const unsigned int iNumPixels = pcTexture->mHeight * pcTexture->mWidth;
const aiTexel *pcTexel = pcTexture->pcData + 1;
const aiTexel *const pcTexelEnd = &pcTexture->pcData[iNumPixels];
2015-05-19 03:57:13 +00:00
2020-03-22 13:21:24 +00:00
while (pcTexel != pcTexelEnd) {
if (*pcTexel != *(pcTexel - 1)) {
pcTexel = nullptr;
2015-05-19 03:57:13 +00:00
break;
}
++pcTexel;
}
2020-03-22 13:21:24 +00:00
if (pcTexel) {
2015-05-19 03:57:13 +00:00
clrOut.r = pcTexture->pcData->r / 255.0f;
clrOut.g = pcTexture->pcData->g / 255.0f;
clrOut.b = pcTexture->pcData->b / 255.0f;
clrOut.a = pcTexture->pcData->a / 255.0f;
}
return clrOut;
}
// ------------------------------------------------------------------------------------------------
// Read a texture from a MDL3 file
2020-03-22 13:21:24 +00:00
void MDLImporter::CreateTextureARGB8_3DGS_MDL3(const unsigned char *szData) {
const MDL::Header *pcHeader = (const MDL::Header *)mBuffer; //the endianness is already corrected in the InternReadFile_3DGS_MDL345 function
2023-12-22 12:06:10 +00:00
const size_t len = pcHeader->skinwidth * pcHeader->skinheight;
VALIDATE_FILE_SIZE(szData + len);
2015-05-19 03:57:13 +00:00
// allocate a new texture object
2020-03-22 13:21:24 +00:00
aiTexture *pcNew = new aiTexture();
2015-05-19 03:57:13 +00:00
pcNew->mWidth = pcHeader->skinwidth;
pcNew->mHeight = pcHeader->skinheight;
if(pcNew->mWidth != 0 && pcNew->mHeight > UINT_MAX/pcNew->mWidth) {
throw DeadlyImportError("Invalid MDL file. A texture is too big.");
}
2015-05-19 03:57:13 +00:00
pcNew->pcData = new aiTexel[pcNew->mWidth * pcNew->mHeight];
2020-03-22 13:21:24 +00:00
const unsigned char *szColorMap;
2015-05-19 03:57:13 +00:00
this->SearchPalette(&szColorMap);
2015-05-19 03:57:13 +00:00
// copy texture data
2020-03-22 13:21:24 +00:00
for (unsigned int i = 0; i < pcNew->mWidth * pcNew->mHeight; ++i) {
2015-05-19 03:57:13 +00:00
const unsigned char val = szData[i];
2020-03-22 13:21:24 +00:00
const unsigned char *sz = &szColorMap[val * 3];
2015-05-19 03:57:13 +00:00
pcNew->pcData[i].a = 0xFF;
pcNew->pcData[i].r = *sz++;
pcNew->pcData[i].g = *sz++;
pcNew->pcData[i].b = *sz;
}
2015-05-19 03:57:13 +00:00
FreePalette(szColorMap);
2015-05-19 03:57:13 +00:00
// store the texture
2020-03-22 13:21:24 +00:00
aiTexture **pc = this->pScene->mTextures;
this->pScene->mTextures = new aiTexture *[pScene->mNumTextures + 1];
for (unsigned int i = 0; i < pScene->mNumTextures; ++i)
2015-05-19 03:57:13 +00:00
pScene->mTextures[i] = pc[i];
2015-05-19 03:57:13 +00:00
pScene->mTextures[this->pScene->mNumTextures] = pcNew;
pScene->mNumTextures++;
delete[] pc;
}
// ------------------------------------------------------------------------------------------------
// Read a texture from a MDL4 file
2020-03-22 13:21:24 +00:00
void MDLImporter::CreateTexture_3DGS_MDL4(const unsigned char *szData,
unsigned int iType,
unsigned int *piSkip) {
ai_assert(nullptr != piSkip);
2015-05-19 03:57:13 +00:00
2020-03-22 13:21:24 +00:00
const MDL::Header *pcHeader = (const MDL::Header *)mBuffer; //the endianness is already corrected in the InternReadFile_3DGS_MDL345 function
2015-05-19 03:57:13 +00:00
2020-03-22 13:21:24 +00:00
if (iType == 1 || iType > 3) {
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_ERROR("Unsupported texture file format");
2015-05-19 03:57:13 +00:00
return;
}
const bool bNoRead = *piSkip == UINT_MAX;
// allocate a new texture object
2020-03-22 13:21:24 +00:00
aiTexture *pcNew = new aiTexture();
2015-05-19 03:57:13 +00:00
pcNew->mWidth = pcHeader->skinwidth;
pcNew->mHeight = pcHeader->skinheight;
2020-03-22 13:21:24 +00:00
if (bNoRead) pcNew->pcData = bad_texel;
ParseTextureColorData(szData, iType, piSkip, pcNew);
2015-05-19 03:57:13 +00:00
// store the texture
2020-03-22 13:21:24 +00:00
if (!bNoRead) {
if (!this->pScene->mNumTextures) {
2015-05-19 03:57:13 +00:00
pScene->mNumTextures = 1;
2020-03-22 13:21:24 +00:00
pScene->mTextures = new aiTexture *[1];
2015-05-19 03:57:13 +00:00
pScene->mTextures[0] = pcNew;
2020-03-22 13:21:24 +00:00
} else {
aiTexture **pc = pScene->mTextures;
pScene->mTextures = new aiTexture *[pScene->mNumTextures + 1];
for (unsigned int i = 0; i < this->pScene->mNumTextures; ++i)
2015-05-19 03:57:13 +00:00
pScene->mTextures[i] = pc[i];
pScene->mTextures[pScene->mNumTextures] = pcNew;
pScene->mNumTextures++;
delete[] pc;
}
2020-03-22 13:21:24 +00:00
} else {
pcNew->pcData = nullptr;
2015-05-19 03:57:13 +00:00
delete pcNew;
}
return;
}
// ------------------------------------------------------------------------------------------------
// Load color data of a texture and convert it to our output format
2020-03-22 13:21:24 +00:00
void MDLImporter::ParseTextureColorData(const unsigned char *szData,
unsigned int iType,
unsigned int *piSkip,
aiTexture *pcNew) {
2015-05-19 03:57:13 +00:00
const bool do_read = bad_texel != pcNew->pcData;
// allocate storage for the texture image
if (do_read) {
if(pcNew->mWidth != 0 && pcNew->mHeight > UINT_MAX/pcNew->mWidth) {
throw DeadlyImportError("Invalid MDL file. A texture is too big.");
}
2015-05-19 03:57:13 +00:00
pcNew->pcData = new aiTexel[pcNew->mWidth * pcNew->mHeight];
}
// R5G6B5 format (with or without MIPs)
// ****************************************************************
2020-03-22 13:21:24 +00:00
if (2 == iType || 10 == iType) {
VALIDATE_FILE_SIZE(szData + pcNew->mWidth * pcNew->mHeight * 2);
2015-05-19 03:57:13 +00:00
// copy texture data
unsigned int i;
2020-03-22 13:21:24 +00:00
if (do_read) {
for (i = 0; i < pcNew->mWidth * pcNew->mHeight; ++i) {
MDL::RGB565 val = ((MDL::RGB565 *)szData)[i];
2015-05-19 03:57:13 +00:00
AI_SWAP2(val);
pcNew->pcData[i].a = 0xFF;
pcNew->pcData[i].r = (unsigned char)val.b << 3;
pcNew->pcData[i].g = (unsigned char)val.g << 2;
pcNew->pcData[i].b = (unsigned char)val.r << 3;
}
2020-03-22 13:21:24 +00:00
} else {
i = pcNew->mWidth * pcNew->mHeight;
2015-05-19 03:57:13 +00:00
}
*piSkip = i * 2;
// apply MIP maps
2020-03-22 13:21:24 +00:00
if (10 == iType) {
2015-05-19 03:57:13 +00:00
*piSkip += ((i >> 2) + (i >> 4) + (i >> 6)) << 1;
VALIDATE_FILE_SIZE(szData + *piSkip);
}
}
// ARGB4 format (with or without MIPs)
// ****************************************************************
2020-03-22 13:21:24 +00:00
else if (3 == iType || 11 == iType) {
VALIDATE_FILE_SIZE(szData + pcNew->mWidth * pcNew->mHeight * 4);
2015-05-19 03:57:13 +00:00
// copy texture data
unsigned int i;
2020-03-22 13:21:24 +00:00
if (do_read) {
for (i = 0; i < pcNew->mWidth * pcNew->mHeight; ++i) {
MDL::ARGB4 val = ((MDL::ARGB4 *)szData)[i];
2015-05-19 03:57:13 +00:00
AI_SWAP2(val);
pcNew->pcData[i].a = (unsigned char)val.a << 4;
pcNew->pcData[i].r = (unsigned char)val.r << 4;
pcNew->pcData[i].g = (unsigned char)val.g << 4;
pcNew->pcData[i].b = (unsigned char)val.b << 4;
}
2020-03-22 13:21:24 +00:00
} else
i = pcNew->mWidth * pcNew->mHeight;
2015-05-19 03:57:13 +00:00
*piSkip = i * 2;
// apply MIP maps
2020-03-22 13:21:24 +00:00
if (11 == iType) {
2015-05-19 03:57:13 +00:00
*piSkip += ((i >> 2) + (i >> 4) + (i >> 6)) << 1;
VALIDATE_FILE_SIZE(szData + *piSkip);
}
}
// RGB8 format (with or without MIPs)
// ****************************************************************
2020-03-22 13:21:24 +00:00
else if (4 == iType || 12 == iType) {
VALIDATE_FILE_SIZE(szData + pcNew->mWidth * pcNew->mHeight * 3);
2015-05-19 03:57:13 +00:00
// copy texture data
unsigned int i;
2020-03-22 13:21:24 +00:00
if (do_read) {
for (i = 0; i < pcNew->mWidth * pcNew->mHeight; ++i) {
const unsigned char *_szData = &szData[i * 3];
2015-05-19 03:57:13 +00:00
pcNew->pcData[i].a = 0xFF;
pcNew->pcData[i].b = *_szData++;
pcNew->pcData[i].g = *_szData++;
pcNew->pcData[i].r = *_szData;
}
2020-03-22 13:21:24 +00:00
} else
i = pcNew->mWidth * pcNew->mHeight;
2015-05-19 03:57:13 +00:00
// apply MIP maps
*piSkip = i * 3;
2020-03-22 13:21:24 +00:00
if (12 == iType) {
*piSkip += ((i >> 2) + (i >> 4) + (i >> 6)) * 3;
2015-05-19 03:57:13 +00:00
VALIDATE_FILE_SIZE(szData + *piSkip);
}
}
// ARGB8 format (with ir without MIPs)
// ****************************************************************
2020-03-22 13:21:24 +00:00
else if (5 == iType || 13 == iType) {
VALIDATE_FILE_SIZE(szData + pcNew->mWidth * pcNew->mHeight * 4);
2015-05-19 03:57:13 +00:00
// copy texture data
unsigned int i;
2020-03-22 13:21:24 +00:00
if (do_read) {
for (i = 0; i < pcNew->mWidth * pcNew->mHeight; ++i) {
const unsigned char *_szData = &szData[i * 4];
2015-05-19 03:57:13 +00:00
pcNew->pcData[i].b = *_szData++;
pcNew->pcData[i].g = *_szData++;
pcNew->pcData[i].r = *_szData++;
pcNew->pcData[i].a = *_szData;
}
2020-03-22 13:21:24 +00:00
} else {
i = pcNew->mWidth * pcNew->mHeight;
2015-05-19 03:57:13 +00:00
}
// apply MIP maps
*piSkip = i << 2;
2020-03-22 13:21:24 +00:00
if (13 == iType) {
2015-05-19 03:57:13 +00:00
*piSkip += ((i >> 2) + (i >> 4) + (i >> 6)) << 2;
}
}
// palletized 8 bit texture. As for Quake 1
// ****************************************************************
2020-03-22 13:21:24 +00:00
else if (0 == iType) {
VALIDATE_FILE_SIZE(szData + pcNew->mWidth * pcNew->mHeight);
2015-05-19 03:57:13 +00:00
// copy texture data
unsigned int i;
2020-03-22 13:21:24 +00:00
if (do_read) {
2015-05-19 03:57:13 +00:00
2020-03-22 13:21:24 +00:00
const unsigned char *szColorMap;
2015-05-19 03:57:13 +00:00
SearchPalette(&szColorMap);
2020-03-22 13:21:24 +00:00
for (i = 0; i < pcNew->mWidth * pcNew->mHeight; ++i) {
2015-05-19 03:57:13 +00:00
const unsigned char val = szData[i];
2020-03-22 13:21:24 +00:00
const unsigned char *sz = &szColorMap[val * 3];
2015-05-19 03:57:13 +00:00
pcNew->pcData[i].a = 0xFF;
pcNew->pcData[i].r = *sz++;
pcNew->pcData[i].g = *sz++;
pcNew->pcData[i].b = *sz;
}
this->FreePalette(szColorMap);
2020-03-22 13:21:24 +00:00
} else
i = pcNew->mWidth * pcNew->mHeight;
2015-05-19 03:57:13 +00:00
*piSkip = i;
// FIXME: Also support for MIP maps?
}
}
// ------------------------------------------------------------------------------------------------
// Get a texture from a MDL5 file
2020-03-22 13:21:24 +00:00
void MDLImporter::CreateTexture_3DGS_MDL5(const unsigned char *szData,
unsigned int iType,
unsigned int *piSkip) {
ai_assert(nullptr != piSkip);
2015-05-19 03:57:13 +00:00
bool bNoRead = *piSkip == UINT_MAX;
// allocate a new texture object
2020-03-22 13:21:24 +00:00
aiTexture *pcNew = new aiTexture();
2015-05-19 03:57:13 +00:00
2020-03-22 13:21:24 +00:00
VALIDATE_FILE_SIZE(szData + 8);
2015-05-19 03:57:13 +00:00
// first read the size of the texture
2020-03-22 13:21:24 +00:00
pcNew->mWidth = *((uint32_t *)szData);
2015-05-19 03:57:13 +00:00
AI_SWAP4(pcNew->mWidth);
szData += sizeof(uint32_t);
2020-03-22 13:21:24 +00:00
pcNew->mHeight = *((uint32_t *)szData);
2015-05-19 03:57:13 +00:00
AI_SWAP4(pcNew->mHeight);
szData += sizeof(uint32_t);
if (bNoRead) {
pcNew->pcData = bad_texel;
}
// this should not occur - at least the docs say it shouldn't.
// however, one can easily try out what MED does if you have
// a model with a DDS texture and export it to MDL5 ...
2021-10-05 08:59:43 +00:00
// yeah, it embeds the DDS file.
2020-03-22 13:21:24 +00:00
if (6 == iType) {
2015-05-19 03:57:13 +00:00
// this is a compressed texture in DDS format
*piSkip = pcNew->mWidth;
VALIDATE_FILE_SIZE(szData + *piSkip);
2020-03-22 13:21:24 +00:00
if (!bNoRead) {
2015-05-19 03:57:13 +00:00
// place a hint and let the application know that this is a DDS file
pcNew->mHeight = 0;
pcNew->achFormatHint[0] = 'd';
pcNew->achFormatHint[1] = 'd';
pcNew->achFormatHint[2] = 's';
pcNew->achFormatHint[3] = '\0';
2020-03-22 13:21:24 +00:00
pcNew->pcData = (aiTexel *)new unsigned char[pcNew->mWidth];
::memcpy(pcNew->pcData, szData, pcNew->mWidth);
2015-05-19 03:57:13 +00:00
}
2020-03-22 13:21:24 +00:00
} else {
2015-05-19 03:57:13 +00:00
// parse the color data of the texture
2020-03-22 13:21:24 +00:00
ParseTextureColorData(szData, iType, piSkip, pcNew);
2015-05-19 03:57:13 +00:00
}
*piSkip += sizeof(uint32_t) * 2;
2020-03-22 13:21:24 +00:00
if (!bNoRead) {
2015-05-19 03:57:13 +00:00
// store the texture
2020-03-22 13:21:24 +00:00
if (!this->pScene->mNumTextures) {
2015-05-19 03:57:13 +00:00
pScene->mNumTextures = 1;
2020-03-22 13:21:24 +00:00
pScene->mTextures = new aiTexture *[1];
2015-05-19 03:57:13 +00:00
pScene->mTextures[0] = pcNew;
2020-03-22 13:21:24 +00:00
} else {
aiTexture **pc = pScene->mTextures;
pScene->mTextures = new aiTexture *[pScene->mNumTextures + 1];
for (unsigned int i = 0; i < pScene->mNumTextures; ++i)
2015-05-19 03:57:13 +00:00
this->pScene->mTextures[i] = pc[i];
pScene->mTextures[pScene->mNumTextures] = pcNew;
pScene->mNumTextures++;
delete[] pc;
}
2020-03-22 13:21:24 +00:00
} else {
pcNew->pcData = nullptr;
2015-05-19 03:57:13 +00:00
delete pcNew;
}
return;
}
// ------------------------------------------------------------------------------------------------
// Get a skin from a MDL7 file - more complex than all other subformats
void MDLImporter::ParseSkinLump_3DGS_MDL7(
2020-03-22 13:21:24 +00:00
const unsigned char *szCurrent,
const unsigned char **szCurrentOut,
aiMaterial *pcMatOut,
unsigned int iType,
unsigned int iWidth,
unsigned int iHeight) {
2022-07-05 18:09:16 +00:00
std::unique_ptr<aiTexture> pcNew;
if (szCurrent == nullptr) {
return;
}
2015-05-19 03:57:13 +00:00
// get the type of the skin
unsigned int iMasked = (unsigned int)(iType & 0xF);
2020-03-22 13:21:24 +00:00
if (0x1 == iMasked) {
2015-05-19 03:57:13 +00:00
// ***** REFERENCE TO ANOTHER SKIN INDEX *****
int referrer = (int)iWidth;
2020-03-22 13:21:24 +00:00
pcMatOut->AddProperty<int>(&referrer, 1, AI_MDL7_REFERRER_MATERIAL);
} else if (0x6 == iMasked) {
2015-05-19 03:57:13 +00:00
// ***** EMBEDDED DDS FILE *****
2020-03-22 13:21:24 +00:00
if (1 != iHeight) {
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_WARN("Found a reference to an embedded DDS texture, "
2020-03-22 13:21:24 +00:00
"but texture height is not equal to 1, which is not supported by MED");
2015-05-19 03:57:13 +00:00
}
if (iWidth == 0) {
ASSIMP_LOG_ERROR("Found a reference to an embedded DDS texture, but texture width is zero, aborting import.");
return;
}
2023-01-16 08:12:35 +00:00
pcNew.reset(new aiTexture);
2015-05-19 03:57:13 +00:00
pcNew->mHeight = 0;
pcNew->mWidth = iWidth;
// place a proper format hint
pcNew->achFormatHint[0] = 'd';
pcNew->achFormatHint[1] = 'd';
pcNew->achFormatHint[2] = 's';
pcNew->achFormatHint[3] = '\0';
2023-06-04 08:07:19 +00:00
SizeCheck(szCurrent + pcNew->mWidth);
2020-03-22 13:21:24 +00:00
pcNew->pcData = (aiTexel *)new unsigned char[pcNew->mWidth];
memcpy(pcNew->pcData, szCurrent, pcNew->mWidth);
2015-05-19 03:57:13 +00:00
szCurrent += iWidth;
2020-03-22 13:21:24 +00:00
} else if (0x7 == iMasked) {
2015-05-19 03:57:13 +00:00
// ***** REFERENCE TO EXTERNAL FILE *****
2020-03-22 13:21:24 +00:00
if (1 != iHeight) {
2018-04-19 15:21:21 +00:00
ASSIMP_LOG_WARN("Found a reference to an external texture, "
2020-03-22 13:21:24 +00:00
"but texture height is not equal to 1, which is not supported by MED");
2015-05-19 03:57:13 +00:00
}
aiString szFile;
2020-03-22 13:21:24 +00:00
const size_t iLen = strlen((const char *)szCurrent);
size_t iLen2 = iLen > (MAXLEN - 1) ? (MAXLEN - 1) : iLen;
2020-03-22 13:21:24 +00:00
memcpy(szFile.data, (const char *)szCurrent, iLen2);
szFile.data[iLen2] = '\0';
szFile.length = static_cast<ai_uint32>(iLen2);
2015-05-19 03:57:13 +00:00
szCurrent += iLen2 + 1;
2015-05-19 03:57:13 +00:00
// place this as diffuse texture
2020-03-22 13:21:24 +00:00
pcMatOut->AddProperty(&szFile, AI_MATKEY_TEXTURE_DIFFUSE(0));
} else if (iMasked || !iType || (iType && iWidth && iHeight)) {
pcNew.reset(new aiTexture());
2020-03-22 13:21:24 +00:00
if (!iHeight || !iWidth) {
2018-04-19 15:21:21 +00:00
ASSIMP_LOG_WARN("Found embedded texture, but its width "
2020-03-22 13:21:24 +00:00
"an height are both 0. Is this a joke?");
2015-05-19 03:57:13 +00:00
// generate an empty chess pattern
pcNew->mWidth = pcNew->mHeight = 8;
pcNew->pcData = new aiTexel[64];
2020-03-22 13:21:24 +00:00
for (unsigned int x = 0; x < 8; ++x) {
for (unsigned int y = 0; y < 8; ++y) {
2015-05-19 03:57:13 +00:00
const bool bSet = ((0 == x % 2 && 0 != y % 2) ||
2020-03-22 13:21:24 +00:00
(0 != x % 2 && 0 == y % 2));
2015-05-19 03:57:13 +00:00
2020-03-22 13:21:24 +00:00
aiTexel *pc = &pcNew->pcData[y * 8 + x];
pc->r = pc->b = pc->g = (bSet ? 0xFF : 0);
2015-05-19 03:57:13 +00:00
pc->a = 0xFF;
}
}
2020-03-22 13:21:24 +00:00
} else {
2015-05-19 03:57:13 +00:00
// it is a standard color texture. Fill in width and height
// and call the same function we used for loading MDL5 files
pcNew->mWidth = iWidth;
pcNew->mHeight = iHeight;
unsigned int iSkip = 0;
2020-03-22 13:21:24 +00:00
ParseTextureColorData(szCurrent, iMasked, &iSkip, pcNew.get());
2015-05-19 03:57:13 +00:00
// skip length of texture data
szCurrent += iSkip;
}
}
// sometimes there are MDL7 files which have a monochrome
2021-10-05 08:59:43 +00:00
// texture instead of material colors ... possible they have
2015-05-19 03:57:13 +00:00
// been converted to MDL7 from other formats, such as MDL5
aiColor4D clrTexture;
2020-03-22 13:21:24 +00:00
if (pcNew)
clrTexture = ReplaceTextureWithColor(pcNew.get());
else
clrTexture.r = get_qnan();
2015-05-19 03:57:13 +00:00
// check whether a material definition is contained in the skin
2020-03-22 13:21:24 +00:00
if (iType & AI_MDL7_SKINTYPE_MATERIAL) {
BE_NCONST MDL::Material_MDL7 *pcMatIn = (BE_NCONST MDL::Material_MDL7 *)szCurrent;
szCurrent = (unsigned char *)(pcMatIn + 1);
2015-05-19 03:57:13 +00:00
VALIDATE_FILE_SIZE(szCurrent);
aiColor3D clrTemp;
2020-03-22 13:21:24 +00:00
#define COLOR_MULTIPLY_RGB() \
if (is_not_qnan(clrTexture.r)) { \
clrTemp.r *= clrTexture.r; \
clrTemp.g *= clrTexture.g; \
clrTemp.b *= clrTexture.b; \
}
2015-05-19 03:57:13 +00:00
// read diffuse color
clrTemp.r = pcMatIn->Diffuse.r;
AI_SWAP4(clrTemp.r);
clrTemp.g = pcMatIn->Diffuse.g;
AI_SWAP4(clrTemp.g);
clrTemp.b = pcMatIn->Diffuse.b;
AI_SWAP4(clrTemp.b);
COLOR_MULTIPLY_RGB();
2020-03-22 13:21:24 +00:00
pcMatOut->AddProperty<aiColor3D>(&clrTemp, 1, AI_MATKEY_COLOR_DIFFUSE);
2015-05-19 03:57:13 +00:00
// read specular color
clrTemp.r = pcMatIn->Specular.r;
AI_SWAP4(clrTemp.r);
clrTemp.g = pcMatIn->Specular.g;
AI_SWAP4(clrTemp.g);
clrTemp.b = pcMatIn->Specular.b;
AI_SWAP4(clrTemp.b);
COLOR_MULTIPLY_RGB();
2020-03-22 13:21:24 +00:00
pcMatOut->AddProperty<aiColor3D>(&clrTemp, 1, AI_MATKEY_COLOR_SPECULAR);
2015-05-19 03:57:13 +00:00
// read ambient color
clrTemp.r = pcMatIn->Ambient.r;
AI_SWAP4(clrTemp.r);
clrTemp.g = pcMatIn->Ambient.g;
AI_SWAP4(clrTemp.g);
clrTemp.b = pcMatIn->Ambient.b;
AI_SWAP4(clrTemp.b);
COLOR_MULTIPLY_RGB();
2020-03-22 13:21:24 +00:00
pcMatOut->AddProperty<aiColor3D>(&clrTemp, 1, AI_MATKEY_COLOR_AMBIENT);
2015-05-19 03:57:13 +00:00
// read emissive color
clrTemp.r = pcMatIn->Emissive.r;
AI_SWAP4(clrTemp.r);
clrTemp.g = pcMatIn->Emissive.g;
AI_SWAP4(clrTemp.g);
clrTemp.b = pcMatIn->Emissive.b;
AI_SWAP4(clrTemp.b);
2020-03-22 13:21:24 +00:00
pcMatOut->AddProperty<aiColor3D>(&clrTemp, 1, AI_MATKEY_COLOR_EMISSIVE);
#undef COLOR_MULITPLY_RGB
2015-05-19 03:57:13 +00:00
// FIX: Take the opacity from the ambient color.
// The doc say something else, but it is fact that MED exports the
// opacity like this .... oh well.
clrTemp.r = pcMatIn->Ambient.a;
AI_SWAP4(clrTemp.r);
if (is_not_qnan(clrTexture.r)) {
clrTemp.r *= clrTexture.a;
}
2020-03-22 13:21:24 +00:00
pcMatOut->AddProperty<ai_real>(&clrTemp.r, 1, AI_MATKEY_OPACITY);
2015-05-19 03:57:13 +00:00
// read phong power
int iShadingMode = (int)aiShadingMode_Gouraud;
AI_SWAP4(pcMatIn->Power);
2020-03-22 13:21:24 +00:00
if (0.0f != pcMatIn->Power) {
2015-05-19 03:57:13 +00:00
iShadingMode = (int)aiShadingMode_Phong;
// pcMatIn is packed, we can't form pointers to its members
float power = pcMatIn->Power;
2020-03-22 13:21:24 +00:00
pcMatOut->AddProperty<float>(&power, 1, AI_MATKEY_SHININESS);
2015-05-19 03:57:13 +00:00
}
2020-03-22 13:21:24 +00:00
pcMatOut->AddProperty<int>(&iShadingMode, 1, AI_MATKEY_SHADING_MODEL);
} else if (is_not_qnan(clrTexture.r)) {
pcMatOut->AddProperty<aiColor4D>(&clrTexture, 1, AI_MATKEY_COLOR_DIFFUSE);
pcMatOut->AddProperty<aiColor4D>(&clrTexture, 1, AI_MATKEY_COLOR_SPECULAR);
2015-05-19 03:57:13 +00:00
}
// if the texture could be replaced by a single material color
// we don't need the texture anymore
2020-03-22 13:21:24 +00:00
if (is_not_qnan(clrTexture.r)) {
pcNew.reset();
2015-05-19 03:57:13 +00:00
}
// If an ASCII effect description (HLSL?) is contained in the file,
// we can simply ignore it ...
2020-03-22 13:21:24 +00:00
if (iType & AI_MDL7_SKINTYPE_MATERIAL_ASCDEF) {
2015-05-19 03:57:13 +00:00
VALIDATE_FILE_SIZE(szCurrent);
2020-03-22 13:21:24 +00:00
int32_t iMe = *((int32_t *)szCurrent);
2015-05-19 03:57:13 +00:00
AI_SWAP4(iMe);
szCurrent += sizeof(char) * iMe + sizeof(int32_t);
VALIDATE_FILE_SIZE(szCurrent);
}
// If an embedded texture has been loaded setup the corresponding
// data structures in the aiScene instance
2020-03-22 13:21:24 +00:00
if (pcNew && pScene->mNumTextures <= 999) {
2015-05-19 03:57:13 +00:00
// place this as diffuse texture
2020-03-01 12:15:45 +00:00
char current[5];
ai_snprintf(current, 5, "*%i", this->pScene->mNumTextures);
2015-05-19 03:57:13 +00:00
aiString szFile;
2020-03-01 12:15:45 +00:00
const size_t iLen = strlen((const char *)current);
::memcpy(szFile.data, (const char *)current, iLen + 1);
szFile.length = (ai_uint32)iLen;
2015-05-19 03:57:13 +00:00
2020-03-22 13:21:24 +00:00
pcMatOut->AddProperty(&szFile, AI_MATKEY_TEXTURE_DIFFUSE(0));
2015-05-19 03:57:13 +00:00
// store the texture
2020-03-22 13:21:24 +00:00
if (!pScene->mNumTextures) {
2015-05-19 03:57:13 +00:00
pScene->mNumTextures = 1;
2020-03-22 13:21:24 +00:00
pScene->mTextures = new aiTexture *[1];
pScene->mTextures[0] = pcNew.release();
2020-03-22 13:21:24 +00:00
} else {
aiTexture **pc = pScene->mTextures;
pScene->mTextures = new aiTexture *[pScene->mNumTextures + 1];
for (unsigned int i = 0; i < pScene->mNumTextures; ++i) {
2015-05-19 03:57:13 +00:00
pScene->mTextures[i] = pc[i];
}
pScene->mTextures[pScene->mNumTextures] = pcNew.release();
2015-05-19 03:57:13 +00:00
pScene->mNumTextures++;
delete[] pc;
}
}
VALIDATE_FILE_SIZE(szCurrent);
*szCurrentOut = szCurrent;
}
// ------------------------------------------------------------------------------------------------
// Skip a skin lump
void MDLImporter::SkipSkinLump_3DGS_MDL7(
2020-03-22 13:21:24 +00:00
const unsigned char *szCurrent,
const unsigned char **szCurrentOut,
unsigned int iType,
unsigned int iWidth,
unsigned int iHeight) {
2015-05-19 03:57:13 +00:00
// get the type of the skin
const unsigned int iMasked = (unsigned int)(iType & 0xF);
2020-03-22 13:21:24 +00:00
if (0x6 == iMasked) {
2015-05-19 03:57:13 +00:00
szCurrent += iWidth;
}
2020-03-22 13:21:24 +00:00
if (0x7 == iMasked) {
const size_t iLen = std::strlen((const char *)szCurrent);
szCurrent += iLen + 1;
} else if (iMasked || !iType) {
if (iMasked || !iType || (iType && iWidth && iHeight)) {
2015-05-19 03:57:13 +00:00
// ParseTextureColorData(..., aiTexture::pcData == bad_texel) will simply
// return the size of the color data in bytes in iSkip
unsigned int iSkip = 0;
aiTexture tex;
tex.pcData = bad_texel;
tex.mHeight = iHeight;
tex.mWidth = iWidth;
try {
ParseTextureColorData(szCurrent, iMasked, &iSkip, &tex);
} catch (...) {
// FIX: Important, otherwise the destructor will crash
tex.pcData = nullptr;
throw;
}
2015-05-19 03:57:13 +00:00
// FIX: Important, otherwise the destructor will crash
2020-03-22 13:21:24 +00:00
tex.pcData = nullptr;
2015-05-19 03:57:13 +00:00
// skip length of texture data
szCurrent += iSkip;
}
}
// check whether a material definition is contained in the skin
2020-03-22 13:21:24 +00:00
if (iType & AI_MDL7_SKINTYPE_MATERIAL) {
BE_NCONST MDL::Material_MDL7 *pcMatIn = (BE_NCONST MDL::Material_MDL7 *)szCurrent;
szCurrent = (unsigned char *)(pcMatIn + 1);
2015-05-19 03:57:13 +00:00
}
// if an ASCII effect description (HLSL?) is contained in the file,
// we can simply ignore it ...
2020-03-22 13:21:24 +00:00
if (iType & AI_MDL7_SKINTYPE_MATERIAL_ASCDEF) {
int32_t iMe = *((int32_t *)szCurrent);
2015-05-19 03:57:13 +00:00
AI_SWAP4(iMe);
szCurrent += sizeof(char) * iMe + sizeof(int32_t);
}
*szCurrentOut = szCurrent;
}
// ------------------------------------------------------------------------------------------------
void MDLImporter::ParseSkinLump_3DGS_MDL7(
2020-03-22 13:21:24 +00:00
const unsigned char *szCurrent,
const unsigned char **szCurrentOut,
std::vector<aiMaterial *> &pcMats) {
ai_assert(nullptr != szCurrent);
ai_assert(nullptr != szCurrentOut);
2015-05-19 03:57:13 +00:00
*szCurrentOut = szCurrent;
2020-03-22 13:21:24 +00:00
BE_NCONST MDL::Skin_MDL7 *pcSkin = (BE_NCONST MDL::Skin_MDL7 *)szCurrent;
2015-05-19 03:57:13 +00:00
AI_SWAP4(pcSkin->width);
AI_SWAP4(pcSkin->height);
szCurrent += 12;
// allocate an output material
2020-03-22 13:21:24 +00:00
aiMaterial *pcMatOut = new aiMaterial();
2015-05-19 03:57:13 +00:00
pcMats.push_back(pcMatOut);
// skip length of file name
szCurrent += AI_MDL7_MAX_TEXNAMESIZE;
2020-03-22 13:21:24 +00:00
ParseSkinLump_3DGS_MDL7(szCurrent, szCurrentOut, pcMatOut,
pcSkin->typ, pcSkin->width, pcSkin->height);
2015-05-19 03:57:13 +00:00
// place the name of the skin in the material
2020-03-22 13:21:24 +00:00
if (pcSkin->texture_name[0]) {
2015-05-19 03:57:13 +00:00
// the 0 termination could be there or not - we can't know
aiString szFile;
2020-03-22 13:21:24 +00:00
::memcpy(szFile.data, pcSkin->texture_name, sizeof(pcSkin->texture_name));
2015-05-19 03:57:13 +00:00
szFile.data[sizeof(pcSkin->texture_name)] = '\0';
szFile.length = (ai_uint32)::strlen(szFile.data);
2015-05-19 03:57:13 +00:00
2020-03-22 13:21:24 +00:00
pcMatOut->AddProperty(&szFile, AI_MATKEY_NAME);
2015-05-19 03:57:13 +00:00
}
}
#endif // !! ASSIMP_BUILD_NO_MDL_IMPORTER