diff --git a/components/nif/controller.cpp b/components/nif/controller.cpp index c900f90282..ee219fe5b5 100644 --- a/components/nif/controller.cpp +++ b/components/nif/controller.cpp @@ -224,15 +224,12 @@ namespace Nif void NiMaterialColorController::read(NIFStream* nif) { NiPoint3InterpController::read(nif); - // Two bits that correspond to the controlled material color. - // 00: Ambient - // 01: Diffuse - // 10: Specular - // 11: Emissive + if (nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 0)) - mTargetColor = nif->getUShort() & 3; + mTargetColor = static_cast(nif->get() & 3); else - mTargetColor = (flags >> 4) & 3; + mTargetColor = static_cast((flags >> 4) & 3); + if (nif->getVersion() <= NIFStream::generateVersion(10, 1, 0, 103)) mData.read(nif); } @@ -240,6 +237,7 @@ namespace Nif void NiMaterialColorController::post(Reader& nif) { NiPoint3InterpController::post(nif); + mData.post(nif); } diff --git a/components/nif/controller.hpp b/components/nif/controller.hpp index e21b04bb72..4b46840d4a 100644 --- a/components/nif/controller.hpp +++ b/components/nif/controller.hpp @@ -157,8 +157,16 @@ namespace Nif struct NiMaterialColorController : public NiPoint3InterpController { + enum class TargetColor + { + Ambient = 0, + Diffuse = 1, + Specular = 2, + Emissive = 3, + }; + NiPosDataPtr mData; - unsigned int mTargetColor; + TargetColor mTargetColor; void read(NIFStream* nif) override; void post(Reader& nif) override; diff --git a/components/nifosg/controller.cpp b/components/nifosg/controller.cpp index 20a3ee92e4..8e9efba8a9 100644 --- a/components/nifosg/controller.cpp +++ b/components/nifosg/controller.cpp @@ -444,7 +444,7 @@ namespace NifOsg MaterialColorController::MaterialColorController( const Nif::NiMaterialColorController* ctrl, const osg::Material* baseMaterial) - : mTargetColor(static_cast(ctrl->mTargetColor)) + : mTargetColor(ctrl->mTargetColor) , mBaseMaterial(baseMaterial) { if (!ctrl->mInterpolator.empty()) @@ -477,30 +477,31 @@ namespace NifOsg { osg::Vec3f value = mData.interpKey(getInputValue(nv)); osg::Material* mat = static_cast(stateset->getAttribute(osg::StateAttribute::MATERIAL)); + using TargetColor = Nif::NiMaterialColorController::TargetColor; switch (mTargetColor) { - case Diffuse: + case TargetColor::Diffuse: { osg::Vec4f diffuse = mat->getDiffuse(osg::Material::FRONT_AND_BACK); diffuse.set(value.x(), value.y(), value.z(), diffuse.a()); mat->setDiffuse(osg::Material::FRONT_AND_BACK, diffuse); break; } - case Specular: + case TargetColor::Specular: { osg::Vec4f specular = mat->getSpecular(osg::Material::FRONT_AND_BACK); specular.set(value.x(), value.y(), value.z(), specular.a()); mat->setSpecular(osg::Material::FRONT_AND_BACK, specular); break; } - case Emissive: + case TargetColor::Emissive: { osg::Vec4f emissive = mat->getEmission(osg::Material::FRONT_AND_BACK); emissive.set(value.x(), value.y(), value.z(), emissive.a()); mat->setEmission(osg::Material::FRONT_AND_BACK, emissive); break; } - case Ambient: + case TargetColor::Ambient: default: { osg::Vec4f ambient = mat->getAmbient(osg::Material::FRONT_AND_BACK); diff --git a/components/nifosg/controller.hpp b/components/nifosg/controller.hpp index 90e87366e1..1025f1988e 100644 --- a/components/nifosg/controller.hpp +++ b/components/nifosg/controller.hpp @@ -336,13 +336,6 @@ namespace NifOsg class MaterialColorController : public SceneUtil::StateSetUpdater, public SceneUtil::Controller { public: - enum TargetColor - { - Ambient = 0, - Diffuse = 1, - Specular = 2, - Emissive = 3 - }; MaterialColorController(const Nif::NiMaterialColorController* ctrl, const osg::Material* baseMaterial); MaterialColorController(); MaterialColorController(const MaterialColorController& copy, const osg::CopyOp& copyop); @@ -355,7 +348,7 @@ namespace NifOsg private: Vec3Interpolator mData; - TargetColor mTargetColor = Ambient; + Nif::NiMaterialColorController::TargetColor mTargetColor; osg::ref_ptr mBaseMaterial; }; diff --git a/components/nifosg/nifloader.cpp b/components/nifosg/nifloader.cpp index 9683de9a23..8a4a62b017 100644 --- a/components/nifosg/nifloader.cpp +++ b/components/nifosg/nifloader.cpp @@ -988,18 +988,17 @@ namespace NifOsg { const Nif::NiMaterialColorController* matctrl = static_cast(ctrl.getPtr()); - if (matctrl->mData.empty() && matctrl->mInterpolator.empty()) + Nif::NiInterpolatorPtr interp = matctrl->mInterpolator; + if (matctrl->mData.empty() && interp.empty()) continue; - auto targetColor = static_cast(matctrl->mTargetColor); if (mVersion <= Nif::NIFFile::VER_MW - && targetColor == MaterialColorController::TargetColor::Specular) + && matctrl->mTargetColor == Nif::NiMaterialColorController::TargetColor::Specular) continue; - if (!matctrl->mInterpolator.empty() - && matctrl->mInterpolator->recType != Nif::RC_NiPoint3Interpolator) + if (!interp.empty() && interp->recType != Nif::RC_NiPoint3Interpolator) { Log(Debug::Error) << "Unsupported interpolator type for NiMaterialColorController " << matctrl->recIndex - << " in " << mFilename << ": " << matctrl->mInterpolator->recName; + << " in " << mFilename << ": " << interp->recName; continue; } osg::ref_ptr osgctrl = new MaterialColorController(matctrl, baseMaterial);