-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCamera.cpp
More file actions
355 lines (307 loc) · 11.7 KB
/
Camera.cpp
File metadata and controls
355 lines (307 loc) · 11.7 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
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
#include "Camera.hpp"
#include "Object.hpp"
#include <vector>
#include <cstdint>
#include <cmath>
#include <Shader.hpp>
namespace Renderer
{
Camera::Camera()
: fov(60)
{
setFOV(fov, 800); // Initialize cached values
} // Default FOV to 60 degrees
void Camera::setPosition(int32_t x, int32_t y, int32_t z)
{
position.assign(x, y, z);
}
void Camera::setPosition(const Vector3 &pos)
{
position.assign(pos);
}
void Camera::setRotation(int32_t rotX, int32_t rotY, int32_t rotZ)
{
#if FLOAT_CAMERA_ANGLES
rotation.assign(
(float)rotX * (2.0f * M_PI) / ANGLE_MAX,
(float)rotY * (2.0f * M_PI) / ANGLE_MAX,
(float)rotZ * (2.0f * M_PI) / ANGLE_MAX);
#else
rotation.x = rotX % ANGLE_MAX;
rotation.y = rotY % ANGLE_MAX;
rotation.z = rotZ % ANGLE_MAX;
#endif
}
void Camera::setRotation(const Vector3 &rot)
{
#if FLOAT_CAMERA_ANGLES
rotation.assign(
(float)rot.x * (2.0f * M_PI) / ANGLE_MAX,
(float)rot.y * (2.0f * M_PI) / ANGLE_MAX,
(float)rot.z * (2.0f * M_PI) / ANGLE_MAX);
#else
rotation.assign(rot);
#endif
}
#if FLOAT_CAMERA_ANGLES
void Camera::rotate(float rotX, float rotY, float rotZ)
{
rotation.x += (float)rotX * (2.0f * M_PI) / ANGLE_MAX;
rotation.y += (float)rotY * (2.0f * M_PI) / ANGLE_MAX;
rotation.z += (float)rotZ * (2.0f * M_PI) / ANGLE_MAX;
// Normalize angles to [0, 2π]
while (rotation.x >= 2.0f * M_PI)
rotation.x -= 2.0f * M_PI;
while (rotation.y >= 2.0f * M_PI)
rotation.y -= 2.0f * M_PI;
while (rotation.z >= 2.0f * M_PI)
rotation.z -= 2.0f * M_PI;
while (rotation.x < 0)
rotation.x += 2.0f * M_PI;
while (rotation.y < 0)
rotation.y += 2.0f * M_PI;
while (rotation.z < 0)
rotation.z += 2.0f * M_PI;
}
void Camera::rotate(const Vector3_f &rot)
{
rotate(rot.x, rot.y, rot.z);
}
void Camera::rotateLocal(float rotX, float rotY, float rotZ)
{
// Convert input angles to radians
float localX = rotX * (2.0f * M_PI) / ANGLE_MAX;
float localY = rotY * (2.0f * M_PI) / ANGLE_MAX;
float localZ = rotZ * (2.0f * M_PI) / ANGLE_MAX;
// Add rotations directly since we're already in radians
rotation.x += localX;
rotation.y += localY;
rotation.z += localZ;
// Normalize angles to [0, 2π]
while (rotation.x >= 2.0f * M_PI) rotation.x -= 2.0f * M_PI;
while (rotation.y >= 2.0f * M_PI) rotation.y -= 2.0f * M_PI;
while (rotation.z >= 2.0f * M_PI) rotation.z -= 2.0f * M_PI;
while (rotation.x < 0) rotation.x += 2.0f * M_PI;
while (rotation.y < 0) rotation.y += 2.0f * M_PI;
while (rotation.z < 0) rotation.z += 2.0f * M_PI;
}
void Camera::rotateLocal(const Vector3_f &rot)
{
rotateLocal(rot.x, rot.y, rot.z);
}
#else
void Camera::rotate(int32_t rotX, int32_t rotY, int32_t rotZ)
{
rotation.x = (rotation.x + rotX) % ANGLE_MAX;
rotation.y = (rotation.y + rotY) % ANGLE_MAX;
rotation.z = (rotation.z + rotZ) % ANGLE_MAX;
}
void Camera::rotate(const Vector3 &rot)
{
rotation.add(rot);
rotation.x %= ANGLE_MAX;
rotation.y %= ANGLE_MAX;
rotation.z %= ANGLE_MAX;
}
void Camera::rotateLocal(int32_t rotX, int32_t rotY, int32_t rotZ)
{
rotation.x = (rotation.x + rotX) % ANGLE_MAX;
if (rotation.x < 0) rotation.x += ANGLE_MAX;
rotation.y = (rotation.y + rotY) % ANGLE_MAX;
if (rotation.y < 0) rotation.y += ANGLE_MAX;
rotation.z = (rotation.z + rotZ) % ANGLE_MAX;
if (rotation.z < 0) rotation.z += ANGLE_MAX;
}
void Camera::rotateLocal(const Vector3 &rot)
{
rotateLocal(rot.x, rot.y, rot.z);
}
#endif
void Camera::translate(int32_t dx, int32_t dy, int32_t dz)
{
position.add(dx, dy, dz);
}
void Camera::lookAt(const Vector3 &target)
{
// Calculate the direction vector from the camera to the target
Vector3 direction = target - position;
// Calculate the rotation angles to look at the target in 3D space
#if FLOAT_CAMERA_ANGLES
rotation.x = -std::atan2(direction.y, std::sqrt((int64_t)direction.x * direction.x + (int64_t)direction.z * direction.z));
rotation.y = std::atan2(direction.x, direction.z);
rotation.z = 0;
#else
rotation.x = -static_cast<int32_t>(std::atan2(direction.y, std::sqrt((int64_t)direction.x * direction.x + (int64_t)direction.z * direction.z)) * ANGLE_MAX / (2.0f * M_PI));
rotation.y = static_cast<int32_t>(std::atan2(direction.x, direction.z) * ANGLE_MAX / (2.0f * M_PI));
rotation.z = 0;
#endif
}
void Camera::lookAt(const Object *targetObject)
{
lookAt(targetObject->position);
}
void Camera::lookAtX(const Vector3 &target)
{
// Calculate the direction vector from the camera to the target
Vector3 direction = target - position;
// Calculate the rotation angles to look at the target in 3D space
#if FLOAT_CAMERA_ANGLES
rotation.x = -std::atan2(direction.y, std::sqrt((int64_t)direction.x * direction.x + (int64_t)direction.z * direction.z));
//rotation.y = 0;
rotation.z = 0;
#else
rotation.x = -static_cast<int32_t>(std::atan2(direction.y, std::sqrt((int64_t)direction.x * direction.x + (int64_t)direction.z * direction.z)) * ANGLE_MAX / (2.0f * M_PI));
//rotation.y = 0;
rotation.z = 0;
#endif
}
void Camera::lookAtX(const Object *targetObject)
{
lookAtX(targetObject->position);
}
void Camera::lookAtY(const Vector3 &target)
{
// Calculate the direction vector from the camera to the target
Vector3 direction = target - position;
// Calculate the rotation angles to look at the target in 3D space
#if FLOAT_CAMERA_ANGLES
//rotation.x = 0;
rotation.y = std::atan2(direction.x, direction.z);
rotation.z = 0;
#else
//rotation.x = 0;
rotation.y = static_cast<int32_t>(std::atan2(direction.x, direction.z) * ANGLE_MAX / (2.0f * M_PI));
rotation.z = 0;
#endif
}
void Camera::lookAtY(const Object *targetObject)
{
lookAtY(targetObject->position);
}
void Camera::translateLocal(int32_t dx, int32_t dy, int32_t dz)
{
#if FLOAT_CAMERA_ANGLES
float angleX = rotation.x;
float angleY = rotation.y;
float cosX = std::cos(angleX);
float sinX = std::sin(angleX);
float cosY = std::cos(angleY);
float sinY = std::sin(angleY);
// First rotate around X (pitch)
float rotatedY = dy * cosX - dz * sinX;
float rotatedZ = dy * sinX + dz * cosX;
// Then rotate around Y (yaw)
Vector3_f localTranslation(
dx * cosY + rotatedZ * sinY,
rotatedY,
-dx * sinY + rotatedZ * cosY);
position.add(localTranslation.x, localTranslation.y, localTranslation.z);
#else
int32_t angleX = rotation.x;
int32_t angleY = rotation.y;
int32_t cosX = lookupCosI(angleX);
int32_t sinX = lookupSinI(angleX);
int32_t cosY = lookupCosI(angleY);
int32_t sinY = lookupSinI(angleY);
// First rotate around X (pitch)
int32_t rotatedY = (dy * cosX - dz * sinX) / FIXED_POINT_SCALE;
int32_t rotatedZ = (dy * sinX + dz * cosX) / FIXED_POINT_SCALE;
// Then rotate around Y (yaw)
Vector3 localTranslation(
(dx * cosY + rotatedZ * sinY) / FIXED_POINT_SCALE,
rotatedY,
(-dx * sinY + rotatedZ * cosY) / FIXED_POINT_SCALE);
position.add(localTranslation.x, localTranslation.y, localTranslation.z);
#endif
}
void Camera::translateLocal(Vector3 translation)
{
translateLocal(translation.x, translation.y, translation.z);
}
void Camera::translateLocalY(int32_t dx, int32_t dy, int32_t dz)
{
//Translate the camera position in local space but only relative to its rotation on the Y axis.
//This is useful for moving the camera forward/backward relative to its current direction.
#if FLOAT_CAMERA_ANGLES
float angleY = rotation.y;
float cosY = std::cos(angleY);
float sinY = std::sin(angleY);
Vector3_f localTranslation(
dx * cosY + dz * sinY,
dy,
-dx * sinY + dz * cosY);
position.add(localTranslation.x, localTranslation.y, localTranslation.z);
#else
int32_t angleY = rotation.y;
int32_t cosY = lookupCosI(angleY);
int32_t sinY = lookupSinI(angleY);
Vector3 localTranslation(
(dx * cosY + dz * sinY) / FIXED_POINT_SCALE,
dy,
(-dx * sinY + dz * cosY) / FIXED_POINT_SCALE);
position.add(localTranslation.x, localTranslation.y, localTranslation.z);
#endif
}
void Camera::translateLocalY(Vector3 translation)
{
translateLocalY(translation.x, translation.y, translation.z);
}
void Camera::setFOV(int32_t fovDegrees, int32_t screenWidth)
{
fov = fovDegrees;
// Pre-compute FOV values
halfFOV = fov / 2;
if (halfFOV >= ANGLE_MAX)
halfFOV %= ANGLE_MAX;
tanHalfFOV = lookupTanI(halfFOV);
if (tanHalfFOV == 0)
tanHalfFOV = 1;
int64_t numerator = static_cast<int64_t>(screenWidth / 2) * FIXED_POINT_SCALE * FIXED_POINT_SCALE;
fovFactor = static_cast<int32_t>(numerator / tanHalfFOV);
}
void Camera::getRotationMatrix(int32_t& cosX, int32_t& sinX,
int32_t& cosY, int32_t& sinY,
int32_t& cosZ, int32_t& sinZ) const {
#if FLOAT_CAMERA_ANGLES
float angleXf = -rotation.x * 180.0f / M_PI;
float angleYf = -rotation.y * 180.0f / M_PI;
float angleZf = -rotation.z * 180.0f / M_PI;
cosX = lookupCos(angleXf) * FIXED_POINT_SCALE;
sinX = lookupSin(angleXf) * FIXED_POINT_SCALE;
cosY = lookupCos(angleYf) * FIXED_POINT_SCALE;
sinY = lookupSin(angleYf) * FIXED_POINT_SCALE;
cosZ = lookupCos(angleZf) * FIXED_POINT_SCALE;
sinZ = lookupSin(angleZf) * FIXED_POINT_SCALE;
#else
int32_t angleX = (ANGLE_MAX - rotation.x) % ANGLE_MAX;
int32_t angleY = (ANGLE_MAX - rotation.y) % ANGLE_MAX;
int32_t angleZ = (ANGLE_MAX - rotation.z) % ANGLE_MAX;
cosX = lookupCosI(angleX);
sinX = lookupSinI(angleX);
cosY = lookupCosI(angleY);
sinY = lookupSinI(angleY);
cosZ = lookupCosI(angleZ);
sinZ = lookupSinI(angleZ);
#endif
}
Vector3 Camera::transformDirection(const Vector3& dir) const {
Vector3 result = dir;
int32_t cosX, sinX, cosY, sinY, cosZ, sinZ;
getRotationMatrix(cosX, sinX, cosY, sinY, cosZ, sinZ);
// Y rotation first (yaw)
result.assign(
(result.x * cosY + result.z * sinY) / FIXED_POINT_SCALE,
result.y,
(-result.x * sinY + result.z * cosY) / FIXED_POINT_SCALE);
// X rotation second (pitch)
result.assign(result.x,
(result.y * cosX - result.z * sinX) / FIXED_POINT_SCALE,
(result.y * sinX + result.z * cosX) / FIXED_POINT_SCALE);
// Z rotation last (roll - usually 0 for FPS camera)
result.assign(
(result.x * cosZ - result.y * sinZ) / FIXED_POINT_SCALE,
(result.x * sinZ + result.y * cosZ) / FIXED_POINT_SCALE,
result.z);
return result;
}
} // namespace Renderer