GteCameraRig.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.1 (2016/11/13)
7 
8 #include <GTEnginePCH.h>
11 using namespace gte;
12 
14 {
15 }
16 
18 {
19  Set(nullptr, 0.0f, 0.0f);
20 }
21 
22 CameraRig::CameraRig(std::shared_ptr<Camera> const& camera, float translationSpeed, float rotationSpeed)
23 {
24  Set(camera, translationSpeed, rotationSpeed);
25 }
26 
27 void CameraRig::Set(std::shared_ptr<Camera> const& camera, float translationSpeed, float rotationSpeed)
28 {
29  mCamera = camera;
30  mTranslationSpeed = translationSpeed;
31  mRotationSpeed = rotationSpeed;
33  ClearMotions();
34 }
35 
37 {
38  if (mCamera)
39  {
40  mWorldAxis[0] = mCamera->GetDVector();
41  mWorldAxis[1] = mCamera->GetUVector();
42  mWorldAxis[2] = mCamera->GetRVector();
43  }
44  else
45  {
46  mWorldAxis[0].MakeZero();
47  mWorldAxis[1].MakeZero();
48  mWorldAxis[2].MakeZero();
49  }
50 }
51 
52 bool CameraRig::PushMotion(int trigger)
53 {
54  auto element = mIndirectMap.find(trigger);
55  return (element != mIndirectMap.end() ? SetActive(element->second) : false);
56 }
57 
58 bool CameraRig::PopMotion(int trigger)
59 {
60  auto element = mIndirectMap.find(trigger);
61  return (element != mIndirectMap.end() ? SetInactive(element->second) : false);
62 }
63 
65 {
66  // The current semantics allow for processing all active motions,
67  // which was the semantics in Wild Magic 5. For example, if you
68  // move the camera with the up-arrow (forward motion) and with
69  // the right-arrow (turn-right motion), both will occur during the
70  // idle loop.
71  if (mNumActiveMotions > 0)
72  {
73  for (int i = 0; i < mNumActiveMotions; ++i)
74  {
75  (this->*mActiveMotions[i])();
76  }
77  return true;
78  }
79  return false;
80 
81 #if 0
82  // If you prefer the previous semantics where only one key press is
83  // processed at a time, use this previous version of the code.
84  if (mMotion)
85  {
86  (this->*mMotion)();
87  return true;
88  }
89  return false;
90 #endif
91 }
92 
94 {
95  mMotion = nullptr;
96  mIndirectMap.clear();
98  std::fill(mActiveMotions.begin(), mActiveMotions.end(), nullptr);
99 }
100 
102 {
103  if (mCamera)
104  {
105  mCamera->SetPosition(mCamera->GetPosition() + mTranslationSpeed * mWorldAxis[0]);
106  }
107 }
108 
110 {
111  if (mCamera)
112  {
113  mCamera->SetPosition(mCamera->GetPosition() - mTranslationSpeed * mWorldAxis[0]);
114  }
115 }
116 
118 {
119  if (mCamera)
120  {
121  mCamera->SetPosition(mCamera->GetPosition() + mTranslationSpeed*mWorldAxis[1]);
122  }
123 }
124 
126 {
127  if (mCamera)
128  {
129  mCamera->SetPosition(mCamera->GetPosition() - mTranslationSpeed*mWorldAxis[1]);
130  }
131 }
132 
134 {
135  if (mCamera)
136  {
137  mCamera->SetPosition(mCamera->GetPosition() + mTranslationSpeed*mWorldAxis[2]);
138  }
139 }
140 
142 {
143  if (mCamera)
144  {
145  mCamera->SetPosition(mCamera->GetPosition() - mTranslationSpeed*mWorldAxis[2]);
146  }
147 }
148 
150 {
151  if (mCamera)
152  {
153  Matrix4x4<float> incremental = Rotation<4, float>(
155 
156 #if defined(GTE_USE_MAT_VEC)
157  mWorldAxis[0] = incremental * mWorldAxis[0];
158  mWorldAxis[2] = incremental * mWorldAxis[2];
159  mCamera->SetAxes(
160  incremental * mCamera->GetDVector(),
161  incremental * mCamera->GetUVector(),
162  incremental * mCamera->GetRVector());
163 #else
164  mWorldAxis[0] = mWorldAxis[0] * incremental;
165  mWorldAxis[2] = mWorldAxis[2] * incremental;
166  mCamera->SetAxes(
167  mCamera->GetDVector() * incremental,
168  mCamera->GetUVector() * incremental,
169  mCamera->GetRVector() * incremental);
170 #endif
171  }
172 }
173 
175 {
176  if (mCamera)
177  {
178  Matrix4x4<float> incremental = Rotation<4, float>(
180 
181 #if defined(GTE_USE_MAT_VEC)
182  mWorldAxis[0] = incremental * mWorldAxis[0];
183  mWorldAxis[2] = incremental * mWorldAxis[2];
184  mCamera->SetAxes(
185  incremental*mCamera->GetDVector(),
186  incremental*mCamera->GetUVector(),
187  incremental*mCamera->GetRVector());
188 #else
189  mWorldAxis[0] = mWorldAxis[0] * incremental;
190  mWorldAxis[2] = mWorldAxis[2] * incremental;
191  mCamera->SetAxes(
192  mCamera->GetDVector() * incremental,
193  mCamera->GetUVector() * incremental,
194  mCamera->GetRVector() * incremental);
195 #endif
196  }
197 }
198 
200 {
201  if (mCamera)
202  {
203  Matrix4x4<float> incremental = Rotation<4, float>(
205 
206 #if defined(GTE_USE_MAT_VEC)
207  mCamera->SetAxes(
208  incremental * mCamera->GetDVector(),
209  incremental * mCamera->GetUVector(),
210  incremental * mCamera->GetRVector());
211 #else
212  mCamera->SetAxes(
213  mCamera->GetDVector() * incremental,
214  mCamera->GetUVector() * incremental,
215  mCamera->GetRVector() * incremental);
216 #endif
217  }
218 }
219 
221 {
222  if (mCamera)
223  {
224  Matrix4x4<float> incremental = Rotation<4, float>(
226 
227 #if defined(GTE_USE_MAT_VEC)
228  mCamera->SetAxes(
229  incremental * mCamera->GetDVector(),
230  incremental * mCamera->GetUVector(),
231  incremental * mCamera->GetRVector());
232 #else
233  mCamera->SetAxes(
234  mCamera->GetDVector() * incremental,
235  mCamera->GetUVector() * incremental,
236  mCamera->GetRVector() * incremental);
237 #endif
238  }
239 }
240 
242 {
243  if (mCamera)
244  {
245  Matrix4x4<float> incremental = Rotation<4, float>(
247 
248 #if defined(GTE_USE_MAT_VEC)
249  mCamera->SetAxes(
250  incremental * mCamera->GetDVector(),
251  incremental * mCamera->GetUVector(),
252  incremental * mCamera->GetRVector());
253 #else
254  mCamera->SetAxes(
255  mCamera->GetDVector() * incremental,
256  mCamera->GetUVector() * incremental,
257  mCamera->GetRVector() * incremental);
258 #endif
259  }
260 }
261 
263 {
264  if (mCamera)
265  {
266  Matrix4x4<float> incremental = Rotation<4, float>(
268 
269 #if defined(GTE_USE_MAT_VEC)
270  mCamera->SetAxes(
271  incremental * mCamera->GetDVector(),
272  incremental * mCamera->GetUVector(),
273  incremental * mCamera->GetRVector());
274 #else
275  mCamera->SetAxes(
276  mCamera->GetDVector() * incremental,
277  mCamera->GetUVector() * incremental,
278  mCamera->GetRVector() * incremental);
279 #endif
280  }
281 }
282 
283 void CameraRig::Register(int trigger, MoveFunction function)
284 {
285  if (trigger >= 0)
286  {
287  auto element = mIndirectMap.find(trigger);
288  if (element == mIndirectMap.end())
289  {
290  mIndirectMap.insert(std::make_pair(trigger, function));
291  }
292  }
293  else
294  {
295  for (auto element : mIndirectMap)
296  {
297  if (element.second == function)
298  {
299  mIndirectMap.erase(trigger);
300  return;
301  }
302  }
303  }
304 }
305 
307 {
308  for (int i = 0; i < mNumActiveMotions; ++i)
309  {
310  if (mActiveMotions[i] == function)
311  {
312  return false;
313  }
314  }
315 
316  if (mNumActiveMotions < MAX_ACTIVE_MOTIONS)
317  {
318  mMotion = function;
319  mActiveMotions[mNumActiveMotions] = function;
321  return true;
322  }
323 
324  return false;
325 }
326 
328 {
329  for (int i = 0; i < mNumActiveMotions; ++i)
330  {
331  if (mActiveMotions[i] == function)
332  {
333  for (int j0 = i, j1 = j0 + 1; j1 < mNumActiveMotions; j0 = j1++)
334  {
335  mActiveMotions[j0] = mActiveMotions[j1];
336  }
339  if (mNumActiveMotions > 0)
340  {
341  mMotion = mActiveMotions[mNumActiveMotions - 1];
342  }
343  else
344  {
345  mMotion = nullptr;
346  }
347  return true;
348  }
349  }
350  return false;
351 }
bool PopMotion(int trigger)
void ComputeWorldAxes()
virtual void TurnRight()
MoveFunction mMotion
Definition: GteCameraRig.h:117
bool PushMotion(int trigger)
bool SetInactive(MoveFunction function)
virtual void RollClockwise()
virtual void RollCounterclockwise()
virtual void MoveRight()
virtual void LookUp()
virtual void TurnLeft()
void Set(std::shared_ptr< Camera > const &camera, float translationSpeed, float rotationSpeed)
virtual void MoveUp()
virtual void MoveForward()
void Register(int trigger, MoveFunction function)
virtual ~CameraRig()
virtual void MoveBackward()
virtual void LookDown()
std::array< MoveFunction, MAX_ACTIVE_MOTIONS > mActiveMotions
Definition: GteCameraRig.h:120
float mRotationSpeed
Definition: GteCameraRig.h:111
GLfloat f
Definition: glcorearb.h:1921
bool SetActive(MoveFunction function)
void(CameraRig::* MoveFunction)(void)
Definition: GteCameraRig.h:104
Vector4< float > mWorldAxis[3]
Definition: GteCameraRig.h:114
std::shared_ptr< Camera > mCamera
Definition: GteCameraRig.h:110
virtual void MoveLeft()
virtual void MoveDown()
float mTranslationSpeed
Definition: GteCameraRig.h:111
std::map< int, MoveFunction > mIndirectMap
Definition: GteCameraRig.h:118


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:59:59