OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
controller.hpp
Go to the documentation of this file.
1 #ifndef COMPONENTS_NIFOSG_CONTROLLER_H
2 #define COMPONENTS_NIFOSG_CONTROLLER_H
3 
8 
11 
12 #include <boost/shared_ptr.hpp>
13 
14 #include <set> //UVController
15 
16 // FlipController
17 #include <osg/Texture2D>
18 #include <osg/ref_ptr>
19 
20 #include <osg/StateSet>
21 #include <osg/NodeCallback>
22 #include <osg/Drawable>
23 
24 
25 namespace osg
26 {
27  class Node;
28  class StateSet;
29 }
30 
31 namespace osgParticle
32 {
33  class Emitter;
34 }
35 
36 namespace osgAnimation
37 {
38  class MorphGeometry;
39 }
40 
41 namespace NifOsg
42 {
43 
44  // interpolation of keyframes
45  template <typename MapT, typename InterpolationFunc>
47  {
48  public:
49  typedef typename MapT::ValueType ValueT;
50 
52  : mDefaultVal(ValueT())
53  {
54  }
55 
56  ValueInterpolator(boost::shared_ptr<const MapT> keys, ValueT defaultVal = ValueT())
57  : mKeys(keys)
58  , mDefaultVal(defaultVal)
59  {
60  if (keys)
61  {
62  mLastLowKey = mKeys->mKeys.end();
63  mLastHighKey = mKeys->mKeys.end();
64  }
65  }
66 
67  ValueT interpKey(float time) const
68  {
69  if (empty())
70  return mDefaultVal;
71 
72  const typename MapT::MapType & keys = mKeys->mKeys;
73 
74  if(time <= keys.begin()->first)
75  return keys.begin()->second.mValue;
76 
77  // retrieve the current position in the map, optimized for the most common case
78  // where time moves linearly along the keyframe track
79  typename MapT::MapType::const_iterator it = mLastHighKey;
80  if (mLastHighKey != keys.end())
81  {
82  if (time > mLastHighKey->first)
83  {
84  // try if we're there by incrementing one
85  ++mLastLowKey;
86  ++mLastHighKey;
87  it = mLastHighKey;
88  }
89  if (mLastHighKey == keys.end() || (time < mLastLowKey->first || time > mLastHighKey->first))
90  it = keys.lower_bound(time); // still not there, reorient by performing lower_bound check on the whole map
91  }
92  else
93  it = keys.lower_bound(time);
94 
95  // now do the actual interpolation
96  if (it != keys.end())
97  {
98  float aTime = it->first;
99  const typename MapT::KeyType* aKey = &it->second;
100 
101  // cache for next time
102  mLastHighKey = it;
103 
104  assert (it != keys.begin()); // Shouldn't happen, was checked at beginning of this function
105 
106  typename MapT::MapType::const_iterator last = --it;
107  mLastLowKey = last;
108  float aLastTime = last->first;
109  const typename MapT::KeyType* aLastKey = &last->second;
110 
111  float a = (time - aLastTime) / (aTime - aLastTime);
112 
113  return InterpolationFunc()(aLastKey->mValue, aKey->mValue, a);
114  }
115  else
116  return keys.rbegin()->second.mValue;
117  }
118 
119  bool empty() const
120  {
121  return !mKeys || mKeys->mKeys.empty();
122  }
123 
124  private:
125  mutable typename MapT::MapType::const_iterator mLastLowKey;
126  mutable typename MapT::MapType::const_iterator mLastHighKey;
127 
128  boost::shared_ptr<const MapT> mKeys;
129 
131  };
132 
133  struct LerpFunc
134  {
135  template <typename ValueType>
136  inline ValueType operator()(const ValueType& a, const ValueType& b, float fraction)
137  {
138  return a + ((b - a) * fraction);
139  }
140  };
141 
143  {
144  inline osg::Quat operator()(const osg::Quat& a, const osg::Quat& b, float fraction)
145  {
146  osg::Quat result;
147  result.slerp(fraction, a, b);
148  return result;
149  }
150  };
151 
155 
157  {
158  private:
159  float mFrequency;
160  float mPhase;
161  float mStartTime;
162  float mStopTime;
164  {
165  Cycle = 0,
166  Reverse = 1,
168  };
170 
171  public:
172  ControllerFunction(const Nif::Controller *ctrl);
173 
174  float calculate(float value) const;
175 
176  virtual float getMaximum() const;
177  };
178 
181  {
182  public:
185  GeomMorpherController(const GeomMorpherController& copy, const osg::CopyOp& copyop);
186 
187  META_Object(NifOsg, GeomMorpherController)
188 
189  virtual void update(osg::NodeVisitor* nv, osg::Drawable* drawable);
190 
191  private:
192  std::vector<FloatInterpolator> mKeyFrames;
193  };
194 
195  class KeyframeController : public osg::NodeCallback, public SceneUtil::Controller
196  {
197  public:
200  KeyframeController(const KeyframeController& copy, const osg::CopyOp& copyop);
201 
202  META_Object(NifOsg, KeyframeController)
203 
204  virtual osg::Vec3f getTranslation(float time) const;
205 
206  virtual void operator() (osg::Node*, osg::NodeVisitor*);
207 
208  private:
210 
214 
217 
218  osg::Quat getXYZRotation(float time) const;
219  };
220 
222  {
223  public:
224  UVController();
225  UVController(const UVController&,const osg::CopyOp&);
226  UVController(const Nif::NiUVData *data, std::set<int> textureUnits);
227 
228  META_Object(NifOsg,UVController)
229 
230  virtual void setDefaults(osg::StateSet* stateset);
231  virtual void apply(osg::StateSet *stateset, osg::NodeVisitor *nv);
232 
233  private:
238  std::set<int> mTextureUnits;
239  };
240 
241  class VisController : public osg::NodeCallback, public SceneUtil::Controller
242  {
243  private:
244  std::vector<Nif::NiVisData::VisData> mData;
245 
246  bool calculate(float time) const;
247 
248  public:
249  VisController(const Nif::NiVisData *data);
250  VisController();
251  VisController(const VisController& copy, const osg::CopyOp& copyop);
252 
253  META_Object(NifOsg, VisController)
254 
255  virtual void operator() (osg::Node* node, osg::NodeVisitor* nv);
256  };
257 
258  class AlphaController : public SceneUtil::StateSetUpdater, public SceneUtil::Controller
259  {
260  private:
262 
263  public:
264  AlphaController(const Nif::NiFloatData *data);
265  AlphaController();
266  AlphaController(const AlphaController& copy, const osg::CopyOp& copyop);
267 
268  virtual void setDefaults(osg::StateSet* stateset);
269 
270  virtual void apply(osg::StateSet* stateset, osg::NodeVisitor* nv);
271 
272  META_Object(NifOsg, AlphaController)
273  };
274 
276  {
277  private:
279 
280  public:
283  MaterialColorController(const MaterialColorController& copy, const osg::CopyOp& copyop);
284 
285  META_Object(NifOsg, MaterialColorController)
286 
287  virtual void setDefaults(osg::StateSet* stateset);
288 
289  virtual void apply(osg::StateSet* stateset, osg::NodeVisitor* nv);
290  };
291 
292  class FlipController : public SceneUtil::StateSetUpdater, public SceneUtil::Controller
293  {
294  private:
295  int mTexSlot;
296  float mDelta;
297  std::vector<osg::ref_ptr<osg::Texture2D> > mTextures;
298 
299  public:
300  FlipController(const Nif::NiFlipController* ctrl, std::vector<osg::ref_ptr<osg::Texture2D> > textures);
301  FlipController(int texSlot, float delta, std::vector<osg::ref_ptr<osg::Texture2D> > textures);
302  FlipController();
303  FlipController(const FlipController& copy, const osg::CopyOp& copyop);
304 
306 
307  std::vector<osg::ref_ptr<osg::Texture2D> >& getTextures() { return mTextures; }
308 
309  virtual void apply(osg::StateSet *stateset, osg::NodeVisitor *nv);
310  };
311 
312  class ParticleSystemController : public osg::NodeCallback, public SceneUtil::Controller
313  {
314  public:
317  ParticleSystemController(const ParticleSystemController& copy, const osg::CopyOp& copyop);
318 
319  META_Object(NifOsg, ParticleSystemController)
320 
321  virtual void operator() (osg::Node* node, osg::NodeVisitor* nv);
322 
323  private:
324  float mEmitStart;
325  float mEmitStop;
326  };
327 
328 }
329 
330 #endif
ExtrapolationMode mExtrapolationMode
Definition: controller.hpp:169
FloatInterpolator mYRotations
Definition: controller.hpp:212
GeomMorpherController()
Definition: controller.cpp:172
META_Object(NifOsg, FlipController) std
Definition: controller.hpp:305
boost::shared_ptr< const MapT > mKeys
Definition: controller.hpp:128
float mPhase
Definition: controller.hpp:160
Definition: controller.hpp:312
ValueInterpolator()
Definition: controller.hpp:51
ControllerFunction(const Nif::Controller *ctrl)
Definition: controller.cpp:20
Implements efficient per-frame updating of StateSets.
Definition: statesetupdater.hpp:19
Definition: controller.hpp:167
Definition: controller.hpp:147
Definition: controller.hpp:32
float mFrequency
Definition: controller.hpp:159
MapT::ValueType ValueT
Definition: controller.hpp:49
ValueInterpolator< Nif::FloatKeyMap, LerpFunc > FloatInterpolator
Definition: controller.hpp:153
bool empty() const
Definition: controller.hpp:119
float mDelta
Definition: controller.hpp:296
Definition: data.hpp:201
Definition: controller.hpp:133
Definition: controller.hpp:156
Definition: controller.hpp:292
Definition: controller.hpp:241
MapT::MapType::const_iterator mLastLowKey
Definition: controller.hpp:125
std::vector< Nif::NiVisData::VisData > mData
Definition: controller.hpp:244
FloatInterpolator mUScale
Definition: controller.hpp:236
Vec3Interpolator mData
Definition: controller.hpp:278
Controller()
Definition: controller.cpp:12
ValueT mDefaultVal
Definition: controller.hpp:130
Definition: controller.hpp:36
FloatInterpolator mScales
Definition: controller.hpp:216
Definition: controller.hpp:195
FloatInterpolator mData
Definition: controller.hpp:261
std::vector< FloatInterpolator > mKeyFrames
Definition: controller.hpp:187
Definition: controller.hpp:46
FloatInterpolator mXRotations
Definition: controller.hpp:211
Definition: controller.hpp:258
Definition: controller.hpp:221
Definition: base.hpp:26
osg::Quat operator()(const osg::Quat &a, const osg::Quat &b, float fraction)
Definition: controller.hpp:144
ValueInterpolator(boost::shared_ptr< const MapT > keys, ValueT defaultVal=ValueT())
Definition: controller.hpp:56
std::vector< osg::ref_ptr< osg::Texture2D > > mTextures
Definition: controller.hpp:297
Definition: data.hpp:89
Definition: data.hpp:143
FloatInterpolator mVScale
Definition: controller.hpp:237
Definition: data.hpp:97
int mTexSlot
Definition: controller.hpp:295
ExtrapolationMode
Definition: controller.hpp:163
Definition: controller.hpp:142
Vec3Interpolator mTranslations
Definition: controller.hpp:215
Definition: controller.hpp:166
ValueInterpolator< Nif::Vector3KeyMap, LerpFunc > Vec3Interpolator
Definition: controller.hpp:154
FloatInterpolator mZRotations
Definition: controller.hpp:213
ValueType operator()(const ValueType &a, const ValueType &b, float fraction)
Definition: controller.hpp:136
FloatInterpolator mUTrans
Definition: controller.hpp:234
Definition: data.hpp:81
Definition: controller.hpp:26
float mStartTime
Definition: controller.hpp:161
ValueT interpKey(float time) const
Definition: controller.hpp:67
QuaternionInterpolator mRotations
Definition: controller.hpp:209
FloatInterpolator mVTrans
Definition: controller.hpp:235
float mStopTime
Definition: controller.hpp:162
virtual float getMaximum() const
Definition: controller.cpp:66
std::set< int > mTextureUnits
Definition: controller.hpp:238
Must be set on an osgAnimation::MorphGeometry.
Definition: controller.hpp:180
Definition: controller.hpp:165
float calculate(float value) const
Definition: controller.cpp:29
Definition: data.hpp:189
Definition: controller.hpp:275
MapT::MapType::const_iterator mLastHighKey
Definition: controller.hpp:126
ValueInterpolator< Nif::QuaternionKeyMap, QuaternionSlerpFunc > QuaternionInterpolator
Definition: controller.hpp:152