From 2e847a12c4b630b2c4d324a963f98285ddc4b261 Mon Sep 17 00:00:00 2001 From: Alexei Kotov Date: Thu, 31 Aug 2023 20:33:43 +0300 Subject: [PATCH] Modernize NiSourceTexture and BSShaderTextureSet --- components/nif/texture.cpp | 55 +++++++++------------- components/nif/texture.hpp | 83 +++++++++++++++++++-------------- components/nifosg/nifloader.cpp | 34 +++++++------- 3 files changed, 89 insertions(+), 83 deletions(-) diff --git a/components/nif/texture.cpp b/components/nif/texture.cpp index 0e86c04079..116ded6f7e 100644 --- a/components/nif/texture.cpp +++ b/components/nif/texture.cpp @@ -7,50 +7,41 @@ namespace Nif void NiSourceTexture::read(NIFStream* nif) { - Named::read(nif); + NiTexture::read(nif); - external = nif->getChar() != 0; - bool internal = false; - if (external) - filename = nif->getString(); - else - { - if (nif->getVersion() <= NIFStream::generateVersion(10, 0, 1, 3)) - internal = nif->getChar(); - if (nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 0)) - filename = nif->getString(); // Original file path of the internal texture - } - if (nif->getVersion() <= NIFStream::generateVersion(10, 0, 1, 3)) - { - if (!external && internal) - data.read(nif); - } - else - { - data.read(nif); - } + nif->read(mExternal); + if (mExternal || nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 0)) + nif->read(mFile); + + bool hasData = nif->getVersion() >= NIFStream::generateVersion(10, 0, 1, 4); + if (!hasData && !mExternal) + nif->read(hasData); + + if (hasData) + mData.read(nif); - pixel = nif->getUInt(); - mipmap = nif->getUInt(); - alpha = nif->getUInt(); + mPrefs.mPixelLayout = static_cast(nif->get()); + mPrefs.mUseMipMaps = static_cast(nif->get()); + mPrefs.mAlphaFormat = static_cast(nif->get()); - // Renderer hints, typically of no use for us - /* bool mIsStatic = */ nif->getChar(); + nif->read(mIsStatic); if (nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 103)) - /* bool mDirectRendering = */ nif->getBoolean(); - if (nif->getVersion() >= NIFStream::generateVersion(20, 2, 0, 4)) - /* bool mPersistRenderData = */ nif->getBoolean(); + { + nif->read(mDirectRendering); + if (nif->getVersion() >= NIFStream::generateVersion(20, 2, 0, 4)) + nif->read(mPersistRenderData); + } } void NiSourceTexture::post(Reader& nif) { - Named::post(nif); - data.post(nif); + NiTexture::post(nif); + mData.post(nif); } void BSShaderTextureSet::read(NIFStream* nif) { - nif->getSizedStrings(textures, nif->getUInt()); + nif->getSizedStrings(mTextures, nif->get()); } } diff --git a/components/nif/texture.hpp b/components/nif/texture.hpp index b74b44bec0..a326b47f14 100644 --- a/components/nif/texture.hpp +++ b/components/nif/texture.hpp @@ -12,35 +12,48 @@ namespace Nif struct NiSourceTexture : public NiTexture { - // Is this an external (references a separate texture file) or - // internal (data is inside the nif itself) texture? - bool external; + enum class PixelLayout : uint32_t + { + Palette = 0, + HighColor = 1, + TrueColor = 2, + Compressed = 3, + BumpMap = 4, + Default = 5, + }; + + enum class MipMapFormat : uint32_t + { + No = 0, + Yes = 1, + Default = 2, + }; + + enum class AlphaFormat : uint32_t + { + None = 0, + Binary = 1, + Smooth = 2, + Default = 3, + }; + + struct FormatPrefs + { + PixelLayout mPixelLayout; + MipMapFormat mUseMipMaps; + AlphaFormat mAlphaFormat; + }; - std::string filename; // In case of external textures - NiPixelDataPtr data; // In case of internal textures + char mExternal; // References external file - /* Pixel layout - 0 - Palettised - 1 - High color 16 - 2 - True color 32 - 3 - Compressed - 4 - Bumpmap - 5 - Default */ - unsigned int pixel; + std::string mFile; + NiPixelDataPtr mData; - /* Mipmap format - 0 - no - 1 - yes - 2 - default */ - unsigned int mipmap; + FormatPrefs mPrefs; - /* Alpha - 0 - none - 1 - binary - 2 - smooth - 3 - default (use material alpha, or multiply material with texture if present) - */ - unsigned int alpha; + char mIsStatic{ 1 }; + bool mDirectRendering{ true }; + bool mPersistRenderData{ false }; void read(NIFStream* nif) override; void post(Reader& nif) override; @@ -48,18 +61,18 @@ namespace Nif struct BSShaderTextureSet : public Record { - enum TextureType + enum class TextureType : uint32_t { - TextureType_Base = 0, - TextureType_Normal = 1, - TextureType_Glow = 2, - TextureType_Parallax = 3, - TextureType_Env = 4, - TextureType_EnvMask = 5, - TextureType_Subsurface = 6, - TextureType_BackLighting = 7 + Base = 0, + Normal = 1, + Glow = 2, + Parallax = 3, + Environment = 4, + EnvironmentMask = 5, + Subsurface = 6, + BackLighting = 7, }; - std::vector textures; + std::vector mTextures; void read(NIFStream* nif) override; }; diff --git a/components/nifosg/nifloader.cpp b/components/nifosg/nifloader.cpp index 3a8e3017c9..c8c89a0660 100644 --- a/components/nifosg/nifloader.cpp +++ b/components/nifosg/nifloader.cpp @@ -510,14 +510,14 @@ namespace NifOsg return nullptr; osg::ref_ptr image; - if (!st->external && !st->data.empty()) + if (st->mExternal) { - image = handleInternalTexture(st->data.getPtr()); + std::string filename = Misc::ResourceHelpers::correctTexturePath(st->mFile, imageManager->getVFS()); + image = imageManager->getImage(filename); } - else + else if (!st->mData.empty()) { - std::string filename = Misc::ResourceHelpers::correctTexturePath(st->filename, imageManager->getVFS()); - image = imageManager->getImage(filename); + image = handleInternalTexture(st->mData.getPtr()); } return image; } @@ -2009,15 +2009,15 @@ namespace NifOsg const unsigned int uvSet = 0; - for (size_t i = 0; i < textureSet->textures.size(); ++i) + for (size_t i = 0; i < textureSet->mTextures.size(); ++i) { - if (textureSet->textures[i].empty()) + if (textureSet->mTextures[i].empty()) continue; - switch (i) + switch (static_cast(i)) { - case Nif::BSShaderTextureSet::TextureType_Base: - case Nif::BSShaderTextureSet::TextureType_Normal: - case Nif::BSShaderTextureSet::TextureType_Glow: + case Nif::BSShaderTextureSet::TextureType::Base: + case Nif::BSShaderTextureSet::TextureType::Normal: + case Nif::BSShaderTextureSet::TextureType::Glow: break; default: { @@ -2027,7 +2027,7 @@ namespace NifOsg } } std::string filename - = Misc::ResourceHelpers::correctTexturePath(textureSet->textures[i], imageManager->getVFS()); + = Misc::ResourceHelpers::correctTexturePath(textureSet->mTextures[i], imageManager->getVFS()); osg::ref_ptr image = imageManager->getImage(filename); osg::ref_ptr texture2d = new osg::Texture2D(image); if (image) @@ -2036,17 +2036,19 @@ namespace NifOsg unsigned int texUnit = boundTextures.size(); stateset->setTextureAttributeAndModes(texUnit, texture2d, osg::StateAttribute::ON); // BSShaderTextureSet presence means there's no need for FFP support for the affected node - switch (i) + switch (static_cast(i)) { - case Nif::BSShaderTextureSet::TextureType_Base: + case Nif::BSShaderTextureSet::TextureType::Base: texture2d->setName("diffuseMap"); break; - case Nif::BSShaderTextureSet::TextureType_Normal: + case Nif::BSShaderTextureSet::TextureType::Normal: texture2d->setName("normalMap"); break; - case Nif::BSShaderTextureSet::TextureType_Glow: + case Nif::BSShaderTextureSet::TextureType::Glow: texture2d->setName("emissiveMap"); break; + default: + break; } boundTextures.emplace_back(uvSet); }