OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
nifkey.hpp
Go to the documentation of this file.
1 
3 #ifndef OPENMW_COMPONENTS_NIF_NIFKEY_HPP
4 #define OPENMW_COMPONENTS_NIF_NIFKEY_HPP
5 
6 #include "nifstream.hpp"
7 
8 #include <sstream>
9 #include <map>
10 
11 #include <boost/shared_ptr.hpp>
12 
13 #include "niffile.hpp"
14 
15 namespace Nif
16 {
17 
18 template<typename T>
19 struct KeyT {
20  T mValue;
21 
22  // FIXME: Implement Quadratic and TBC interpolation
23  /*
24  T mForwardValue; // Only for Quadratic interpolation, and never for QuaternionKeyList
25  T mBackwardValue; // Only for Quadratic interpolation, and never for QuaternionKeyList
26  float mTension; // Only for TBC interpolation
27  float mBias; // Only for TBC interpolation
28  float mContinuity; // Only for TBC interpolation
29  */
30 };
35 
36 template<typename T, T (NIFStream::*getValue)()>
37 struct KeyMapT {
38  typedef std::map< float, KeyT<T> > MapType;
39 
40  typedef T ValueType;
41  typedef KeyT<T> KeyType;
42 
43  static const unsigned int sLinearInterpolation = 1;
44  static const unsigned int sQuadraticInterpolation = 2;
45  static const unsigned int sTBCInterpolation = 3;
46  static const unsigned int sXYZInterpolation = 4;
47 
48  unsigned int mInterpolationType;
50 
52 
53  //Read in a KeyGroup (see http://niftools.sourceforge.net/doc/nif/NiKeyframeData.html)
54  void read(NIFStream *nif, bool force=false)
55  {
56  assert(nif);
57 
59 
60  size_t count = nif->getUInt();
61  if(count == 0 && !force)
62  return;
63 
64  mKeys.clear();
65 
66  mInterpolationType = nif->getUInt();
67 
68  KeyT<T> key;
69  NIFStream &nifReference = *nif;
70 
72  {
73  for(size_t i = 0;i < count;i++)
74  {
75  float time = nif->getFloat();
76  readValue(nifReference, key);
77  mKeys[time] = key;
78  }
79  }
81  {
82  for(size_t i = 0;i < count;i++)
83  {
84  float time = nif->getFloat();
85  readQuadratic(nifReference, key);
86  mKeys[time] = key;
87  }
88  }
90  {
91  for(size_t i = 0;i < count;i++)
92  {
93  float time = nif->getFloat();
94  readTBC(nifReference, key);
95  mKeys[time] = key;
96  }
97  }
98  //XYZ keys aren't actually read here.
99  //data.hpp sees that the last type read was sXYZInterpolation and:
100  // Eats a floating point number, then
101  // Re-runs the read function 3 more times.
102  // When it does that it's reading in a bunch of sLinearInterpolation keys, not sXYZInterpolation.
104  {
105  //Don't try to read XYZ keys into the wrong part
106  if ( count != 1 )
107  {
108  std::stringstream error;
109  error << "XYZ_ROTATION_KEY count should always be '1' . Retrieved Value: "
110  << count;
111  nif->file->fail(error.str());
112  }
113  }
114  else if (0 == mInterpolationType)
115  {
116  if (count != 0)
117  nif->file->fail("Interpolation type 0 doesn't work with keys");
118  }
119  else
120  {
121  std::stringstream error;
122  error << "Unhandled interpolation type: " << mInterpolationType;
123  nif->file->fail(error.str());
124  }
125  }
126 
127 private:
128  static void readValue(NIFStream &nif, KeyT<T> &key)
129  {
130  key.mValue = (nif.*getValue)();
131  }
132 
133  template <typename U>
134  static void readQuadratic(NIFStream &nif, KeyT<U> &key)
135  {
136  readValue(nif, key);
137  /*key.mForwardValue = */(nif.*getValue)();
138  /*key.mBackwardValue = */(nif.*getValue)();
139  }
140 
141  static void readQuadratic(NIFStream &nif, KeyT<osg::Quat> &key)
142  {
143  readValue(nif, key);
144  }
145 
146  static void readTBC(NIFStream &nif, KeyT<T> &key)
147  {
148  readValue(nif, key);
149  /*key.mTension = */nif.getFloat();
150  /*key.mBias = */nif.getFloat();
151  /*key.mContinuity = */nif.getFloat();
152  }
153 };
158 
159 typedef boost::shared_ptr<FloatKeyMap> FloatKeyMapPtr;
160 typedef boost::shared_ptr<Vector3KeyMap> Vector3KeyMapPtr;
161 typedef boost::shared_ptr<Vector4KeyMap> Vector4KeyMapPtr;
162 typedef boost::shared_ptr<QuaternionKeyMap> QuaternionKeyMapPtr;
163 
164 } // Namespace
165 #endif //#ifndef OPENMW_COMPONENTS_NIF_NIFKEY_HPP
static void readValue(NIFStream &nif, KeyT< T > &key)
Definition: nifkey.hpp:128
void read(NIFStream *nif, bool force=false)
Definition: nifkey.hpp:54
boost::shared_ptr< FloatKeyMap > FloatKeyMapPtr
Definition: nifkey.hpp:159
T ValueType
Definition: nifkey.hpp:40
static void readQuadratic(NIFStream &nif, KeyT< U > &key)
Definition: nifkey.hpp:134
NIFFile *const file
Definition: nifstream.hpp:38
T mValue
Definition: nifkey.hpp:20
KeyMapT< osg::Vec3f,&NIFStream::getVector3 > Vector3KeyMap
Definition: nifkey.hpp:155
MapType mKeys
Definition: nifkey.hpp:49
std::map< float, KeyT< T > > MapType
Definition: nifkey.hpp:38
unsigned int mInterpolationType
Definition: nifkey.hpp:48
static void readTBC(NIFStream &nif, KeyT< T > &key)
Definition: nifkey.hpp:146
KeyMapT< float,&NIFStream::getFloat > FloatKeyMap
Definition: nifkey.hpp:154
KeyT< osg::Vec3f > Vector3Key
Definition: nifkey.hpp:32
boost::shared_ptr< Vector3KeyMap > Vector3KeyMapPtr
Definition: nifkey.hpp:160
static const unsigned int sLinearInterpolation
Definition: nifkey.hpp:43
void fail(const std::string &msg) const
Used if file parsing fails.
Definition: niffile.hpp:51
static const unsigned int sXYZInterpolation
Definition: nifkey.hpp:46
KeyT< T > KeyType
Definition: nifkey.hpp:41
KeyMapT()
Definition: nifkey.hpp:51
Definition: nifkey.hpp:19
boost::shared_ptr< Vector4KeyMap > Vector4KeyMapPtr
Definition: nifkey.hpp:161
KeyT< osg::Quat > QuaternionKey
Definition: nifkey.hpp:34
boost::shared_ptr< QuaternionKeyMap > QuaternionKeyMapPtr
Definition: nifkey.hpp:162
KeyMapT< osg::Vec4f,&NIFStream::getVector4 > Vector4KeyMap
Definition: nifkey.hpp:156
float getFloat()
Definition: nifstream.hpp:49
KeyT< float > FloatKey
Definition: nifkey.hpp:31
Definition: nifstream.hpp:26
unsigned int getUInt()
Definition: nifstream.hpp:48
KeyMapT< osg::Quat,&NIFStream::getQuaternion > QuaternionKeyMap
Definition: nifkey.hpp:157
Definition: nifkey.hpp:37
static const unsigned int sQuadraticInterpolation
Definition: nifkey.hpp:44
KeyT< osg::Vec4f > Vector4Key
Definition: nifkey.hpp:33
static const unsigned int sTBCInterpolation
Definition: nifkey.hpp:45
static void readQuadratic(NIFStream &nif, KeyT< osg::Quat > &key)
Definition: nifkey.hpp:141