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