11
11
#include < fstream>
12
12
#include < chrono>
13
13
14
- using namespace nbl ;
15
- using namespace core ;
16
- using namespace ui ;
17
-
18
14
class Camera {
19
15
public:
20
- Camera () = default ;
21
-
22
- Camera ( const core::vectorSIMDf& position,
23
- const core::vectorSIMDf& lookat,
24
- const core::matrix4SIMD& projection,
16
+ Camera (const nbl::core::vectorSIMDf& position,
17
+ const nbl::core::vectorSIMDf& lookat,
18
+ const nbl::core::matrix4SIMD& projection,
25
19
float moveSpeed = 1 .0f ,
26
20
float rotateSpeed = 1 .0f ,
27
- const core::vectorSIMDf& upVec = core::vectorSIMDf(0 .0f , 1 .0f , 0 .0f ),
28
- const core::vectorSIMDf& backupUpVec = core::vectorSIMDf(0 .5f , 1 .0f , 0 .0f )
21
+ const nbl:: core::vectorSIMDf& upVec = nbl:: core::vectorSIMDf(0 .0f , 1 .0f , 0 .0f ),
22
+ const nbl:: core::vectorSIMDf& backupUpVec = nbl:: core::vectorSIMDf(0 .5f , 1 .0f , 0 .0f )
29
23
)
30
24
: position(position)
31
25
, target(lookat)
@@ -44,90 +38,102 @@ class Camera {
44
38
45
39
public:
46
40
47
- inline const core::matrix4SIMD& getProjectionMatrix () const { return projMatrix; }
48
- inline const core::matrix3x4SIMD & getViewMatrix () const { return viewMatrix; }
49
- inline const core::matrix4SIMD & getConcatenatedMatrix () const { return concatMatrix; }
41
+ inline const nbl:: core::matrix4SIMD& getProjectionMatrix () const { return projMatrix; }
42
+ inline const nbl:: core::matrix3x4SIMD & getViewMatrix () const { return viewMatrix; }
43
+ inline const nbl:: core::matrix4SIMD & getConcatenatedMatrix () const { return concatMatrix; }
50
44
51
- inline void setProjectionMatrix (const core::matrix4SIMD& projection) {
45
+ inline void setProjectionMatrix (const nbl:: core::matrix4SIMD& projection) {
52
46
projMatrix = projection;
53
- leftHanded = core::determinant (projMatrix) < 0 .f ;
54
- concatMatrix = core::matrix4SIMD::concatenateBFollowedByAPrecisely (projMatrix, core::matrix4SIMD (viewMatrix));
47
+ leftHanded = nbl:: core::determinant (projMatrix) < 0 .f ;
48
+ concatMatrix = nbl:: core::matrix4SIMD::concatenateBFollowedByAPrecisely (projMatrix, nbl:: core::matrix4SIMD (viewMatrix));
55
49
}
56
50
57
- inline void setPosition (const core::vectorSIMDf& pos) {
51
+ inline void setPosition (const nbl:: core::vectorSIMDf& pos) {
58
52
position.set (pos);
59
53
recomputeViewMatrix ();
60
54
}
61
55
62
- inline const core::vectorSIMDf& getPosition () const { return position; }
56
+ inline const nbl:: core::vectorSIMDf& getPosition () const { return position; }
63
57
64
- inline void setTarget (const core::vectorSIMDf& pos) {
58
+ inline void setTarget (const nbl:: core::vectorSIMDf& pos) {
65
59
target.set (pos);
66
60
recomputeViewMatrix ();
67
61
}
68
62
69
- inline const core::vectorSIMDf& getTarget () const { return target; }
63
+ inline const nbl:: core::vectorSIMDf& getTarget () const { return target; }
70
64
71
- inline void setUpVector (const core::vectorSIMDf& up) {
65
+ inline void setUpVector (const nbl:: core::vectorSIMDf& up) {
72
66
upVector = up;
73
67
}
74
68
75
- inline void setBackupUpVector (const core::vectorSIMDf& up) {
69
+ inline void setBackupUpVector (const nbl:: core::vectorSIMDf& up) {
76
70
backupUpVector = up;
77
71
}
78
72
79
- inline const core::vectorSIMDf& getUpVector () const { return upVector; }
73
+ inline const nbl:: core::vectorSIMDf& getUpVector () const { return upVector; }
80
74
81
- inline const core::vectorSIMDf& getBackupUpVector () const { return backupUpVector; }
75
+ inline const nbl::core::vectorSIMDf& getBackupUpVector () const { return backupUpVector; }
76
+
77
+ inline const float getMoveSpeed () const { return moveSpeed; }
78
+
79
+ inline void setMoveSpeed (const float _moveSpeed) {
80
+ moveSpeed = _moveSpeed;
81
+ }
82
+
83
+ inline const float getRotateSpeed () const { return rotateSpeed; }
84
+
85
+ inline void setRotateSpeed (const float _rotateSpeed) {
86
+ rotateSpeed = _rotateSpeed;
87
+ }
82
88
83
89
inline void recomputeViewMatrix () {
84
- core::vectorSIMDf pos = position;
85
- core::vectorSIMDf localTarget = core::normalize (target - pos);
90
+ nbl:: core::vectorSIMDf pos = position;
91
+ nbl:: core::vectorSIMDf localTarget = nbl:: core::normalize (target - pos);
86
92
87
93
// if upvector and vector to the target are the same, we have a
88
94
// problem. so solve this problem:
89
- core::vectorSIMDf up = core::normalize (upVector);
90
- core::vectorSIMDf cross = core::cross (localTarget, up);
91
- bool upVectorNeedsChange = core::lengthsquared (cross)[0 ] == 0 ;
95
+ nbl:: core::vectorSIMDf up = nbl:: core::normalize (upVector);
96
+ nbl:: core::vectorSIMDf cross = nbl:: core::cross (localTarget, up);
97
+ bool upVectorNeedsChange = nbl:: core::lengthsquared (cross)[0 ] == 0 ;
92
98
if (upVectorNeedsChange)
93
99
{
94
- up = core::normalize (backupUpVector);
100
+ up = nbl:: core::normalize (backupUpVector);
95
101
}
96
102
97
103
if (leftHanded)
98
- viewMatrix = core::matrix3x4SIMD::buildCameraLookAtMatrixLH (pos, target, up);
104
+ viewMatrix = nbl:: core::matrix3x4SIMD::buildCameraLookAtMatrixLH (pos, target, up);
99
105
else
100
- viewMatrix = core::matrix3x4SIMD::buildCameraLookAtMatrixRH (pos, target, up);
101
- concatMatrix = core::matrix4SIMD::concatenateBFollowedByAPrecisely (projMatrix, core::matrix4SIMD (viewMatrix));
106
+ viewMatrix = nbl:: core::matrix3x4SIMD::buildCameraLookAtMatrixRH (pos, target, up);
107
+ concatMatrix = nbl:: core::matrix4SIMD::concatenateBFollowedByAPrecisely (projMatrix, nbl:: core::matrix4SIMD (viewMatrix));
102
108
}
103
109
104
110
inline bool getLeftHanded () const { return leftHanded; }
105
111
106
112
public:
107
113
108
- void mouseProcess (const IMouseEventChannel::range_t & events)
114
+ void mouseProcess (const nbl::ui:: IMouseEventChannel::range_t & events)
109
115
{
110
116
for (auto eventIt=events.begin (); eventIt!=events.end (); eventIt++)
111
117
{
112
118
auto ev = *eventIt;
113
119
114
- if (ev.type == ui::SMouseEvent::EET_CLICK && ev.clickEvent .mouseButton == ui::EMB_LEFT_BUTTON) {
115
- if (ev.clickEvent .action == ui::SMouseEvent::SClickEvent::EA_PRESSED) {
120
+ if (ev.type == nbl:: ui::SMouseEvent::EET_CLICK && ev.clickEvent .mouseButton == nbl:: ui::EMB_LEFT_BUTTON) {
121
+ if (ev.clickEvent .action == nbl:: ui::SMouseEvent::SClickEvent::EA_PRESSED) {
116
122
mouseDown = true ;
117
- } else if (ev.clickEvent .action == ui::SMouseEvent::SClickEvent::EA_RELEASED) {
123
+ } else if (ev.clickEvent .action == nbl:: ui::SMouseEvent::SClickEvent::EA_RELEASED) {
118
124
mouseDown = false ;
119
125
}
120
126
}
121
127
122
- if (ev.type == ui::SMouseEvent::EET_MOVEMENT && mouseDown) {
123
- core::vectorSIMDf pos = getPosition ();
124
- core::vectorSIMDf localTarget = getTarget () - pos;
128
+ if (ev.type == nbl:: ui::SMouseEvent::EET_MOVEMENT && mouseDown) {
129
+ nbl:: core::vectorSIMDf pos = getPosition ();
130
+ nbl:: core::vectorSIMDf localTarget = getTarget () - pos;
125
131
126
132
// Get Relative Rotation for localTarget in Radians
127
133
float relativeRotationX, relativeRotationY;
128
134
relativeRotationY = atan2 (localTarget.X , localTarget.Z );
129
- const double z1 = core::sqrt (localTarget.X *localTarget.X + localTarget.Z *localTarget.Z );
130
- relativeRotationX = atan2 (z1, localTarget.Y ) - core::PI<float >()/2 ;
135
+ const double z1 = nbl:: core::sqrt (localTarget.X *localTarget.X + localTarget.Z *localTarget.Z );
136
+ relativeRotationX = atan2 (z1, localTarget.Y ) - nbl:: core::PI<float >()/2 ;
131
137
132
138
constexpr float RotateSpeedScale = 0 .003f ;
133
139
relativeRotationX -= ev.movementEvent .relativeMovementY * rotateSpeed * RotateSpeedScale * -1 .0f ;
@@ -137,24 +143,24 @@ class Camera {
137
143
else
138
144
relativeRotationY += tmpYRot;
139
145
140
- const double MaxVerticalAngle = core::radians<float >(88 .0f );
146
+ const double MaxVerticalAngle = nbl:: core::radians<float >(88 .0f );
141
147
if (relativeRotationX > MaxVerticalAngle*2 &&
142
- relativeRotationX < 2 * core::PI<float >()-MaxVerticalAngle)
148
+ relativeRotationX < 2 * nbl:: core::PI<float >()-MaxVerticalAngle)
143
149
{
144
- relativeRotationX = 2 * core::PI<float >()-MaxVerticalAngle;
150
+ relativeRotationX = 2 * nbl:: core::PI<float >()-MaxVerticalAngle;
145
151
}
146
152
else
147
153
if (relativeRotationX > MaxVerticalAngle &&
148
- relativeRotationX < 2 * core::PI<float >()-MaxVerticalAngle)
154
+ relativeRotationX < 2 * nbl:: core::PI<float >()-MaxVerticalAngle)
149
155
{
150
156
relativeRotationX = MaxVerticalAngle;
151
157
}
152
158
153
- localTarget.set (0 ,0 , core::max (1 .f , core::length (pos)[0 ]), 1 .f );
159
+ localTarget.set (0 ,0 , nbl:: core::max (1 .f , nbl:: core::length (pos)[0 ]), 1 .f );
154
160
155
- core::matrix3x4SIMD mat;
161
+ nbl:: core::matrix3x4SIMD mat;
156
162
{
157
- mat.setRotation (core::quaternion (relativeRotationX, relativeRotationY, 0 ));
163
+ mat.setRotation (nbl:: core::quaternion (relativeRotationX, relativeRotationY, 0 ));
158
164
}
159
165
mat.transformVect (localTarget);
160
166
@@ -163,7 +169,7 @@ class Camera {
163
169
}
164
170
}
165
171
166
- void keyboardProcess (const IKeyboardEventChannel::range_t & events)
172
+ void keyboardProcess (const nbl::ui:: IKeyboardEventChannel::range_t & events)
167
173
{
168
174
for (uint32_t k = 0 ; k < Keys::EKA_COUNT; ++k) {
169
175
perActionDt[k] = 0.0 ;
@@ -190,41 +196,41 @@ class Camera {
190
196
auto timeDiff = std::chrono::duration_cast<std::chrono::milliseconds>(nextPresentationTimeStamp - ev.timeStamp ).count ();
191
197
assert (timeDiff >= 0 );
192
198
193
- if (ev.keyCode == ui::EKC_UP_ARROW || ev.keyCode == ui::EKC_W) {
194
- if (ev.action == ui::SKeyboardEvent::ECA_PRESSED && keysDown[Keys::EKA_MOVE_FORWARD] == false ) {
199
+ if (ev.keyCode == nbl:: ui::EKC_UP_ARROW || ev.keyCode == nbl:: ui::EKC_W) {
200
+ if (ev.action == nbl:: ui::SKeyboardEvent::ECA_PRESSED && keysDown[Keys::EKA_MOVE_FORWARD] == false ) {
195
201
perActionDt[Keys::EKA_MOVE_FORWARD] += timeDiff;
196
202
keysDown[Keys::EKA_MOVE_FORWARD] = true ;
197
- } else if (ev.action == ui::SKeyboardEvent::ECA_RELEASED) {
203
+ } else if (ev.action == nbl:: ui::SKeyboardEvent::ECA_RELEASED) {
198
204
// perActionDt[Keys::EKA_MOVE_FORWARD] -= timeDiff;
199
205
keysDown[Keys::EKA_MOVE_FORWARD] = false ;
200
206
}
201
207
}
202
208
203
- if (ev.keyCode == ui::EKC_DOWN_ARROW || ev.keyCode == ui::EKC_S) {
204
- if (ev.action == ui::SKeyboardEvent::ECA_PRESSED && keysDown[Keys::EKA_MOVE_BACKWARD] == false ) {
209
+ if (ev.keyCode == nbl:: ui::EKC_DOWN_ARROW || ev.keyCode == nbl:: ui::EKC_S) {
210
+ if (ev.action == nbl:: ui::SKeyboardEvent::ECA_PRESSED && keysDown[Keys::EKA_MOVE_BACKWARD] == false ) {
205
211
perActionDt[Keys::EKA_MOVE_BACKWARD] += timeDiff;
206
212
keysDown[Keys::EKA_MOVE_BACKWARD] = true ;
207
- } else if (ev.action == ui::SKeyboardEvent::ECA_RELEASED) {
213
+ } else if (ev.action == nbl:: ui::SKeyboardEvent::ECA_RELEASED) {
208
214
// perActionDt[Keys::EKA_MOVE_BACKWARD] -= timeDiff;
209
215
keysDown[Keys::EKA_MOVE_BACKWARD] = false ;
210
216
}
211
217
}
212
218
213
- if (ev.keyCode == ui::EKC_LEFT_ARROW || ev.keyCode == ui::EKC_A) {
214
- if (ev.action == ui::SKeyboardEvent::ECA_PRESSED && keysDown[Keys::EKA_MOVE_LEFT] == false ) {
219
+ if (ev.keyCode == nbl:: ui::EKC_LEFT_ARROW || ev.keyCode == nbl:: ui::EKC_A) {
220
+ if (ev.action == nbl:: ui::SKeyboardEvent::ECA_PRESSED && keysDown[Keys::EKA_MOVE_LEFT] == false ) {
215
221
perActionDt[Keys::EKA_MOVE_LEFT] += timeDiff;
216
222
keysDown[Keys::EKA_MOVE_LEFT] = true ;
217
- } else if (ev.action == ui::SKeyboardEvent::ECA_RELEASED) {
223
+ } else if (ev.action == nbl:: ui::SKeyboardEvent::ECA_RELEASED) {
218
224
// perActionDt[Keys::EKA_MOVE_LEFT] -= timeDiff;
219
225
keysDown[Keys::EKA_MOVE_LEFT] = false ;
220
226
}
221
227
}
222
228
223
- if (ev.keyCode == ui::EKC_RIGHT_ARROW || ev.keyCode == ui::EKC_D) {
224
- if (ev.action == ui::SKeyboardEvent::ECA_PRESSED && keysDown[Keys::EKA_MOVE_RIGHT] == false ) {
229
+ if (ev.keyCode == nbl:: ui::EKC_RIGHT_ARROW || ev.keyCode == nbl:: ui::EKC_D) {
230
+ if (ev.action == nbl:: ui::SKeyboardEvent::ECA_PRESSED && keysDown[Keys::EKA_MOVE_RIGHT] == false ) {
225
231
perActionDt[Keys::EKA_MOVE_RIGHT] += timeDiff;
226
232
keysDown[Keys::EKA_MOVE_RIGHT] = true ;
227
- } else if (ev.action == ui::SKeyboardEvent::ECA_RELEASED) {
233
+ } else if (ev.action == nbl:: ui::SKeyboardEvent::ECA_RELEASED) {
228
234
// perActionDt[Keys::EKA_MOVE_RIGHT] -= timeDiff;
229
235
keysDown[Keys::EKA_MOVE_RIGHT] = false ;
230
236
}
@@ -245,12 +251,12 @@ class Camera {
245
251
}
246
252
247
253
void endInputProcessing (std::chrono::microseconds _nextPresentationTimeStamp) {
248
- core::vectorSIMDf pos = getPosition ();
249
- core::vectorSIMDf localTarget = getTarget () - pos;
254
+ nbl:: core::vectorSIMDf pos = getPosition ();
255
+ nbl:: core::vectorSIMDf localTarget = getTarget () - pos;
250
256
251
- core::vectorSIMDf movedir = localTarget;
257
+ nbl:: core::vectorSIMDf movedir = localTarget;
252
258
movedir.makeSafe3D ();
253
- movedir = core::normalize (movedir);
259
+ movedir = nbl:: core::normalize (movedir);
254
260
255
261
constexpr float MoveSpeedScale = 0 .02f ;
256
262
@@ -262,21 +268,21 @@ class Camera {
262
268
263
269
// if upvector and vector to the target are the same, we have a
264
270
// problem. so solve this problem:
265
- core::vectorSIMDf up = core::normalize (upVector);
266
- core::vectorSIMDf cross = core::cross (localTarget, up);
267
- bool upVectorNeedsChange = core::lengthsquared (cross)[0 ] == 0 ;
271
+ nbl:: core::vectorSIMDf up = nbl:: core::normalize (upVector);
272
+ nbl:: core::vectorSIMDf cross = nbl:: core::cross (localTarget, up);
273
+ bool upVectorNeedsChange = nbl:: core::lengthsquared (cross)[0 ] == 0 ;
268
274
if (upVectorNeedsChange)
269
275
{
270
- up = core::normalize (backupUpVector);
276
+ up = nbl:: core::normalize (backupUpVector);
271
277
}
272
278
273
- core::vectorSIMDf strafevect = localTarget;
279
+ nbl:: core::vectorSIMDf strafevect = localTarget;
274
280
if (leftHanded)
275
- strafevect = core::cross (strafevect, up);
281
+ strafevect = nbl:: core::cross (strafevect, up);
276
282
else
277
- strafevect = core::cross (up, strafevect);
283
+ strafevect = nbl:: core::cross (up, strafevect);
278
284
279
- strafevect = core::normalize (strafevect);
285
+ strafevect = nbl:: core::normalize (strafevect);
280
286
281
287
pos += strafevect * perActionDt[Keys::EKA_MOVE_LEFT] * moveSpeed * MoveSpeedScale;
282
288
@@ -299,16 +305,16 @@ class Camera {
299
305
}
300
306
301
307
private:
302
- core::vectorSIMDf position;
303
- core::vectorSIMDf target;
304
- core::vectorSIMDf upVector;
305
- core::vectorSIMDf backupUpVector;
308
+ nbl:: core::vectorSIMDf position;
309
+ nbl:: core::vectorSIMDf target;
310
+ nbl:: core::vectorSIMDf upVector;
311
+ nbl:: core::vectorSIMDf backupUpVector;
306
312
307
- core::matrix3x4SIMD viewMatrix;
308
- core::matrix4SIMD concatMatrix;
313
+ nbl:: core::matrix3x4SIMD viewMatrix;
314
+ nbl:: core::matrix4SIMD concatMatrix;
309
315
310
316
// actual projection matrix used
311
- core::matrix4SIMD projMatrix;
317
+ nbl:: core::matrix4SIMD projMatrix;
312
318
313
319
bool leftHanded;
314
320
0 commit comments