GteKeyframeController.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #include <GTEnginePCH.h>
10 #include <Graphics/GteSpatial.h>
12 using namespace gte;
13 
15 {
16 }
17 
18 KeyframeController::KeyframeController(int numCommonTimes, int numTranslations,
19  int numRotations, int numScales, const Transform& localTransform)
20  :
21  TransformController(localTransform),
22  mNumCommonTimes(0),
24  mNumRotations(0),
25  mNumScales(0),
26  mTLastIndex(0),
27  mRLastIndex(0),
28  mSLastIndex(0),
29  mCLastIndex(0)
30 {
31  if (numCommonTimes > 0)
32  {
33  mNumCommonTimes = numCommonTimes;
35 
36  if (numTranslations > 0)
37  {
38  mNumTranslations = numTranslations;
41  }
42 
43  if (numRotations > 0)
44  {
45  mNumRotations = numRotations;
47  mRotations.resize(mNumRotations);
48  }
49 
50  if (numScales > 0)
51  {
52  mNumScales = numScales;
54  mScales.resize(mNumScales);
55  }
56  }
57  else
58  {
59  if (numTranslations > 0)
60  {
61  mNumTranslations = numTranslations;
64  }
65 
66  if (numRotations > 0)
67  {
68  mNumRotations = numRotations;
70  mRotations.resize(mNumRotations);
71  }
72 
73  if (numScales > 0)
74  {
75  mNumScales = numScales;
76  mScaleTimes.resize(mNumScales);
77  mScales.resize(mNumScales);
78  }
79  }
80 }
81 
82 bool KeyframeController::Update(double applicationTime)
83 {
84  if (!Controller::Update(applicationTime))
85  {
86  return false;
87  }
88 
89  float ctrlTime = static_cast<float>(GetControlTime(applicationTime));
90  float normTime = 0.0f;
91  int i0 = 0, i1 = 0;
92  Vector4<float> trn;
93  Matrix4x4<float> rot;
94  float scale;
95 
96  // The logic here checks for equal-time arrays to minimize the number of
97  // times GetKeyInfo is called.
98  if (mNumCommonTimes > 0)
99  {
101  normTime, i0, i1);
102 
103  if (mNumTranslations > 0)
104  {
105  trn = GetTranslate(normTime, i0, i1);
107  }
108 
109  if (mNumRotations > 0)
110  {
111  rot = GetRotate(normTime, i0, i1);
113  }
114 
115  if (mNumScales > 0)
116  {
117  scale = GetScale(normTime, i0, i1);
119  }
120  }
121  else
122  {
123  if (mNumTranslations > 0)
124  {
126  mTLastIndex, normTime, i0, i1);
127  trn = GetTranslate(normTime, i0, i1);
129  }
130 
131  if (mNumRotations > 0)
132  {
134  normTime, i0, i1);
135  rot = GetRotate(normTime, i0, i1);
137  }
138 
139  if (mNumScales > 0)
140  {
141  GetKeyInfo(ctrlTime, mNumScales, mScaleTimes.data(), mSLastIndex,
142  normTime, i0, i1);
143  scale = GetScale(normTime, i0, i1);
145  }
146  }
147 
148  Spatial* spatial = reinterpret_cast<Spatial*>(mObject);
149  spatial->localTransform = mLocalTransform;
150  return true;
151 }
152 
153 void KeyframeController::GetKeyInfo(float ctrlTime, int numTimes, float* times,
154  int& lastIndex, float& normTime, int& i0, int& i1)
155 {
156  if (ctrlTime <= times[0])
157  {
158  normTime = 0.0f;
159  lastIndex = 0;
160  i0 = 0;
161  i1 = 0;
162  return;
163  }
164 
165  if (ctrlTime >= times[numTimes - 1])
166  {
167  normTime = 0.0f;
168  lastIndex = numTimes - 1;
169  i0 = lastIndex;
170  i1 = lastIndex;
171  return;
172  }
173 
174  int nextIndex;
175  if (ctrlTime > times[lastIndex])
176  {
177  nextIndex = lastIndex + 1;
178  while (ctrlTime >= times[nextIndex])
179  {
180  lastIndex = nextIndex;
181  ++nextIndex;
182  }
183 
184  i0 = lastIndex;
185  i1 = nextIndex;
186  normTime = (ctrlTime - times[i0]) / (times[i1] - times[i0]);
187  }
188  else if (ctrlTime < times[lastIndex])
189  {
190  nextIndex = lastIndex - 1;
191  while (ctrlTime <= times[nextIndex])
192  {
193  lastIndex = nextIndex;
194  --nextIndex;
195  }
196 
197  i0 = nextIndex;
198  i1 = lastIndex;
199  normTime = (ctrlTime - times[i0]) / (times[i1] - times[i0]);
200  }
201  else
202  {
203  normTime = 0.0f;
204  i0 = lastIndex;
205  i1 = lastIndex;
206  }
207 }
208 
209 Vector4<float> KeyframeController::GetTranslate(float normTime, int i0, int i1)
210 {
211  Vector4<float> trn = mTranslations[i0] + normTime * (mTranslations[i1] - mTranslations[i0]);
212  return trn;
213 }
214 
215 Matrix4x4<float> KeyframeController::GetRotate(float normTime, int i0, int i1)
216 {
217  Quaternion<float> q = Slerp(normTime, mRotations[i0], mRotations[i1]);
219  return rot;
220 }
221 
222 float KeyframeController::GetScale(float normTime, int i0, int i1)
223 {
224  return mScales[i0] + normTime * (mScales[i1] - mScales[i0]);
225 }
std::vector< float > mTranslationTimes
ControlledObject * mObject
Definition: GteController.h:71
void SetUniformScale(float scale)
static void GetKeyInfo(float ctrlTime, int numTimes, float *times, int &lastIndex, float &normTime, int &i0, int &i1)
GLenum GLenum GLenum GLenum GLenum scale
Definition: glext.h:9914
Quaternion< Real > Slerp(Real t, Quaternion< Real > const &q0, Quaternion< Real > const &q1)
std::vector< float > mCommonTimes
KeyframeController(int numCommonTimes, int numTranslations, int numRotations, int numScales, Transform const &localTransform)
std::vector< float > mScales
double GetControlTime(double applicationTime)
virtual bool Update(double applicationTime)
virtual bool Update(double applicationTime)
Matrix4x4< float > GetRotate(float normTime, int i0, int i1)
std::vector< Vector4< float > > mTranslations
std::vector< float > mScaleTimes
std::vector< Quaternion< float > > mRotations
GLdouble GLdouble GLdouble GLdouble q
Definition: glext.h:255
Vector4< float > GetTranslate(float normTime, int i0, int i1)
void SetRotation(Matrix4x4< float > const &rotate)
void SetTranslation(float x0, float x1, float x2)
float GetScale(float normTime, int i0, int i1)
std::vector< float > mRotationTimes
Transform localTransform
Definition: GteSpatial.h:49


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:00