-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCamera.hpp
More file actions
158 lines (141 loc) · 6.61 KB
/
Camera.hpp
File metadata and controls
158 lines (141 loc) · 6.61 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
#ifndef CAMERA_HPP
#define CAMERA_HPP
#include <cstdint>
#include "TrigLUT.hpp"
#include "Shader.hpp"
#include "Object.hpp"
#include "Config.hpp"
namespace Renderer {
/// @brief View / projection source for the renderer.
///
/// Holds world-space position, Euler rotation, FOV and near/far clipping
/// distances. Rotation precision is selected at compile time via
/// FLOAT_CAMERA_ANGLES; integer angles are kept in degrees on the
/// [0, ANGLE_MAX) range and converted through the trig LUTs.
class Camera {
public:
Vector3 position = {0,0,0};
#if FLOAT_CAMERA_ANGLES
Vector3_f rotation = {0,0,0};
#else
Vector3 rotation = {0,0,0};
#endif
int32_t fov; ///< Field of view in degrees.
int32_t nearPlane = 128; ///< Near clip distance. Values below 128 can degrade perspective-correct texture mapping.
int32_t farPlane = 1024; ///< Far clip distance.
int32_t halfFOV; ///< Cached half-FOV in degrees (set by setFOV).
int32_t tanHalfFOV; ///< Cached tan(halfFOV) in fixed point (set by setFOV).
int32_t fovFactor; ///< Cached projection scale factor (set by setFOV).
/// @brief Construct a camera with a default 60 degree FOV.
Camera();
/// @brief Set the world-space position.
/// @param x World X coordinate.
/// @param y World Y coordinate.
/// @param z World Z coordinate.
void setPosition(int32_t x, int32_t y, int32_t z);
/// @brief Set the world-space position.
/// @param pos World-space position vector.
void setPosition(const Vector3& pos);
/// @brief Set the absolute Euler rotation (degrees).
/// @param rotX Rotation about X (pitch).
/// @param rotY Rotation about Y (yaw).
/// @param rotZ Rotation about Z (roll).
void setRotation(int32_t rotX, int32_t rotY, int32_t rotZ);
/// @brief Set the absolute Euler rotation (degrees).
/// @param rot Rotation vector (X=pitch, Y=yaw, Z=roll).
void setRotation(const Vector3& rot);
#if FLOAT_CAMERA_ANGLES
/// @brief Add a delta rotation in world axes (degrees, float build).
/// @param rotX Delta about world X.
/// @param rotY Delta about world Y.
/// @param rotZ Delta about world Z.
void rotate(float rotX, float rotY, float rotZ);
/// @brief Add a delta rotation in world axes (degrees, float build).
/// @param rot Delta rotation vector.
void rotate(const Vector3_f& rot);
/// @brief Add a delta rotation in the camera's local axes (float build).
/// @param rotX Delta about local X.
/// @param rotY Delta about local Y.
/// @param rotZ Delta about local Z.
void rotateLocal(float rotX, float rotY, float rotZ);
/// @brief Add a delta rotation in the camera's local axes (float build).
/// @param rot Delta rotation vector.
void rotateLocal(const Vector3_f& rot);
#else
/// @brief Add a delta rotation in world axes (degrees).
/// @param rotX Delta about world X.
/// @param rotY Delta about world Y.
/// @param rotZ Delta about world Z.
void rotate(int32_t rotX, int32_t rotY, int32_t rotZ);
/// @brief Add a delta rotation in world axes (degrees).
/// @param rot Delta rotation vector.
void rotate(const Vector3& rot);
/// @brief Add a delta rotation in the camera's local axes (degrees).
/// @param rotX Delta about local X.
/// @param rotY Delta about local Y.
/// @param rotZ Delta about local Z.
void rotateLocal(int32_t rotX, int32_t rotY, int32_t rotZ);
/// @brief Add a delta rotation in the camera's local axes (degrees).
/// @param rot Delta rotation vector.
void rotateLocal(const Vector3& rot);
#endif
/// @brief Translate the camera in world space.
/// @param dx Delta X.
/// @param dy Delta Y.
/// @param dz Delta Z.
void translate(int32_t dx, int32_t dy, int32_t dz);
/// @brief Translate the camera along its local axes.
/// @param dx Delta along local X (right).
/// @param dy Delta along local Y (up).
/// @param dz Delta along local Z (forward).
void translateLocal(int32_t dx, int32_t dy, int32_t dz);
/// @brief Translate the camera along its local axes.
/// @param translation Local-space translation vector.
void translateLocal(Vector3 translation);
/// @brief Translate along local X/Z but keep Y world-aligned (yaw-only locomotion).
/// @param dx Delta along yaw-relative right.
/// @param dy Delta along world Y.
/// @param dz Delta along yaw-relative forward.
void translateLocalY(int32_t dx, int32_t dy, int32_t dz);
/// @brief Translate along local X/Z but keep Y world-aligned (yaw-only locomotion).
/// @param translation Translation vector.
void translateLocalY(Vector3 translation);
/// @brief Set the FOV and recompute cached projection factors.
/// @param fovDegrees Vertical field of view in degrees.
/// @param screenWidth Output width in pixels (used to derive fovFactor).
void setFOV(int32_t fovDegrees, int32_t screenWidth);
/// @brief Orient the camera so its forward vector points at a target.
/// @param target World-space point to look at.
void lookAt(const Vector3& target);
/// @brief Orient the camera so its forward vector points at an object's centre.
/// @param targetObject Object whose position+centreVolume is the target.
void lookAt(const Object* targetObject);
/// @brief Look-at restricted to the X (pitch) axis only.
/// @param target World-space point.
void lookAtX(const Vector3& target);
/// @brief Look-at restricted to the X (pitch) axis only.
/// @param targetObject Object to face.
void lookAtX(const Object* targetObject);
/// @brief Look-at restricted to the Y (yaw) axis only.
/// @param target World-space point.
void lookAtY(const Vector3& target);
/// @brief Look-at restricted to the Y (yaw) axis only.
/// @param targetObject Object to face.
void lookAtY(const Object* targetObject);
/// @brief Get the camera's combined rotation as sin/cos pairs per axis.
/// @param cosX Out: cosine of pitch.
/// @param sinX Out: sine of pitch.
/// @param cosY Out: cosine of yaw.
/// @param sinY Out: sine of yaw.
/// @param cosZ Out: cosine of roll.
/// @param sinZ Out: sine of roll.
void getRotationMatrix(int32_t& cosX, int32_t& sinX,
int32_t& cosY, int32_t& sinY,
int32_t& cosZ, int32_t& sinZ) const;
/// @brief Transform a direction vector from world to view space.
/// @param dir World-space direction.
/// @return Direction in camera/view space.
Vector3 transformDirection(const Vector3& dir) const;
};
} // namespace Renderer
#endif // CAMERA_HPP