OpenMW
components/nifogre/controller.hpp
Go to the documentation of this file.
00001 #ifndef COMPONENTS_NIFOGRE_CONTROLLER_H
00002 #define COMPONENTS_NIFOGRE_CONTROLLER_H
00003 
00004 #include <components/nif/niffile.hpp>
00005 #include <OgreController.h>
00006 
00007 namespace NifOgre
00008 {
00009 
00010     class ValueInterpolator
00011     {
00012     protected:
00013         float interpKey(const Nif::FloatKeyList::VecType &keys, float time, float def=0.f) const
00014         {
00015             if (keys.size() == 0)
00016                 return def;
00017 
00018             if(time <= keys.front().mTime)
00019                 return keys.front().mValue;
00020 
00021             Nif::FloatKeyList::VecType::const_iterator iter(keys.begin()+1);
00022             for(;iter != keys.end();iter++)
00023             {
00024                 if(iter->mTime < time)
00025                     continue;
00026 
00027                 Nif::FloatKeyList::VecType::const_iterator last(iter-1);
00028                 float a = (time-last->mTime) / (iter->mTime-last->mTime);
00029                 return last->mValue + ((iter->mValue - last->mValue)*a);
00030             }
00031             return keys.back().mValue;
00032         }
00033 
00034         Ogre::Vector3 interpKey(const Nif::Vector3KeyList::VecType &keys, float time) const
00035         {
00036             if(time <= keys.front().mTime)
00037                 return keys.front().mValue;
00038 
00039             Nif::Vector3KeyList::VecType::const_iterator iter(keys.begin()+1);
00040             for(;iter != keys.end();iter++)
00041             {
00042                 if(iter->mTime < time)
00043                     continue;
00044 
00045                 Nif::Vector3KeyList::VecType::const_iterator last(iter-1);
00046                 float a = (time-last->mTime) / (iter->mTime-last->mTime);
00047                 return last->mValue + ((iter->mValue - last->mValue)*a);
00048             }
00049             return keys.back().mValue;
00050         }
00051     };
00052 
00053     // FIXME: Should not be here.
00054     class DefaultFunction : public Ogre::ControllerFunction<Ogre::Real>
00055     {
00056     private:
00057         float mFrequency;
00058         float mPhase;
00059         float mStartTime;
00060     public:
00061         float mStopTime;
00062 
00063     public:
00064         DefaultFunction(const Nif::Controller *ctrl, bool deltaInput)
00065             : Ogre::ControllerFunction<Ogre::Real>(deltaInput)
00066             , mFrequency(ctrl->frequency)
00067             , mPhase(ctrl->phase)
00068             , mStartTime(ctrl->timeStart)
00069             , mStopTime(ctrl->timeStop)
00070         {
00071             if(mDeltaInput)
00072                 mDeltaCount = mPhase;
00073         }
00074 
00075         virtual Ogre::Real calculate(Ogre::Real value)
00076         {
00077             if(mDeltaInput)
00078             {
00079                 mDeltaCount += value*mFrequency;
00080                 if(mDeltaCount < mStartTime)
00081                     mDeltaCount = mStopTime - std::fmod(mStartTime - mDeltaCount,
00082                                                         mStopTime - mStartTime);
00083                 mDeltaCount = std::fmod(mDeltaCount - mStartTime,
00084                                         mStopTime - mStartTime) + mStartTime;
00085                 return mDeltaCount;
00086             }
00087 
00088             value = std::min(mStopTime, std::max(mStartTime, value+mPhase));
00089             return value;
00090         }
00091     };
00092 
00093 }
00094 
00095 #endif