-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathCameraMan.cpp
416 lines (356 loc) · 10.9 KB
/
CameraMan.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include <iostream>
#include <raisim/math.hpp>
#include "raisim/CameraMan.hpp"
#include "OgreSceneManager.h"
namespace raisim {
CameraMan::CameraMan(Ogre::SceneNode *cam)
: mYawSpace(Ogre::Node::TS_PARENT)
, mCamera(0)
, mStyle(CS_MANUAL)
, mTarget(0)
, mOrbiting(true)
, mMoving(false)
, mTopSpeed(10.0f)
, mVelocity(Ogre::Vector3::ZERO)
, mGoingForward(false)
, mGoingBack(false)
, mGoingLeft(false)
, mGoingRight(false)
, mGoingUp(false)
, mGoingDown(false)
, mFastMove(false)
, mOffset(0, 0, 0)
{
setCamera(cam);
setStyle(CS_FREELOOK);
}
void CameraMan::setCamera(Ogre::SceneNode *cam)
{
mCamera = cam;
}
void CameraMan::setTarget(Ogre::SceneNode *target)
{
if (target == mTarget)
return;
mTarget = target;
}
void CameraMan::setYawPitchDist(Ogre::Radian yaw, Ogre::Radian pitch, Ogre::Real dist, bool trackObjectsYaw)
{
OgreAssert(mTarget, "no target set");
yaw_ = yaw;
pitch_ = pitch;
dist_ = dist;
mOffset = Ogre::Vector3::ZERO;
mCamera->setPosition(mTarget->_getDerivedPosition());
mCamera->setOrientation(Ogre::Quaternion{1.,0.,0.,0.});
if(trackObjectsYaw)
mCamera->yaw(mTarget->_getDerivedOrientation().getRoll());
mCamera->yaw(-yaw_);
mCamera->pitch(-pitch_);
mCamera->translate(Ogre::Vector3(0, 0, dist_), Ogre::Node::TS_LOCAL);
}
void CameraMan::setStyle(CameraStyle style)
{
if (mStyle != CS_ORBIT && style == CS_ORBIT)
{
setTarget(mTarget ? mTarget : mCamera->getCreator()->getRootSceneNode());
auto targetPos = mTarget->getPosition();
auto cameraPos = mCamera->getPosition();
auto diff = targetPos - cameraPos;
yaw_ = mCamera->getOrientation().getYaw();
pitch_ = mCamera->getOrientation().getPitch();
dist_ = diff.normalise();
setYawPitchDist(-yaw_, -pitch_, dist_);
manualStop();
// try to replicate the camera configuration
// Ogre::Real dist = getDistToTarget();
// const Ogre::Quaternion& q = mCamera->getOrientation();
// setYawPitchDist(q.getYaw(), q.getPitch(), dist == 0 ? 150 : dist); // enforce some distance
}
else if (mStyle != CS_FREELOOK && style == CS_FREELOOK)
{
mCamera->setFixedYawAxis(true, Ogre::Vector3::UNIT_Z); // also fix axis with lookAt calls
}
else if (mStyle != CS_MANUAL && style == CS_MANUAL)
{
manualStop();
}
mStyle = style;
mCamera->setAutoTracking(true);
}
void CameraMan::manualStop()
{
if (mStyle == CS_FREELOOK)
{
mGoingForward = false;
mGoingBack = false;
mGoingLeft = false;
mGoingRight = false;
mGoingUp = false;
mGoingDown = false;
mVelocity = Ogre::Vector3::ZERO;
}
}
void CameraMan::frameRendered(const Ogre::FrameEvent &evt)
{
update();
if (mStyle == CS_FREELOOK)
#include "OgreBitesPrerequisites.h"
#include "OgreCamera.h"
#include "OgreSceneNode.h"
#include "OgreFrameListener.h"
#include "OgreInput.h"
/** \addtogroup Optional
* @{
*/
/** \addtogroup Bites
* @{
*/
{
enum CameraStyle /// enumerator values for different styles of camera movement
{
CS_FREELOOK,
CS_ORBIT,
CS_MANUAL
};
/**
Utility class for controlling the camera in samples.
*/
class _OgreBitesExport CameraMan : public InputListener
{
public:
CameraMan(Ogre::SceneNode* cam);
/**
Swaps the camera on our camera man for another camera.
*/
void setCamera(Ogre::SceneNode* cam);
Ogre::SceneNode* getCamera()
{
return mCamera;
}
/**
Sets the target we will revolve around. Only applies for orbit style.
*/
virtual void setTarget(Ogre::SceneNode* target);
Ogre::SceneNode* getTarget()
{
return mTarget;
}
/**
Sets the spatial offset from the target. Only applies for orbit style.
*/
void setYawPitchDist(Ogre::Radian yaw, Ogre::Radian pitch, Ogre::Real dist, bool trackObjectsYaw);
/**
Sets the camera's top speed. Only applies for free-look style.
*/
void setTopSpeed(Ogre::Real topSpeed)
{
mTopSpeed = topSpeed;
}
Ogre::Real getTopSpeed()
{
return mTopSpeed;
}
/**
Sets the movement style of our camera man.
*/
virtual void setStyle(CameraStyle style);
CameraStyle getStyle()
{
return mStyle;
}
/**
Manually stops the camera when in free-look mode.
*/
void manualStop();
void frameRendered(const Ogre::FrameEvent& evt);
/**
Processes key presses for free-look style movement.
*/
bool keyPressed(const KeyboardEvent& evt);
/**
Processes key releases for free-look style movement.
*/
bool keyReleased(const KeyboardEvent& evt);
/**
Processes mouse movement differently for each style.
*/
bool mouseMoved(const MouseMotionEvent& evt);
bool mouseWheelRolled(const MouseWheelEvent& evt);
/**
Processes mouse presses. Only applies for orbit style.
Left button is for orbiting, and right button is for zooming.
*/
bool mousePressed(const MouseButtonEvent& evt);
/**
Processes mouse releases. Only applies for orbit style.
Left button is for orbiting, and right button is for zooming.
*/
bool mouseReleased(const MouseButtonEvent& evt);
/**
* fix the yaw axis to be Vector3::UNIT_Y of the parent node (tabletop mode)
*
* otherwise the yaw axis can change freely
*/
void setFixedYaw(bool fixed)
{
mYawSpace = fixed ? Ogre::Node::TS_PARENT : Ogre::Node::TS_LOCAL;
}
void setPivotOffset(const Ogre::Vector3& offset);
protected:
Ogre::Real getDistToTarget();
Ogre::Node::TransformSpace mYawSpace;
Ogre::SceneNode* mCamera;
CameraStyle mStyle;
Ogre::SceneNode* mTarget;
bool mOrbiting;
bool mMoving;
Ogre::Real mTopSpeed;
Ogre::Vector3 mVelocity;
bool mGoingForward;
bool mGoingBack;
bool mGoingLeft;
bool mGoingRight;
bool mGoingUp;
bool mGoingDown;
bool mFastMove;
Ogre::Vector3 mOffset;
};
}
/** @} */
/** @} */
{
// build our acceleration vector based on keyboard input composite
Ogre::Vector3 accel = Ogre::Vector3::ZERO;
Ogre::Matrix3 axes = mCamera->getLocalAxes();
if (mGoingForward) accel -= axes.GetColumn(2);
if (mGoingBack) accel += axes.GetColumn(2);
if (mGoingRight) accel += axes.GetColumn(0);
if (mGoingLeft) accel -= axes.GetColumn(0);
if (mGoingUp) accel += axes.GetColumn(1);
if (mGoingDown) accel -= axes.GetColumn(1);
// if accelerating, try to reach top speed in a certain time
Ogre::Real topSpeed = mFastMove ? mTopSpeed * 20 : mTopSpeed;
if (accel.squaredLength() != 0)
{
accel.normalise();
mVelocity += accel * topSpeed * evt.timeSinceLastFrame * 10;
}
// if not accelerating, try to stop in a certain time
else mVelocity -= mVelocity * evt.timeSinceLastFrame * 10;
Ogre::Real tooSmall = std::numeric_limits<Ogre::Real>::epsilon();
// keep camera velocity below top speed and above epsilon
if (mVelocity.squaredLength() > topSpeed * topSpeed)
{
mVelocity.normalise();
mVelocity *= topSpeed;
}
else if (mVelocity.squaredLength() < tooSmall * tooSmall)
mVelocity = Ogre::Vector3::ZERO;
if (mVelocity != Ogre::Vector3::ZERO) mCamera->translate(mVelocity * evt.timeSinceLastFrame);
}
}
bool CameraMan::keyPressed(const KeyboardEvent &evt)
{
if (mStyle == CS_FREELOOK)
{
Keycode key = evt.keysym.sym;
if (key == 'w' || key == SDLK_UP) mGoingForward = true;
else if (key == 's' || key == SDLK_DOWN) mGoingBack = true;
else if (key == 'a' || key == SDLK_LEFT) mGoingLeft = true;
else if (key == 'd' || key == SDLK_RIGHT) mGoingRight = true;
else if (key == SDLK_PAGEUP) mGoingUp = true;
else if (key == SDLK_PAGEDOWN) mGoingDown = true;
else if (key == SDLK_LSHIFT) mFastMove = true;
}
return InputListener::keyPressed(evt);
}
bool CameraMan::keyReleased(const KeyboardEvent &evt)
{
if (mStyle == CS_FREELOOK)
{
Keycode key = evt.keysym.sym;
if (key == 'w' || key == SDLK_UP) mGoingForward = false;
else if (key == 's' || key == SDLK_DOWN) mGoingBack = false;
else if (key == 'a' || key == SDLK_LEFT) mGoingLeft = false;
else if (key == 'd' || key == SDLK_RIGHT) mGoingRight = false;
else if (key == SDLK_PAGEUP) mGoingUp = false;
else if (key == SDLK_PAGEDOWN) mGoingDown = false;
else if (key == SDLK_LSHIFT) mFastMove = false;
}
return InputListener::keyReleased(evt);
}
Ogre::Real CameraMan::getDistToTarget()
{
Ogre::Vector3 offset = mCamera->getPosition() - mTarget->_getDerivedPosition() - mOffset;
return offset.length();
}
void CameraMan::update() {
if (mOrbiting && mStyle == CS_ORBIT) // yaw around the target, and pitch locally
{
auto oldRot = mCamera->getOrientation();
mCamera->setPosition(mTarget->_getDerivedPosition());
mCamera->translate(Ogre::Vector3(0, 0, dist_), Ogre::Node::TS_LOCAL);
}
}
void CameraMan::setPivotOffset(const Ogre::Vector3& pivot)
{
Ogre::Real dist = getDistToTarget();
mOffset = pivot;
mCamera->setPosition(mTarget->_getDerivedPosition() + mOffset);
mCamera->translate(Ogre::Vector3(0, 0, dist), Ogre::Node::TS_LOCAL);
}
bool CameraMan::mouseMoved(const MouseMotionEvent &evt, bool midMousePressed)
{
if (mStyle == CS_ORBIT)
{
if (mOrbiting) // yaw around the target, and pitch locally
{
mCamera->setPosition(mTarget->_getDerivedPosition() + mOffset);
yaw_ += Ogre::Degree(evt.xrel * 0.25f);
pitch_ += Ogre::Degree(evt.yrel * 0.25f);
setYawPitchDist(yaw_, pitch_, dist_);
// don't let the camera go over the top or around the bottom of the target
}
}
else if (mStyle == CS_FREELOOK)
{
if(!midMousePressed) {
mCamera->yaw(Ogre::Degree(-evt.xrel * 0.15f), Ogre::Node::TS_PARENT);
mCamera->pitch(Ogre::Degree(-evt.yrel * 0.15f));
} else {
mCamera->translate(Ogre::Vector3(evt.xrel*0.005f*getTopSpeed(), -evt.yrel*0.005f*getTopSpeed(), 0), Ogre::Node::TS_LOCAL);
}
}
return InputListener::mouseMoved(evt);
}
bool CameraMan::mouseWheelRolled(const MouseWheelEvent &evt) {
if (mStyle == CS_ORBIT && evt.y != 0)
{
if(evt.y > 0)
dist_ /= 1.08;
else
dist_ *= 1.08;
setYawPitchDist(yaw_, pitch_, dist_);
}
return InputListener::mouseWheelRolled(evt);
}
bool CameraMan::mousePressed(const MouseButtonEvent &evt)
{
if (mStyle == CS_ORBIT)
{
if (evt.button == BUTTON_LEFT) mOrbiting = true;
else if (evt.button == BUTTON_RIGHT) mMoving = true;
}
return InputListener::mousePressed(evt);
}
bool CameraMan::mouseReleased(const MouseButtonEvent &evt)
{
if (mStyle == CS_ORBIT)
{
if (evt.button == BUTTON_LEFT) mOrbiting = true;
else if (evt.button == BUTTON_RIGHT) mMoving = false;
}
return InputListener::mouseReleased(evt);
}
}