assimp/code/AssetLib/LWO/LWOMaterial.cpp

845 lines
32 KiB
C++
Raw Normal View History

/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
2021-02-28 11:17:54 +00:00
Copyright (c) 2006-2021, assimp team
2018-01-28 18:42:05 +00:00
2017-05-09 17:57:36 +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 oart of the LWO importer class */
#ifndef ASSIMP_BUILD_NO_LWO_IMPORTER
// internal headers
#include "LWOLoader.h"
#include <assimp/ByteSwapper.h>
using namespace Assimp;
// ------------------------------------------------------------------------------------------------
template <class T>
2020-03-01 12:15:45 +00:00
T lerp(const T &one, const T &two, float val) {
return one + (two - one) * val;
}
// ------------------------------------------------------------------------------------------------
// Convert a lightwave mapping mode to our's
2020-03-01 12:15:45 +00:00
inline aiTextureMapMode GetMapMode(LWO::Texture::Wrap in) {
switch (in) {
2015-05-19 03:57:13 +00:00
case LWO::Texture::REPEAT:
return aiTextureMapMode_Wrap;
case LWO::Texture::MIRROR:
return aiTextureMapMode_Mirror;
case LWO::Texture::RESET:
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_WARN("LWO2: Unsupported texture map mode: RESET");
2015-05-19 03:57:13 +00:00
// fall though here
case LWO::Texture::EDGE:
return aiTextureMapMode_Clamp;
}
return (aiTextureMapMode)0;
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
bool LWOImporter::HandleTextures(aiMaterial *pcMat, const TextureList &in, aiTextureType type) {
ai_assert(nullptr != pcMat);
2015-05-19 03:57:13 +00:00
unsigned int cur = 0, temp = 0;
aiString s;
bool ret = false;
2020-03-01 12:15:45 +00:00
for (const auto &texture : in) {
2016-05-21 20:28:33 +00:00
if (!texture.enabled || !texture.bCanUse)
2015-05-19 03:57:13 +00:00
continue;
ret = true;
// Convert lightwave's mapping modes to ours. We let them
// as they are, the GenUVcoords step will compute UV
// channels if they're not there.
2020-03-01 12:15:45 +00:00
aiTextureMapping mapping = aiTextureMapping_OTHER;
switch (texture.mapMode) {
2015-05-19 03:57:13 +00:00
case LWO::Texture::Planar:
mapping = aiTextureMapping_PLANE;
break;
case LWO::Texture::Cylindrical:
mapping = aiTextureMapping_CYLINDER;
break;
case LWO::Texture::Spherical:
mapping = aiTextureMapping_SPHERE;
break;
case LWO::Texture::Cubic:
mapping = aiTextureMapping_BOX;
break;
case LWO::Texture::FrontProjection:
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWO2: Unsupported texture mapping: FrontProjection");
2015-05-19 03:57:13 +00:00
mapping = aiTextureMapping_OTHER;
break;
2020-03-01 12:15:45 +00:00
case LWO::Texture::UV: {
if (UINT_MAX == texture.mRealUVIndex) {
// We have no UV index for this texture, so we can't display it
continue;
}
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
// add the UV source index
temp = texture.mRealUVIndex;
pcMat->AddProperty<int>((int *)&temp, 1, AI_MATKEY_UVWSRC(type, cur));
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
mapping = aiTextureMapping_UV;
} break;
2015-05-19 03:57:13 +00:00
default:
ai_assert(false);
};
if (mapping != aiTextureMapping_UV) {
// Setup the main axis
aiVector3D v;
2016-05-21 20:28:33 +00:00
switch (texture.majorAxis) {
2015-05-19 03:57:13 +00:00
case Texture::AXIS_X:
2020-03-01 12:15:45 +00:00
v = aiVector3D(1.0, 0.0, 0.0);
2015-05-19 03:57:13 +00:00
break;
case Texture::AXIS_Y:
2020-03-01 12:15:45 +00:00
v = aiVector3D(0.0, 1.0, 0.0);
2015-05-19 03:57:13 +00:00
break;
default: // case Texture::AXIS_Z:
2020-03-01 12:15:45 +00:00
v = aiVector3D(0.0, 0.0, 1.0);
2015-05-19 03:57:13 +00:00
break;
}
2020-03-01 12:15:45 +00:00
pcMat->AddProperty(&v, 1, AI_MATKEY_TEXMAP_AXIS(type, cur));
2015-05-19 03:57:13 +00:00
// Setup UV scalings for cylindric and spherical projections
if (mapping == aiTextureMapping_CYLINDER || mapping == aiTextureMapping_SPHERE) {
aiUVTransform trafo;
2016-05-21 20:28:33 +00:00
trafo.mScaling.x = texture.wrapAmountW;
trafo.mScaling.y = texture.wrapAmountH;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
static_assert(sizeof(aiUVTransform) / sizeof(ai_real) == 5, "sizeof(aiUVTransform)/sizeof(ai_real) == 5");
pcMat->AddProperty(&trafo, 1, AI_MATKEY_UVTRANSFORM(type, cur));
2015-05-19 03:57:13 +00:00
}
2020-05-15 15:59:42 +00:00
ASSIMP_LOG_VERBOSE_DEBUG("LWO2: Setting up non-UV mapping");
2015-05-19 03:57:13 +00:00
}
// The older LWOB format does not use indirect references to clips.
// The file name of a texture is directly specified in the tex chunk.
2020-03-01 12:15:45 +00:00
if (mIsLWO2) {
2015-05-19 03:57:13 +00:00
// find the corresponding clip (take the last one if multiple
// share the same index)
ClipList::iterator end = mClips.end(), candidate = end;
2016-05-21 20:28:33 +00:00
temp = texture.mClipIdx;
2015-05-19 03:57:13 +00:00
for (ClipList::iterator clip = mClips.begin(); clip != end; ++clip) {
if ((*clip).idx == temp) {
candidate = clip;
}
}
2020-03-01 12:15:45 +00:00
if (candidate == end) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWO2: Clip index is out of bounds");
2015-05-19 03:57:13 +00:00
temp = 0;
// fixme: apparently some LWO files shipping with Doom3 don't
// have clips at all ... check whether that's true or whether
// it's a bug in the loader.
s.Set("$texture.png");
//continue;
2020-03-01 12:15:45 +00:00
} else {
2015-05-19 03:57:13 +00:00
if (Clip::UNSUPPORTED == (*candidate).type) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWO2: Clip type is not supported");
2015-05-19 03:57:13 +00:00
continue;
}
AdjustTexturePath((*candidate).path);
s.Set((*candidate).path);
// Additional image settings
int flags = 0;
if ((*candidate).negate) {
flags |= aiTextureFlags_Invert;
}
2020-03-01 12:15:45 +00:00
pcMat->AddProperty(&flags, 1, AI_MATKEY_TEXFLAGS(type, cur));
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
} else {
2016-05-21 20:28:33 +00:00
std::string ss = texture.mFileName;
2015-05-19 03:57:13 +00:00
if (!ss.length()) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_WARN("LWOB: Empty file name");
2015-05-19 03:57:13 +00:00
continue;
}
AdjustTexturePath(ss);
s.Set(ss);
}
2020-03-01 12:15:45 +00:00
pcMat->AddProperty(&s, AI_MATKEY_TEXTURE(type, cur));
2015-05-19 03:57:13 +00:00
// add the blend factor
2020-03-01 12:15:45 +00:00
pcMat->AddProperty<float>(&texture.mStrength, 1, AI_MATKEY_TEXBLEND(type, cur));
2015-05-19 03:57:13 +00:00
// add the blend operation
2020-03-01 12:15:45 +00:00
switch (texture.blendType) {
2015-05-19 03:57:13 +00:00
case LWO::Texture::Normal:
case LWO::Texture::Multiply:
temp = (unsigned int)aiTextureOp_Multiply;
break;
case LWO::Texture::Subtractive:
case LWO::Texture::Difference:
temp = (unsigned int)aiTextureOp_Subtract;
break;
case LWO::Texture::Divide:
temp = (unsigned int)aiTextureOp_Divide;
break;
case LWO::Texture::Additive:
temp = (unsigned int)aiTextureOp_Add;
break;
default:
temp = (unsigned int)aiTextureOp_Multiply;
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_WARN("LWO2: Unsupported texture blend mode: alpha or displacement");
2015-05-19 03:57:13 +00:00
}
// Setup texture operation
2020-03-01 12:15:45 +00:00
pcMat->AddProperty<int>((int *)&temp, 1, AI_MATKEY_TEXOP(type, cur));
2015-05-19 03:57:13 +00:00
// setup the mapping mode
int mapping_ = static_cast<int>(mapping);
pcMat->AddProperty<int>(&mapping_, 1, AI_MATKEY_MAPPING(type, cur));
2015-05-19 03:57:13 +00:00
// add the u-wrapping
2016-05-21 20:28:33 +00:00
temp = (unsigned int)GetMapMode(texture.wrapModeWidth);
2020-03-01 12:15:45 +00:00
pcMat->AddProperty<int>((int *)&temp, 1, AI_MATKEY_MAPPINGMODE_U(type, cur));
2015-05-19 03:57:13 +00:00
// add the v-wrapping
2016-05-21 20:28:33 +00:00
temp = (unsigned int)GetMapMode(texture.wrapModeHeight);
2020-03-01 12:15:45 +00:00
pcMat->AddProperty<int>((int *)&temp, 1, AI_MATKEY_MAPPINGMODE_V(type, cur));
2015-05-19 03:57:13 +00:00
++cur;
}
return ret;
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::ConvertMaterial(const LWO::Surface &surf, aiMaterial *pcMat) {
2015-05-19 03:57:13 +00:00
// copy the name of the surface
aiString st;
st.Set(surf.mName);
2020-03-01 12:15:45 +00:00
pcMat->AddProperty(&st, AI_MATKEY_NAME);
2015-05-19 03:57:13 +00:00
const int i = surf.bDoubleSided ? 1 : 0;
2020-03-01 12:15:45 +00:00
pcMat->AddProperty(&i, 1, AI_MATKEY_TWOSIDED);
2015-05-19 03:57:13 +00:00
// add the refraction index and the bump intensity
2020-03-01 12:15:45 +00:00
pcMat->AddProperty(&surf.mIOR, 1, AI_MATKEY_REFRACTI);
pcMat->AddProperty(&surf.mBumpIntensity, 1, AI_MATKEY_BUMPSCALING);
2015-05-19 03:57:13 +00:00
aiShadingMode m;
2020-03-01 12:15:45 +00:00
if (surf.mSpecularValue && surf.mGlossiness) {
2015-05-19 03:57:13 +00:00
float fGloss;
2020-03-01 12:15:45 +00:00
if (mIsLWO2) {
fGloss = std::pow(surf.mGlossiness * ai_real(10.0) + ai_real(2.0), ai_real(2.0));
} else {
if (16.0 >= surf.mGlossiness)
fGloss = 6.0;
else if (64.0 >= surf.mGlossiness)
fGloss = 20.0;
else if (256.0 >= surf.mGlossiness)
fGloss = 50.0;
2020-03-01 12:15:45 +00:00
else
fGloss = 80.0;
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
pcMat->AddProperty(&surf.mSpecularValue, 1, AI_MATKEY_SHININESS_STRENGTH);
pcMat->AddProperty(&fGloss, 1, AI_MATKEY_SHININESS);
2015-05-19 03:57:13 +00:00
m = aiShadingMode_Phong;
2020-03-01 12:15:45 +00:00
} else
m = aiShadingMode_Gouraud;
2015-05-19 03:57:13 +00:00
// specular color
2020-03-01 12:15:45 +00:00
aiColor3D clr = lerp(aiColor3D(1.0, 1.0, 1.0), surf.mColor, surf.mColorHighlights);
pcMat->AddProperty(&clr, 1, AI_MATKEY_COLOR_SPECULAR);
pcMat->AddProperty(&surf.mSpecularValue, 1, AI_MATKEY_SHININESS_STRENGTH);
2015-05-19 03:57:13 +00:00
// emissive color
// luminosity is not really the same but it affects the surface in a similar way. Some scaling looks good.
2020-03-01 12:15:45 +00:00
clr.g = clr.b = clr.r = surf.mLuminosity * ai_real(0.8);
pcMat->AddProperty<aiColor3D>(&clr, 1, AI_MATKEY_COLOR_EMISSIVE);
2015-05-19 03:57:13 +00:00
// opacity ... either additive or default-blended, please
2020-03-01 12:15:45 +00:00
if (0.0 != surf.mAdditiveTransparency) {
2015-05-19 03:57:13 +00:00
const int add = aiBlendMode_Additive;
2020-03-01 12:15:45 +00:00
pcMat->AddProperty(&surf.mAdditiveTransparency, 1, AI_MATKEY_OPACITY);
pcMat->AddProperty(&add, 1, AI_MATKEY_BLEND_FUNC);
} else if (10e10f != surf.mTransparency) {
2015-05-19 03:57:13 +00:00
const int def = aiBlendMode_Default;
2020-03-01 12:15:45 +00:00
const float f = 1.0f - surf.mTransparency;
pcMat->AddProperty(&f, 1, AI_MATKEY_OPACITY);
pcMat->AddProperty(&def, 1, AI_MATKEY_BLEND_FUNC);
2015-05-19 03:57:13 +00:00
}
// ADD TEXTURES to the material
// TODO: find out how we can handle COLOR textures correctly...
2020-03-01 12:15:45 +00:00
bool b = HandleTextures(pcMat, surf.mColorTextures, aiTextureType_DIFFUSE);
b = (b || HandleTextures(pcMat, surf.mDiffuseTextures, aiTextureType_DIFFUSE));
HandleTextures(pcMat, surf.mSpecularTextures, aiTextureType_SPECULAR);
HandleTextures(pcMat, surf.mGlossinessTextures, aiTextureType_SHININESS);
HandleTextures(pcMat, surf.mBumpTextures, aiTextureType_HEIGHT);
HandleTextures(pcMat, surf.mOpacityTextures, aiTextureType_OPACITY);
HandleTextures(pcMat, surf.mReflectionTextures, aiTextureType_REFLECTION);
2015-05-19 03:57:13 +00:00
// Now we need to know which shader to use .. iterate through the shader list of
// the surface and search for a name which we know ...
2020-03-01 12:15:45 +00:00
for (const auto &shader : surf.mShaders) {
if (shader.functionName == "LW_SuperCelShader" || shader.functionName == "AH_CelShader") {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_INFO("LWO2: Mapping LW_SuperCelShader/AH_CelShader to aiShadingMode_Toon");
2015-05-19 03:57:13 +00:00
m = aiShadingMode_Toon;
break;
2020-03-01 12:15:45 +00:00
} else if (shader.functionName == "LW_RealFresnel" || shader.functionName == "LW_FastFresnel") {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_INFO("LWO2: Mapping LW_RealFresnel/LW_FastFresnel to aiShadingMode_Fresnel");
2015-05-19 03:57:13 +00:00
m = aiShadingMode_Fresnel;
break;
2020-03-01 12:15:45 +00:00
} else {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_WARN_F("LWO2: Unknown surface shader: ", shader.functionName);
2015-05-19 03:57:13 +00:00
}
}
if (surf.mMaximumSmoothAngle <= 0.0)
2015-05-19 03:57:13 +00:00
m = aiShadingMode_Flat;
int m_ = static_cast<int>(m);
pcMat->AddProperty(&m_, 1, AI_MATKEY_SHADING_MODEL);
2015-05-19 03:57:13 +00:00
// (the diffuse value is just a scaling factor)
// If a diffuse texture is set, we set this value to 1.0
2020-03-01 12:15:45 +00:00
clr = (b && false ? aiColor3D(1.0, 1.0, 1.0) : surf.mColor);
2015-05-19 03:57:13 +00:00
clr.r *= surf.mDiffuseValue;
clr.g *= surf.mDiffuseValue;
clr.b *= surf.mDiffuseValue;
2020-03-01 12:15:45 +00:00
pcMat->AddProperty<aiColor3D>(&clr, 1, AI_MATKEY_COLOR_DIFFUSE);
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
char LWOImporter::FindUVChannels(LWO::TextureList &list,
LWO::Layer & /*layer*/, LWO::UVChannel &uv, unsigned int next) {
2015-05-19 03:57:13 +00:00
char ret = 0;
2020-03-01 12:15:45 +00:00
for (auto &texture : list) {
2015-05-19 03:57:13 +00:00
// Ignore textures with non-UV mappings for the moment.
2020-03-01 12:15:45 +00:00
if (!texture.enabled || !texture.bCanUse || texture.mapMode != LWO::Texture::UV) {
2015-05-19 03:57:13 +00:00
continue;
}
2016-05-21 20:28:33 +00:00
if (texture.mUVChannelIndex == uv.name) {
2015-05-19 03:57:13 +00:00
ret = 1;
// got it.
2020-03-01 12:15:45 +00:00
if (texture.mRealUVIndex == UINT_MAX || texture.mRealUVIndex == next) {
2016-05-21 20:28:33 +00:00
texture.mRealUVIndex = next;
2020-03-01 12:15:45 +00:00
} else {
2015-05-19 03:57:13 +00:00
// channel mismatch. need to duplicate the material.
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_WARN("LWO: Channel mismatch, would need to duplicate surface [design bug]");
2015-05-19 03:57:13 +00:00
// TODO
}
}
}
return ret;
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::FindUVChannels(LWO::Surface &surf,
LWO::SortedRep &sorted, LWO::Layer &layer,
unsigned int out[AI_MAX_NUMBER_OF_TEXTURECOORDS]) {
2015-05-19 03:57:13 +00:00
unsigned int next = 0, extra = 0, num_extra = 0;
// Check whether we have an UV entry != 0 for one of the faces in 'sorted'
2020-03-01 12:15:45 +00:00
for (unsigned int i = 0; i < layer.mUVChannels.size(); ++i) {
LWO::UVChannel &uv = layer.mUVChannels[i];
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
for (LWO::SortedRep::const_iterator it = sorted.begin(); it != sorted.end(); ++it) {
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
LWO::Face &face = layer.mFaces[*it];
2015-05-19 03:57:13 +00:00
for (unsigned int n = 0; n < face.mNumIndices; ++n) {
unsigned int idx = face.mIndices[n];
2020-03-01 12:15:45 +00:00
if (uv.abAssigned[idx] && ((aiVector2D *)&uv.rawData[0])[idx] != aiVector2D()) {
2015-05-19 03:57:13 +00:00
if (extra >= AI_MAX_NUMBER_OF_TEXTURECOORDS) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWO: Maximum number of UV channels for "
2020-03-01 12:15:45 +00:00
"this mesh reached. Skipping channel \'" +
uv.name + "\'");
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
} else {
2015-05-19 03:57:13 +00:00
// Search through all textures assigned to 'surf' and look for this UV channel
char had = 0;
2020-03-01 12:15:45 +00:00
had |= FindUVChannels(surf.mColorTextures, layer, uv, next);
had |= FindUVChannels(surf.mDiffuseTextures, layer, uv, next);
had |= FindUVChannels(surf.mSpecularTextures, layer, uv, next);
had |= FindUVChannels(surf.mGlossinessTextures, layer, uv, next);
had |= FindUVChannels(surf.mOpacityTextures, layer, uv, next);
had |= FindUVChannels(surf.mBumpTextures, layer, uv, next);
had |= FindUVChannels(surf.mReflectionTextures, layer, uv, next);
2015-05-19 03:57:13 +00:00
// We have a texture referencing this UV channel so we have to take special care
// and are willing to drop unreferenced channels in favour of it.
if (had != 0) {
if (num_extra) {
2020-03-01 12:15:45 +00:00
for (unsigned int a = next; a < std::min(extra, AI_MAX_NUMBER_OF_TEXTURECOORDS - 1u); ++a) {
out[a + 1] = out[a];
2015-05-19 03:57:13 +00:00
}
}
++extra;
out[next++] = i;
}
// Bah ... seems not to be used at all. Push to end if enough space is available.
2015-05-19 03:57:13 +00:00
else {
out[extra++] = i;
++num_extra;
}
}
2020-03-01 12:15:45 +00:00
it = sorted.end() - 1;
2015-05-19 03:57:13 +00:00
break;
}
}
}
}
if (extra < AI_MAX_NUMBER_OF_TEXTURECOORDS) {
out[extra] = UINT_MAX;
}
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::FindVCChannels(const LWO::Surface &surf, LWO::SortedRep &sorted, const LWO::Layer &layer,
unsigned int out[AI_MAX_NUMBER_OF_COLOR_SETS]) {
2015-05-19 03:57:13 +00:00
unsigned int next = 0;
// Check whether we have an vc entry != 0 for one of the faces in 'sorted'
2020-03-01 12:15:45 +00:00
for (unsigned int i = 0; i < layer.mVColorChannels.size(); ++i) {
const LWO::VColorChannel &vc = layer.mVColorChannels[i];
2015-05-19 03:57:13 +00:00
if (surf.mVCMap == vc.name) {
// The vertex color map is explicitly requested by the surface so we need to take special care of it
2020-03-01 12:15:45 +00:00
for (unsigned int a = 0; a < std::min(next, AI_MAX_NUMBER_OF_COLOR_SETS - 1u); ++a) {
out[a + 1] = out[a];
2015-05-19 03:57:13 +00:00
}
out[0] = i;
++next;
2020-03-01 12:15:45 +00:00
} else {
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
for (LWO::SortedRep::iterator it = sorted.begin(); it != sorted.end(); ++it) {
const LWO::Face &face = layer.mFaces[*it];
2015-05-19 03:57:13 +00:00
for (unsigned int n = 0; n < face.mNumIndices; ++n) {
unsigned int idx = face.mIndices[n];
2020-03-01 12:15:45 +00:00
if (vc.abAssigned[idx] && ((aiColor4D *)&vc.rawData[0])[idx] != aiColor4D(0.0, 0.0, 0.0, 1.0)) {
2015-05-19 03:57:13 +00:00
if (next >= AI_MAX_NUMBER_OF_COLOR_SETS) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWO: Maximum number of vertex color channels for "
2020-03-01 12:15:45 +00:00
"this mesh reached. Skipping channel \'" +
vc.name + "\'");
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
} else {
2015-05-19 03:57:13 +00:00
out[next++] = i;
}
2020-03-01 12:15:45 +00:00
it = sorted.end() - 1;
2015-05-19 03:57:13 +00:00
break;
}
}
}
}
}
if (next != AI_MAX_NUMBER_OF_COLOR_SETS) {
out[next] = UINT_MAX;
}
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::LoadLWO2ImageMap(unsigned int size, LWO::Texture &tex) {
LE_NCONST uint8_t *const end = mFileBuffer + size;
while (true) {
if (mFileBuffer + 6 >= end) break;
2015-05-19 03:57:13 +00:00
LE_NCONST IFF::SubChunkHeader head = IFF::LoadSubChunk(mFileBuffer);
if (mFileBuffer + head.length > end)
throw DeadlyImportError("LWO2: Invalid SURF.BLOCK chunk length");
2020-03-01 12:15:45 +00:00
uint8_t *const next = mFileBuffer + head.length;
switch (head.type) {
case AI_LWO_PROJ:
tex.mapMode = (Texture::MappingMode)GetU2();
break;
case AI_LWO_WRAP:
tex.wrapModeWidth = (Texture::Wrap)GetU2();
tex.wrapModeHeight = (Texture::Wrap)GetU2();
break;
case AI_LWO_AXIS:
tex.majorAxis = (Texture::Axes)GetU2();
break;
case AI_LWO_IMAG:
tex.mClipIdx = GetU2();
break;
case AI_LWO_VMAP:
GetS0(tex.mUVChannelIndex, head.length);
break;
case AI_LWO_WRPH:
tex.wrapAmountH = GetF4();
break;
case AI_LWO_WRPW:
tex.wrapAmountW = GetF4();
break;
2015-05-19 03:57:13 +00:00
}
mFileBuffer = next;
}
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::LoadLWO2Procedural(unsigned int /*size*/, LWO::Texture &tex) {
2015-05-19 03:57:13 +00:00
// --- not supported at the moment
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWO2: Found procedural texture, this is not supported");
2015-05-19 03:57:13 +00:00
tex.bCanUse = false;
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::LoadLWO2Gradient(unsigned int /*size*/, LWO::Texture &tex) {
2015-05-19 03:57:13 +00:00
// --- not supported at the moment
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWO2: Found gradient texture, this is not supported");
2015-05-19 03:57:13 +00:00
tex.bCanUse = false;
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::LoadLWO2TextureHeader(unsigned int size, LWO::Texture &tex) {
LE_NCONST uint8_t *const end = mFileBuffer + size;
2015-05-19 03:57:13 +00:00
// get the ordinal string
2020-03-01 12:15:45 +00:00
GetS0(tex.ordinal, size);
2015-05-19 03:57:13 +00:00
// we could crash later if this is an empty string ...
2020-03-01 12:15:45 +00:00
if (!tex.ordinal.length()) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWO2: Ill-formed SURF.BLOK ordinal string");
2015-05-19 03:57:13 +00:00
tex.ordinal = "\x00";
}
2020-03-01 12:15:45 +00:00
while (true) {
if (mFileBuffer + 6 >= end) break;
2015-05-19 03:57:13 +00:00
const IFF::SubChunkHeader head = IFF::LoadSubChunk(mFileBuffer);
if (mFileBuffer + head.length > end)
throw DeadlyImportError("LWO2: Invalid texture header chunk length");
2020-03-01 12:15:45 +00:00
uint8_t *const next = mFileBuffer + head.length;
switch (head.type) {
case AI_LWO_CHAN:
tex.type = GetU4();
break;
case AI_LWO_ENAB:
tex.enabled = GetU2() ? true : false;
break;
case AI_LWO_OPAC:
tex.blendType = (Texture::BlendType)GetU2();
tex.mStrength = GetF4();
break;
2015-05-19 03:57:13 +00:00
}
mFileBuffer = next;
}
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::LoadLWO2TextureBlock(LE_NCONST IFF::SubChunkHeader *head, unsigned int size) {
2015-05-19 03:57:13 +00:00
ai_assert(!mSurfaces->empty());
2020-03-01 12:15:45 +00:00
LWO::Surface &surf = mSurfaces->back();
2015-05-19 03:57:13 +00:00
LWO::Texture tex;
// load the texture header
2020-03-01 12:15:45 +00:00
LoadLWO2TextureHeader(head->length, tex);
2015-05-19 03:57:13 +00:00
size -= head->length + 6;
// now get the exact type of the texture
2020-03-01 12:15:45 +00:00
switch (head->type) {
case AI_LWO_PROC:
LoadLWO2Procedural(size, tex);
break;
case AI_LWO_GRAD:
LoadLWO2Gradient(size, tex);
break;
case AI_LWO_IMAP:
LoadLWO2ImageMap(size, tex);
2015-05-19 03:57:13 +00:00
}
// get the destination channel
TextureList *listRef = nullptr;
2020-03-01 12:15:45 +00:00
switch (tex.type) {
case AI_LWO_COLR:
listRef = &surf.mColorTextures;
break;
case AI_LWO_DIFF:
listRef = &surf.mDiffuseTextures;
break;
case AI_LWO_SPEC:
listRef = &surf.mSpecularTextures;
break;
case AI_LWO_GLOS:
listRef = &surf.mGlossinessTextures;
break;
case AI_LWO_BUMP:
listRef = &surf.mBumpTextures;
break;
case AI_LWO_TRAN:
listRef = &surf.mOpacityTextures;
break;
case AI_LWO_REFL:
listRef = &surf.mReflectionTextures;
break;
default:
ASSIMP_LOG_WARN("LWO2: Encountered unknown texture type");
return;
2015-05-19 03:57:13 +00:00
}
// now attach the texture to the parent surface - sort by ordinal string
2020-03-01 12:15:45 +00:00
for (TextureList::iterator it = listRef->begin(); it != listRef->end(); ++it) {
if (::strcmp(tex.ordinal.c_str(), (*it).ordinal.c_str()) < 0) {
listRef->insert(it, tex);
2015-05-19 03:57:13 +00:00
return;
}
}
listRef->push_back(tex);
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::LoadLWO2ShaderBlock(LE_NCONST IFF::SubChunkHeader * /*head*/, unsigned int size) {
LE_NCONST uint8_t *const end = mFileBuffer + size;
2015-05-19 03:57:13 +00:00
ai_assert(!mSurfaces->empty());
2020-03-01 12:15:45 +00:00
LWO::Surface &surf = mSurfaces->back();
2015-05-19 03:57:13 +00:00
LWO::Shader shader;
// get the ordinal string
2020-03-01 12:15:45 +00:00
GetS0(shader.ordinal, size);
2015-05-19 03:57:13 +00:00
// we could crash later if this is an empty string ...
2020-03-01 12:15:45 +00:00
if (!shader.ordinal.length()) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_ERROR("LWO2: Ill-formed SURF.BLOK ordinal string");
2015-05-19 03:57:13 +00:00
shader.ordinal = "\x00";
}
// read the header
2020-03-01 12:15:45 +00:00
while (true) {
if (mFileBuffer + 6 >= end) break;
2015-05-19 03:57:13 +00:00
const IFF::SubChunkHeader head = IFF::LoadSubChunk(mFileBuffer);
if (mFileBuffer + head.length > end)
throw DeadlyImportError("LWO2: Invalid shader header chunk length");
2020-03-01 12:15:45 +00:00
uint8_t *const next = mFileBuffer + head.length;
switch (head.type) {
case AI_LWO_ENAB:
shader.enabled = GetU2() ? true : false;
break;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
case AI_LWO_FUNC:
GetS0(shader.functionName, head.length);
2015-05-19 03:57:13 +00:00
}
mFileBuffer = next;
}
// now attach the shader to the parent surface - sort by ordinal string
2020-03-01 12:15:45 +00:00
for (ShaderList::iterator it = surf.mShaders.begin(); it != surf.mShaders.end(); ++it) {
if (::strcmp(shader.ordinal.c_str(), (*it).ordinal.c_str()) < 0) {
surf.mShaders.insert(it, shader);
2015-05-19 03:57:13 +00:00
return;
}
}
surf.mShaders.push_back(shader);
}
// ------------------------------------------------------------------------------------------------
2020-03-01 12:15:45 +00:00
void LWOImporter::LoadLWO2Surface(unsigned int size) {
LE_NCONST uint8_t *const end = mFileBuffer + size;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
mSurfaces->push_back(LWO::Surface());
LWO::Surface &surf = mSurfaces->back();
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
GetS0(surf.mName, size);
2015-05-19 03:57:13 +00:00
// check whether this surface was derived from any other surface
std::string derived;
2020-03-01 12:15:45 +00:00
GetS0(derived, (unsigned int)(end - mFileBuffer));
if (derived.length()) {
2015-05-19 03:57:13 +00:00
// yes, find this surface
2020-03-01 12:15:45 +00:00
for (SurfaceList::iterator it = mSurfaces->begin(), itEnd = mSurfaces->end() - 1; it != itEnd; ++it) {
2015-05-19 03:57:13 +00:00
if ((*it).mName == derived) {
// we have it ...
surf = *it;
2020-03-01 12:15:45 +00:00
derived.clear();
break;
2015-05-19 03:57:13 +00:00
}
}
2020-03-01 12:15:45 +00:00
if (derived.size()) {
2018-04-20 14:23:24 +00:00
ASSIMP_LOG_WARN("LWO2: Unable to find source surface: " + derived);
2020-03-01 12:15:45 +00:00
}
2015-05-19 03:57:13 +00:00
}
2020-03-01 12:15:45 +00:00
while (true) {
2015-05-19 03:57:13 +00:00
if (mFileBuffer + 6 >= end)
break;
const IFF::SubChunkHeader head = IFF::LoadSubChunk(mFileBuffer);
if (mFileBuffer + head.length > end)
throw DeadlyImportError("LWO2: Invalid surface chunk length");
2020-03-01 12:15:45 +00:00
uint8_t *const next = mFileBuffer + head.length;
switch (head.type) {
// diffuse color
case AI_LWO_COLR: {
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, COLR, 12);
2015-05-19 03:57:13 +00:00
surf.mColor.r = GetF4();
surf.mColor.g = GetF4();
surf.mColor.b = GetF4();
break;
}
2020-03-01 12:15:45 +00:00
// diffuse strength ... hopefully
case AI_LWO_DIFF: {
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, DIFF, 4);
2015-05-19 03:57:13 +00:00
surf.mDiffuseValue = GetF4();
break;
}
2020-03-01 12:15:45 +00:00
// specular strength ... hopefully
case AI_LWO_SPEC: {
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, SPEC, 4);
2015-05-19 03:57:13 +00:00
surf.mSpecularValue = GetF4();
break;
}
2020-03-01 12:15:45 +00:00
// transparency
case AI_LWO_TRAN: {
2015-05-19 03:57:13 +00:00
// transparency explicitly disabled?
if (surf.mTransparency == 10e10f)
break;
2020-03-01 12:15:45 +00:00
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, TRAN, 4);
2015-05-19 03:57:13 +00:00
surf.mTransparency = GetF4();
break;
}
2020-03-01 12:15:45 +00:00
// additive transparency
case AI_LWO_ADTR: {
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, ADTR, 4);
2015-05-19 03:57:13 +00:00
surf.mAdditiveTransparency = GetF4();
break;
}
2020-03-01 12:15:45 +00:00
// wireframe mode
case AI_LWO_LINE: {
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, LINE, 2);
2015-05-19 03:57:13 +00:00
if (GetU2() & 0x1)
surf.mWireframe = true;
break;
}
2020-03-01 12:15:45 +00:00
// glossiness
case AI_LWO_GLOS: {
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, GLOS, 4);
2015-05-19 03:57:13 +00:00
surf.mGlossiness = GetF4();
break;
}
2020-03-01 12:15:45 +00:00
// bump intensity
case AI_LWO_BUMP: {
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, BUMP, 4);
2015-05-19 03:57:13 +00:00
surf.mBumpIntensity = GetF4();
break;
}
2020-03-01 12:15:45 +00:00
// color highlights
case AI_LWO_CLRH: {
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, CLRH, 4);
2015-05-19 03:57:13 +00:00
surf.mColorHighlights = GetF4();
break;
}
2020-03-01 12:15:45 +00:00
// index of refraction
case AI_LWO_RIND: {
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, RIND, 4);
2015-05-19 03:57:13 +00:00
surf.mIOR = GetF4();
break;
}
2020-03-01 12:15:45 +00:00
// polygon sidedness
case AI_LWO_SIDE: {
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, SIDE, 2);
2015-05-19 03:57:13 +00:00
surf.bDoubleSided = (3 == GetU2());
break;
}
2020-03-01 12:15:45 +00:00
// maximum smoothing angle
case AI_LWO_SMAN: {
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, SMAN, 4);
surf.mMaximumSmoothAngle = std::fabs(GetF4());
2015-05-19 03:57:13 +00:00
break;
}
2020-03-01 12:15:45 +00:00
// vertex color channel to be applied to the surface
case AI_LWO_VCOL: {
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, VCOL, 12);
surf.mDiffuseValue *= GetF4(); // strength
ReadVSizedIntLWO2(mFileBuffer); // skip envelope
surf.mVCMapType = GetU4(); // type of the channel
2015-05-19 03:57:13 +00:00
// name of the channel
2020-03-01 12:15:45 +00:00
GetS0(surf.mVCMap, (unsigned int)(next - mFileBuffer));
2015-05-19 03:57:13 +00:00
break;
}
2020-03-01 12:15:45 +00:00
// surface bock entry
case AI_LWO_BLOK: {
AI_LWO_VALIDATE_CHUNK_LENGTH(head.length, BLOK, 4);
2015-05-19 03:57:13 +00:00
IFF::SubChunkHeader head2 = IFF::LoadSubChunk(mFileBuffer);
2020-03-01 12:15:45 +00:00
switch (head2.type) {
case AI_LWO_PROC:
case AI_LWO_GRAD:
case AI_LWO_IMAP:
LoadLWO2TextureBlock(&head2, head.length);
break;
case AI_LWO_SHDR:
LoadLWO2ShaderBlock(&head2, head.length);
break;
2015-05-19 03:57:13 +00:00
2020-03-01 12:15:45 +00:00
default:
ASSIMP_LOG_WARN("LWO2: Found an unsupported surface BLOK");
2015-05-19 03:57:13 +00:00
};
break;
}
}
mFileBuffer = next;
}
}
#endif // !! ASSIMP_BUILD_NO_X_IMPORTER