forked from Devsh-Graphics-Programming/Nabla
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSceneNodeAnimatorCameraMaya.cpp
More file actions
319 lines (263 loc) · 8.02 KB
/
CSceneNodeAnimatorCameraMaya.cpp
File metadata and controls
319 lines (263 loc) · 8.02 KB
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
// Copyright (C) 2019 - DevSH Graphics Programming Sp. z O.O.
// This file is part of the "Nabla Engine" and was originally part of the "Irrlicht Engine"
// For conditions of distribution and use, see copyright notice in nabla.h
// See the original file in irrlicht source for authors
#include "CSceneNodeAnimatorCameraMaya.h"
#include "ICursorControl.h"
#include "ICameraSceneNode.h"
#include "SViewFrustum.h"
#include "ISceneManager.h"
namespace nbl
{
namespace scene
{
//! constructor
CSceneNodeAnimatorCameraMaya::CSceneNodeAnimatorCameraMaya(gui::ICursorControl* cursor,
float rotateSpeed, float zoomSpeed, float translateSpeed, float distance)
: CursorControl(cursor), OldCamera(0), MousePos(0.5f, 0.5f),
ZoomSpeed(zoomSpeed), RotateSpeed(rotateSpeed), TranslateSpeed(translateSpeed),
CurrentZoom(distance), RotX(0.0f), RotY(0.0f),
Zooming(false), Rotating(false), Moving(false), Translating(false)
{
#ifdef _NBL_DEBUG
setDebugName("CSceneNodeAnimatorCameraMaya");
#endif
if (CursorControl)
{
CursorControl->grab();
MousePos = CursorControl->getRelativePosition();
}
allKeysUp();
}
//! destructor
CSceneNodeAnimatorCameraMaya::~CSceneNodeAnimatorCameraMaya()
{
if (CursorControl)
CursorControl->drop();
}
//! It is possible to send mouse and key events to the camera. Most cameras
//! may ignore this input, but camera scene nodes which are created for
//! example with scene::ISceneManager::addMayaCameraSceneNode or
//! scene::ISceneManager::addMeshViewerCameraSceneNode, may want to get this input
//! for changing their position, look at target or whatever.
bool CSceneNodeAnimatorCameraMaya::OnEvent(const SEvent& event)
{
if (event.EventType != EET_MOUSE_INPUT_EVENT)
return false;
switch(event.MouseInput.Event)
{
case EMIE_LMOUSE_PRESSED_DOWN:
MouseKeys[0] = true;
break;
case EMIE_RMOUSE_PRESSED_DOWN:
MouseKeys[2] = true;
break;
case EMIE_MMOUSE_PRESSED_DOWN:
MouseKeys[1] = true;
break;
case EMIE_LMOUSE_LEFT_UP:
MouseKeys[0] = false;
break;
case EMIE_RMOUSE_LEFT_UP:
MouseKeys[2] = false;
break;
case EMIE_MMOUSE_LEFT_UP:
MouseKeys[1] = false;
break;
case EMIE_MOUSE_MOVED:
MousePos = CursorControl->getRelativePosition();
break;
case EMIE_MOUSE_WHEEL:
case EMIE_LMOUSE_DOUBLE_CLICK:
case EMIE_RMOUSE_DOUBLE_CLICK:
case EMIE_MMOUSE_DOUBLE_CLICK:
case EMIE_LMOUSE_TRIPLE_CLICK:
case EMIE_RMOUSE_TRIPLE_CLICK:
case EMIE_MMOUSE_TRIPLE_CLICK:
case EMIE_COUNT:
return false;
}
return true;
}
//! OnAnimate() is called just before rendering the whole scene.
void CSceneNodeAnimatorCameraMaya::animateNode(IDummyTransformationSceneNode *node, uint32_t timeMs)
{
//Alt + LM = Rotate around camera pivot
//Alt + LM + MM = Dolly forth/back in view direction (speed % distance camera pivot - max distance to pivot)
//Alt + MM = Move on camera plane (Screen center is about the mouse pointer, depending on move speed)
if (!node/* || node->getType() != ESNT_CAMERA*/)
return;
ICameraSceneNode* camera = static_cast<ICameraSceneNode*>(node);
// If the camera isn't the active camera, and receiving input, then don't process it.
if (!camera->isInputReceiverEnabled())
return;
/*
scene::ISceneManager * smgr = camera->getSceneManager();
if (smgr && smgr->getActiveCamera() != camera)
return;
*/
if (OldCamera != camera)
{
LastCameraTarget = OldTarget = camera->getTarget();
OldCamera = camera;
}
else
{
OldTarget += camera->getTarget() - LastCameraTarget;
}
float nRotX = RotX;
float nRotY = RotY;
float nZoom = CurrentZoom;
if ( (isMouseKeyDown(0) && isMouseKeyDown(2)) || isMouseKeyDown(1) )
{
if (!Zooming)
{
ZoomStart = MousePos;
Zooming = true;
}
else
{
const float targetMinDistance = 0.1f;
nZoom += (ZoomStart.X - MousePos.X) * ZoomSpeed;
if (nZoom < targetMinDistance) // jox: fixed bug: bounce back when zooming to close
nZoom = targetMinDistance;
}
}
else if (Zooming)
{
const float old = CurrentZoom;
CurrentZoom = CurrentZoom + (ZoomStart.X - MousePos.X ) * ZoomSpeed;
nZoom = CurrentZoom;
if (nZoom < 0)
nZoom = CurrentZoom = old;
Zooming = false;
}
// Translation ---------------------------------
core::vector3df_SIMD translate(OldTarget);
core::vector3df_SIMD target,upVector;
upVector = camera->getUpVector();
target = camera->getTarget();
core::vector3df_SIMD pos,tvectX;
pos.getAsVector3df() = camera->getPosition();
tvectX = pos - target;
if (camera->getLeftHanded())
tvectX = normalize(cross(tvectX,upVector));
else
tvectX = normalize(cross(upVector, tvectX));
const SViewFrustum* const va = camera->getViewFrustum();
core::vector3df_SIMD tvectY = (va->getFarLeftDown() - va->getFarRightDown());
tvectY = normalize(cross(tvectY,upVector.Y > 0 ? pos - target : target - pos));
if (isMouseKeyDown(2) && !Zooming)
{
if (!Translating)
{
TranslateStart = MousePos;
Translating = true;
}
else
{
translate += tvectX * (TranslateStart.X - MousePos.X)*TranslateSpeed +
tvectY * (TranslateStart.Y - MousePos.Y)*TranslateSpeed;
}
}
else if (Translating)
{
translate += tvectX * (TranslateStart.X - MousePos.X)*TranslateSpeed +
tvectY * (TranslateStart.Y - MousePos.Y)*TranslateSpeed;
OldTarget = translate;
Translating = false;
}
// Rotation ------------------------------------
auto getValueDependentOnHandOrientation = [&](const float& expression)
{
return core::mix<float, bool>(-expression, expression, camera->getLeftHanded());
};
if (isMouseKeyDown(0) && !Zooming)
{
if (!Rotating)
{
RotateStart = MousePos;
Rotating = true;
nRotX = RotX;
nRotY = RotY;
}
else
{
nRotX += getValueDependentOnHandOrientation((RotateStart.X - MousePos.X) * RotateSpeed);
nRotY += getValueDependentOnHandOrientation((RotateStart.Y - MousePos.Y) * RotateSpeed);
}
}
else if (Rotating)
{
RotX += getValueDependentOnHandOrientation((RotateStart.X - MousePos.X) * RotateSpeed);
RotY += getValueDependentOnHandOrientation((RotateStart.Y - MousePos.Y) * RotateSpeed);
nRotX = RotX;
nRotY = RotY;
Rotating = false;
}
// Set pos ------------------------------------
pos = translate;
pos.X += nZoom;
pos.rotateXYByRAD( core::radians(nRotY), translate);
pos.rotateXZByRAD(-core::radians(nRotX), translate);
camera->setPosition(pos.getAsVector3df());
camera->setTarget(translate.getAsVector3df());
// Rotation Error ----------------------------
// jox: fixed bug: jitter when rotating to the top and bottom of y
pos.set(0,1,0);
pos.rotateXYByRAD(-core::radians(nRotY));
pos.rotateXZByRAD(core::PI<float>()-core::radians(nRotX));
camera->setUpVector(pos);
LastCameraTarget = camera->getTarget();
}
bool CSceneNodeAnimatorCameraMaya::isMouseKeyDown(int32_t key) const
{
return MouseKeys[key];
}
void CSceneNodeAnimatorCameraMaya::allKeysUp()
{
for (int32_t i=0; i<3; ++i)
MouseKeys[i] = false;
}
//! Sets the rotation speed
void CSceneNodeAnimatorCameraMaya::setRotateSpeed(float speed)
{
RotateSpeed = speed;
}
//! Sets the movement speed
void CSceneNodeAnimatorCameraMaya::setMoveSpeed(float speed)
{
TranslateSpeed = speed;
}
//! Sets the zoom speed
void CSceneNodeAnimatorCameraMaya::setZoomSpeed(float speed)
{
ZoomSpeed = speed;
}
//! Set the distance
void CSceneNodeAnimatorCameraMaya::setDistance(float distance)
{
CurrentZoom=distance;
}
//! Gets the rotation speed
float CSceneNodeAnimatorCameraMaya::getRotateSpeed() const
{
return RotateSpeed;
}
// Gets the movement speed
float CSceneNodeAnimatorCameraMaya::getMoveSpeed() const
{
return TranslateSpeed;
}
//! Gets the zoom speed
float CSceneNodeAnimatorCameraMaya::getZoomSpeed() const
{
return ZoomSpeed;
}
//! Returns the current distance, i.e. orbit radius
float CSceneNodeAnimatorCameraMaya::getDistance() const
{
return CurrentZoom;
}
} // end namespace
} // end namespace