From 8056107c6e7824b780a2a98c4ca5a9749d986e5b Mon Sep 17 00:00:00 2001 From: Capostrophic Date: Sat, 5 Oct 2019 15:02:42 +0300 Subject: [PATCH 1/2] More minor NIF improvements --- components/nif/controller.hpp | 2 +- components/nif/data.cpp | 10 ++++---- components/nif/property.cpp | 42 ++++++++++++++++----------------- components/nif/property.hpp | 16 ++++--------- components/nifosg/nifloader.cpp | 42 ++++++++++++++++----------------- 5 files changed, 50 insertions(+), 62 deletions(-) diff --git a/components/nif/controller.hpp b/components/nif/controller.hpp index 52ab6f1f6..2fe28fe1d 100644 --- a/components/nif/controller.hpp +++ b/components/nif/controller.hpp @@ -112,7 +112,7 @@ class NiUVController : public Controller { public: NiUVDataPtr data; - int uvSet; + unsigned int uvSet; void read(NIFStream *nif); void post(NIFFile *nif); diff --git a/components/nif/data.cpp b/components/nif/data.cpp index 5f7722794..4e1487f69 100644 --- a/components/nif/data.cpp +++ b/components/nif/data.cpp @@ -102,12 +102,12 @@ void NiTriStripsData::read(NIFStream *nif) std::vector lengths; nif->getUShorts(lengths, numStrips); + if (!numStrips) + return; + + strips.resize(numStrips); for (int i = 0; i < numStrips; i++) - { - std::vector strip; - nif->getUShorts(strip, lengths[i]); - strips.emplace_back(strip); - } + nif->getUShorts(strips[i], lengths[i]); } void NiAutoNormalParticlesData::read(NIFStream *nif) diff --git a/components/nif/property.cpp b/components/nif/property.cpp index 47c6d35b3..1398326be 100644 --- a/components/nif/property.cpp +++ b/components/nif/property.cpp @@ -18,14 +18,13 @@ void NiTexturingProperty::Texture::read(NIFStream *nif) if(!inUse) return; texture.read(nif); - clamp = nif->getInt(); - filter = nif->getInt(); - uvSet = nif->getInt(); + clamp = nif->getUInt(); + nif->skip(4); // Filter mode. Ignoring because global filtering settings are more sensible + uvSet = nif->getUInt(); - // I have no idea, but I think these are actually two - // PS2-specific shorts (ps2L and ps2K), followed by an unknown - // short. - nif->skip(6); + // Two PS2-specific shorts. + nif->skip(4); + nif->skip(2); // Unknown short } void NiTexturingProperty::Texture::post(NIFFile *nif) @@ -36,26 +35,25 @@ void NiTexturingProperty::Texture::post(NIFFile *nif) void NiTexturingProperty::read(NIFStream *nif) { Property::read(nif); - apply = nif->getInt(); + apply = nif->getUInt(); - // Unknown, always 7. Probably the number of textures to read - // below - nif->getInt(); + unsigned int numTextures = nif->getUInt(); - textures[0].read(nif); // Base - textures[1].read(nif); // Dark - textures[2].read(nif); // Detail - textures[3].read(nif); // Gloss (never present) - textures[4].read(nif); // Glow - textures[5].read(nif); // Bump map - if(textures[5].inUse) + if (!numTextures) + return; + + textures.resize(numTextures); + for (unsigned int i = 0; i < numTextures; i++) { + textures[i].read(nif); // Ignore these at the moment - /*float lumaScale =*/ nif->getFloat(); - /*float lumaOffset =*/ nif->getFloat(); - /*const Vector4 *lumaMatrix =*/ nif->getVector4(); + if (i == 5 && textures[5].inUse) // Bump map settings + { + /*float lumaScale =*/ nif->getFloat(); + /*float lumaOffset =*/ nif->getFloat(); + /*const Vector4 *lumaMatrix =*/ nif->getVector4(); + } } - textures[6].read(nif); // Decal } void NiTexturingProperty::post(NIFFile *nif) diff --git a/components/nif/property.hpp b/components/nif/property.hpp index f46f8ef27..a3f399b83 100644 --- a/components/nif/property.hpp +++ b/components/nif/property.hpp @@ -51,17 +51,10 @@ public: 3 - wrapS wrapT */ - /* Filter: - 0 - nearest - 1 - bilinear - 2 - trilinear - 3, 4, 5 - who knows - */ bool inUse; NiSourceTexturePtr texture; - int clamp, uvSet, filter; - short unknown2; + unsigned int clamp, uvSet; void read(NIFStream *nif); void post(NIFFile *nif); @@ -74,7 +67,7 @@ public: 3 - hilight // These two are for PS2 only? 4 - hilight2 */ - int apply; + unsigned int apply; /* * The textures in this list are as follows: @@ -82,7 +75,7 @@ public: * 0 - Base texture * 1 - Dark texture * 2 - Detail texture - * 3 - Gloss texture (never used?) + * 3 - Gloss texture * 4 - Glow texture * 5 - Bump map texture * 6 - Decal texture @@ -96,10 +89,9 @@ public: GlowTexture = 4, BumpTexture = 5, DecalTexture = 6, - NumTextures = 7 // Sentry value }; - Texture textures[7]; + std::vector textures; void read(NIFStream *nif); void post(NIFFile *nif); diff --git a/components/nifosg/nifloader.cpp b/components/nifosg/nifloader.cpp index 58e336b5d..f32224ca8 100644 --- a/components/nifosg/nifloader.cpp +++ b/components/nifosg/nifloader.cpp @@ -262,7 +262,7 @@ namespace NifOsg osg::ref_ptr textkeys (new TextKeyMapHolder); - osg::ref_ptr created = handleNode(nifNode, nullptr, imageManager, std::vector(), 0, false, false, false, &textkeys->mTextKeys); + osg::ref_ptr created = handleNode(nifNode, nullptr, imageManager, std::vector(), 0, false, false, false, &textkeys->mTextKeys); if (nif->getUseSkinning()) { @@ -288,7 +288,7 @@ namespace NifOsg return created; } - void applyNodeProperties(const Nif::Node *nifNode, osg::Node *applyTo, SceneUtil::CompositeStateSetUpdater* composite, Resource::ImageManager* imageManager, std::vector& boundTextures, int animflags) + void applyNodeProperties(const Nif::Node *nifNode, osg::Node *applyTo, SceneUtil::CompositeStateSetUpdater* composite, Resource::ImageManager* imageManager, std::vector& boundTextures, int animflags) { const Nif::PropertyList& props = nifNode->props; for (size_t i = 0; i setTextureSize(image->s(), image->t()); texture2d->setName("envMap"); - unsigned int clamp = static_cast(textureEffect->clamp); - int wrapT = (clamp) & 0x1; - int wrapS = (clamp >> 1) & 0x1; + bool wrapT = textureEffect->clamp & 0x1; + bool wrapS = (textureEffect->clamp >> 1) & 0x1; texture2d->setWrap(osg::Texture::WRAP_S, wrapS ? osg::Texture::REPEAT : osg::Texture::CLAMP_TO_EDGE); texture2d->setWrap(osg::Texture::WRAP_T, wrapT ? osg::Texture::REPEAT : osg::Texture::CLAMP_TO_EDGE); @@ -492,7 +491,7 @@ namespace NifOsg } osg::ref_ptr handleNode(const Nif::Node* nifNode, osg::Group* parentNode, Resource::ImageManager* imageManager, - std::vector boundTextures, int animflags, bool skipMeshes, bool hasMarkers, bool isAnimated, TextKeyMap* textKeys, osg::Node* rootNode=nullptr) + std::vector boundTextures, int animflags, bool skipMeshes, bool hasMarkers, bool isAnimated, TextKeyMap* textKeys, osg::Node* rootNode=nullptr) { if (rootNode != nullptr && Misc::StringUtils::ciEqual(nifNode->name, "Bounding Box")) return nullptr; @@ -657,7 +656,7 @@ namespace NifOsg return node; } - void handleMeshControllers(const Nif::Node *nifNode, osg::Node* node, SceneUtil::CompositeStateSetUpdater* composite, const std::vector &boundTextures, int animflags) + void handleMeshControllers(const Nif::Node *nifNode, osg::Node* node, SceneUtil::CompositeStateSetUpdater* composite, const std::vector &boundTextures, int animflags) { for (Nif::ControllerPtr ctrl = nifNode->controller; !ctrl.empty(); ctrl = ctrl->next) { @@ -666,7 +665,7 @@ namespace NifOsg if (ctrl->recType == Nif::RC_NiUVController) { const Nif::NiUVController *niuvctrl = static_cast(ctrl.getPtr()); - const int uvSet = niuvctrl->uvSet; + const unsigned int uvSet = niuvctrl->uvSet; std::set texUnits; // UVController should work only for textures which use a given UV Set, usually 0. for (unsigned int i=0; i& vertices, const std::vector& normals, const std::vector>& uvlist, const std::vector& colors, const std::vector& boundTextures, const std::string& name) + void triCommonToGeometry(osg::Geometry *geometry, const std::vector& vertices, const std::vector& normals, const std::vector>& uvlist, const std::vector& colors, const std::vector& boundTextures, const std::string& name) { if (!vertices.empty()) geometry->setVertexArray(new osg::Vec3Array(vertices.size(), vertices.data())); @@ -1057,9 +1056,9 @@ namespace NifOsg geometry->setColorArray(new osg::Vec4Array(colors.size(), colors.data()), osg::Array::BIND_PER_VERTEX); int textureStage = 0; - for (const int uvSet : boundTextures) + for (const unsigned int uvSet : boundTextures) { - if (uvSet >= (int)uvlist.size()) + if (uvSet >= uvlist.size()) { Log(Debug::Verbose) << "Out of bounds UV set " << uvSet << " on shape \"" << name << "\" in " << mFilename; if (!uvlist.empty()) @@ -1072,7 +1071,7 @@ namespace NifOsg } } - void triShapeToGeometry(const Nif::Node *nifNode, osg::Geometry *geometry, osg::Node* parentNode, SceneUtil::CompositeStateSetUpdater* composite, const std::vector& boundTextures, int animflags) + void triShapeToGeometry(const Nif::Node *nifNode, osg::Geometry *geometry, osg::Node* parentNode, SceneUtil::CompositeStateSetUpdater* composite, const std::vector& boundTextures, int animflags) { bool vertexColorsPresent = false; if (nifNode->recType == Nif::RC_NiTriShape) @@ -1119,7 +1118,7 @@ namespace NifOsg applyDrawableProperties(parentNode, drawableProps, composite, vertexColorsPresent, animflags, false); } - void handleTriShape(const Nif::Node* nifNode, osg::Group* parentNode, SceneUtil::CompositeStateSetUpdater* composite, const std::vector& boundTextures, int animflags) + void handleTriShape(const Nif::Node* nifNode, osg::Group* parentNode, SceneUtil::CompositeStateSetUpdater* composite, const std::vector& boundTextures, int animflags) { assert(nifNode->recType == Nif::RC_NiTriShape || nifNode->recType == Nif::RC_NiTriStrips); osg::ref_ptr drawable; @@ -1151,7 +1150,7 @@ namespace NifOsg parentNode->addChild(drawable); } - osg::ref_ptr handleMorphGeometry(const Nif::NiGeomMorpherController* morpher, osg::ref_ptr sourceGeometry, osg::Node* parentNode, SceneUtil::CompositeStateSetUpdater* composite, const std::vector& boundTextures, int animflags) + osg::ref_ptr handleMorphGeometry(const Nif::NiGeomMorpherController* morpher, osg::ref_ptr sourceGeometry, osg::Node* parentNode, SceneUtil::CompositeStateSetUpdater* composite, const std::vector& boundTextures, int animflags) { osg::ref_ptr morphGeom = new SceneUtil::MorphGeometry; morphGeom->setSourceGeometry(sourceGeometry); @@ -1167,7 +1166,7 @@ namespace NifOsg } void handleSkinnedTriShape(const Nif::Node *nifNode, osg::Group *parentNode, SceneUtil::CompositeStateSetUpdater* composite, - const std::vector& boundTextures, int animflags) + const std::vector& boundTextures, int animflags) { assert(nifNode->recType == Nif::RC_NiTriShape || nifNode->recType == Nif::RC_NiTriStrips); osg::ref_ptr geometry (new osg::Geometry); @@ -1392,7 +1391,7 @@ namespace NifOsg return image; } - void handleTextureProperty(const Nif::NiTexturingProperty* texprop, osg::StateSet* stateset, SceneUtil::CompositeStateSetUpdater* composite, Resource::ImageManager* imageManager, std::vector& boundTextures, int animflags) + void handleTextureProperty(const Nif::NiTexturingProperty* texprop, osg::StateSet* stateset, SceneUtil::CompositeStateSetUpdater* composite, Resource::ImageManager* imageManager, std::vector& boundTextures, int animflags) { if (!boundTextures.empty()) { @@ -1403,7 +1402,7 @@ namespace NifOsg } // If this loop is changed such that the base texture isn't guaranteed to end up in texture unit 0, the shadow casting shader will need to be updated accordingly. - for (int i=0; itextures.size(); ++i) { if (texprop->textures[i].inUse) { @@ -1451,14 +1450,13 @@ namespace NifOsg else texture2d = new osg::Texture2D; - unsigned int clamp = static_cast(tex.clamp); - int wrapT = (clamp) & 0x1; - int wrapS = (clamp >> 1) & 0x1; + bool wrapT = tex.clamp & 0x1; + bool wrapS = (tex.clamp >> 1) & 0x1; texture2d->setWrap(osg::Texture::WRAP_S, wrapS ? osg::Texture::REPEAT : osg::Texture::CLAMP_TO_EDGE); texture2d->setWrap(osg::Texture::WRAP_T, wrapT ? osg::Texture::REPEAT : osg::Texture::CLAMP_TO_EDGE); - int texUnit = boundTextures.size(); + unsigned int texUnit = boundTextures.size(); stateset->setTextureAttributeAndModes(texUnit, texture2d, osg::StateAttribute::ON); @@ -1547,7 +1545,7 @@ namespace NifOsg } void handleProperty(const Nif::Property *property, - osg::Node *node, SceneUtil::CompositeStateSetUpdater* composite, Resource::ImageManager* imageManager, std::vector& boundTextures, int animflags) + osg::Node *node, SceneUtil::CompositeStateSetUpdater* composite, Resource::ImageManager* imageManager, std::vector& boundTextures, int animflags) { switch (property->recType) { From 7c4743fdd1534930181276f5d5f260a102685ed9 Mon Sep 17 00:00:00 2001 From: Capostrophic Date: Wed, 9 Oct 2019 17:08:32 +0300 Subject: [PATCH 2/2] Convert recordptr typedefs to using directives --- components/nif/recordptr.hpp | 48 ++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/components/nif/recordptr.hpp b/components/nif/recordptr.hpp index fbd148a04..d165111b8 100644 --- a/components/nif/recordptr.hpp +++ b/components/nif/recordptr.hpp @@ -143,31 +143,31 @@ class NiAutoNormalParticlesData; class NiPalette; struct NiParticleModifier; -typedef RecordPtrT NodePtr; -typedef RecordPtrT ExtraPtr; -typedef RecordPtrT NiUVDataPtr; -typedef RecordPtrT NiPosDataPtr; -typedef RecordPtrT NiVisDataPtr; -typedef RecordPtrT ControllerPtr; -typedef RecordPtrT NamedPtr; -typedef RecordPtrT NiSkinDataPtr; -typedef RecordPtrT NiMorphDataPtr; -typedef RecordPtrT NiPixelDataPtr; -typedef RecordPtrT NiFloatDataPtr; -typedef RecordPtrT NiColorDataPtr; -typedef RecordPtrT NiKeyframeDataPtr; -typedef RecordPtrT NiTriShapeDataPtr; -typedef RecordPtrT NiTriStripsDataPtr; -typedef RecordPtrT NiSkinInstancePtr; -typedef RecordPtrT NiSourceTexturePtr; -typedef RecordPtrT NiRotatingParticlesDataPtr; -typedef RecordPtrT NiAutoNormalParticlesDataPtr; -typedef RecordPtrT NiPalettePtr; -typedef RecordPtrT NiParticleModifierPtr; +using NodePtr = RecordPtrT; +using ExtraPtr = RecordPtrT; +using NiUVDataPtr = RecordPtrT; +using NiPosDataPtr = RecordPtrT; +using NiVisDataPtr = RecordPtrT; +using ControllerPtr = RecordPtrT; +using NamedPtr = RecordPtrT; +using NiSkinDataPtr = RecordPtrT; +using NiMorphDataPtr = RecordPtrT; +using NiPixelDataPtr = RecordPtrT; +using NiFloatDataPtr = RecordPtrT; +using NiColorDataPtr = RecordPtrT; +using NiKeyframeDataPtr = RecordPtrT; +using NiTriShapeDataPtr = RecordPtrT; +using NiTriStripsDataPtr = RecordPtrT; +using NiSkinInstancePtr = RecordPtrT; +using NiSourceTexturePtr = RecordPtrT; +using NiRotatingParticlesDataPtr = RecordPtrT; +using NiAutoNormalParticlesDataPtr = RecordPtrT; +using NiPalettePtr = RecordPtrT; +using NiParticleModifierPtr = RecordPtrT; -typedef RecordListT NodeList; -typedef RecordListT PropertyList; -typedef RecordListT NiSourceTextureList; +using NodeList = RecordListT; +using PropertyList = RecordListT; +using NiSourceTextureList = RecordListT; } // Namespace #endif