From 523e7e8228946aff170cafdf6b0f84e769392318 Mon Sep 17 00:00:00 2001 From: Alexei Kotov Date: Sun, 3 Sep 2023 18:43:08 +0300 Subject: [PATCH] Modernize NiRotatingParticlesData and NiVisData --- components/nif/data.cpp | 21 +++++++++++++++------ components/nif/data.hpp | 8 ++------ components/nifosg/controller.cpp | 14 ++++++-------- components/nifosg/controller.hpp | 13 ++++++------- 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/components/nif/data.cpp b/components/nif/data.cpp index 453fa64564..6086a07aa9 100644 --- a/components/nif/data.cpp +++ b/components/nif/data.cpp @@ -194,7 +194,12 @@ namespace Nif { NiParticlesData::read(nif); - if (nif->getVersion() <= NIFStream::generateVersion(4, 2, 2, 0) && nif->getBoolean()) + if (nif->getVersion() > NIFStream::generateVersion(4, 2, 2, 0)) + return; + + bool hasRotations; + nif->read(hasRotations); + if (hasRotations) nif->readVector(rotations, vertices.size()); } @@ -281,12 +286,16 @@ namespace Nif void NiVisData::read(NIFStream* nif) { - int count = nif->getInt(); - mVis.resize(count); - for (size_t i = 0; i < mVis.size(); i++) + mKeys = std::make_shared>(); + uint32_t numKeys; + nif->read(numKeys); + for (size_t i = 0; i < numKeys; i++) { - mVis[i].time = nif->getFloat(); - mVis[i].isSet = (nif->getChar() != 0); + float time; + char value; + nif->read(time); + nif->read(value); + (*mKeys)[time] = (value != 0); } } diff --git a/components/nif/data.hpp b/components/nif/data.hpp index 8cde0a705f..d9f118f0fe 100644 --- a/components/nif/data.hpp +++ b/components/nif/data.hpp @@ -161,12 +161,8 @@ namespace Nif struct NiVisData : public Record { - struct VisData - { - float time; - bool isSet; - }; - std::vector mVis; + // TODO: investigate possible use of ByteKeyMap + std::shared_ptr> mKeys; void read(NIFStream* nif) override; }; diff --git a/components/nifosg/controller.cpp b/components/nifosg/controller.cpp index ce5485ac53..20a3ee92e4 100644 --- a/components/nifosg/controller.cpp +++ b/components/nifosg/controller.cpp @@ -319,7 +319,7 @@ namespace NifOsg = ByteInterpolator(static_cast(ctrl->mInterpolator.getPtr())); } else if (!ctrl->mData.empty()) - mData = ctrl->mData->mVis; + mData = ctrl->mData->mKeys; } VisController::VisController() {} @@ -338,15 +338,13 @@ namespace NifOsg if (!mInterpolator.empty()) return mInterpolator.interpKey(time); - if (mData.size() == 0) + if (mData->empty()) return true; - for (size_t i = 1; i < mData.size(); i++) - { - if (mData[i].time > time) - return mData[i - 1].isSet; - } - return mData.back().isSet; + auto iter = mData->upper_bound(time); + if (iter != mData->begin()) + --iter; + return iter->second; } void VisController::operator()(osg::Node* node, osg::NodeVisitor* nv) diff --git a/components/nifosg/controller.hpp b/components/nifosg/controller.hpp index 7666fdeb3f..90e87366e1 100644 --- a/components/nifosg/controller.hpp +++ b/components/nifosg/controller.hpp @@ -1,19 +1,18 @@ #ifndef COMPONENTS_NIFOSG_CONTROLLER_H #define COMPONENTS_NIFOSG_CONTROLLER_H +#include +#include + +#include + #include #include #include - #include #include #include -#include -#include - -#include - namespace osg { class Material; @@ -283,7 +282,7 @@ namespace NifOsg class VisController : public SceneUtil::NodeCallback, public SceneUtil::Controller { private: - std::vector mData; + std::shared_ptr> mData; ByteInterpolator mInterpolator; unsigned int mMask{ 0u };