nngn
Loading...
Searching...
No Matches
camera.h
Go to the documentation of this file.
1#ifndef NNGN_MATH_CAMERA_H
2#define NNGN_MATH_CAMERA_H
3
4#include "utils/def.h"
5#include "utils/flags.h"
6
7#include "mat4.h"
8#include "math.h"
9#include "vec2.h"
10#include "vec3.h"
11
12namespace nngn {
13
14struct Timing;
15
69struct Camera {
71 enum Flag : u8 {
77 UPDATED = 1u << 0,
84 SCREEN_UPDATED = 1u << 1,
86 DASH = 1u << 2,
88 PERSPECTIVE = 1u << 3,
90 IGNORE_LIMITS = 1u << 4,
91 };
93 static constexpr float NEAR = 0.01f;
95 static constexpr float FAR = 2048;
97 static constexpr float FOVY = static_cast<float>(Math::pi() / 3);
113 static constexpr float fov(float z, float w);
114 // Flags
115 bool dash(void) const;
116 bool perspective(void) const;
117 void set_dash(bool b);
118 void set_perspective(bool b);
119 void set_ignore_limits(bool b);
120 // Positions / vectors
122 vec3 center(void) const;
124 vec3 up(void) const;
126 vec3 world_to_view(vec3 p) const;
128 vec3 view_to_clip(vec3 p) const;
134 vec3 clip_to_view(vec3 p) const;
136 vec3 view_to_world(vec3 p) const;
137 // Screen
151 float z_for_fov(void) const;
168 float scale_for_fov(void) const;
169 // Mutators
170 void set_pos(vec3 p);
171 void set_rot(vec3 r);
172 void set_zoom(float z);
173 void set_fov_y(float f);
174 void set_fov_z(float z) { this->fov_z = z; }
175 void set_screen(uvec2 s);
176 void set_limits(vec3 bl, vec3 tr);
177 void look_at(vec3 center, vec3 pos, vec3 up);
178 bool update(const Timing &t);
179 // Data
181 vec3 p = {};
183 vec3 v = {};
185 vec3 a = {};
187 vec3 rot = {};
193 vec3 bl_limit = {-INFINITY, -INFINITY, -INFINITY};
195 vec3 tr_limit = {+INFINITY, +INFINITY, +INFINITY};
197 float zoom = 1;
199 float zoom_v = {};
201 float zoom_a = {};
203 float max_v = INFINITY;
205 float max_rot_v = INFINITY;
207 float max_zoom_v = INFINITY;
209 float fov_y = FOVY;
216 float fov_z = {};
218 float damp = {};
232};
233
234inline constexpr float Camera::fov(float z, float w) {
235 return 2.0f * std::atan(w / 2.0f / z);
236}
237
238inline bool Camera::dash(void) const {
239 return this->flags.is_set(Flag::DASH);
240}
241
242inline bool Camera::perspective(void) const {
243 return this->flags.is_set(Flag::PERSPECTIVE);
244}
245
246inline void Camera::set_dash(bool b) {
247 this->flags.set(Flag::DASH, b);
248}
249
250inline void Camera::set_perspective(bool b) {
251 this->flags.set(Flag::PERSPECTIVE, b);
252 this->flags.set(Flag::UPDATED);
253}
254
255inline void Camera::set_ignore_limits(bool b) {
256 this->flags.set(Flag::IGNORE_LIMITS, b);
257 this->flags.set(Flag::UPDATED);
258}
259
261 return Math::perspective_transform(this->view, wp);
262}
263
265 return Math::perspective_transform(this->proj, vp);
266}
267
269 return (cp.xy() + vec2{1}) / 2.0f * static_cast<vec2>(this->screen);
270}
271
273 return {2.0f * (sp / static_cast<vec2>(this->screen)) - vec2{1} , 0};
274}
275
277 return Math::perspective_transform(this->inv_proj, cp);
278}
279
281 return Math::perspective_transform(this->inv_view, vp);
282}
283
284inline float Camera::z_for_fov(void) const {
285 return static_cast<float>(this->screen.y)
286 / 2.0f
287 / std::tan(this->fov_y / 2);
288}
289
290inline float Camera::scale_for_fov(void) const {
291 return this->zoom * this->z_for_fov() / this->p.z;
292}
293
295 this->p = pos;
296 this->flags.set(Flag::UPDATED);
297}
298
299inline void Camera::set_rot(vec3 r) {
300 this->rot = r;
301 this->flags.set(Flag::UPDATED);
302}
303
304inline void Camera::set_zoom(float z) {
305 this->zoom = z;
306 this->flags.set(Flag::UPDATED);
307}
308
309inline void Camera::set_fov_y(float f) {
310 this->fov_y = f;
311 this->flags.set(Flag::UPDATED);
312}
313
315 this->screen = s;
316 this->flags.set(Flag::SCREEN_UPDATED);
317}
318
320 this->bl_limit = bl;
321 this->tr_limit = tr;
322 this->flags.set(Flag::UPDATED);
323}
324
325}
326
327#endif
static constexpr T pi(void)
Definition math.h:186
static vec3_base< T > perspective_transform(const mat4_base< T > &m, vec3_base< T > v)
Definition math.h:333
local r
Definition gamma.lua:7
update
Definition img_common.lua:42
set_limits
Definition img_common.lua:33
pos
Definition lua_audio.cpp:17
Definition audio.cpp:7
std::uint8_t u8
Definition def.h:12
set_perspective
Definition camera.lua:51
local function f()) end
local w
Definition strict.lua:12
Abstract orthographic/perspective camera.
Definition camera.h:69
float fov_y
Field of view in perspective mode.
Definition camera.h:209
float fov_z
Enable auto-adjustment of the field-of-view angle.
Definition camera.h:216
vec3 tr_limit
Top right point of the limit for p.
Definition camera.h:195
void set_perspective(bool b)
Definition camera.h:250
vec3 rot_a
Rotational acceleration.
Definition camera.h:191
float damp
Damping factor when not accelerating.
Definition camera.h:218
vec3 up(void) const
The "up" vector for the view direction.
Definition camera.cpp:27
void set_fov_y(float f)
Definition camera.h:309
mat4 screen_proj
Projection matrix for screen coordinates.
Definition camera.h:224
vec3 clip_to_view(vec3 p) const
Projects a point from clip to view space.
Definition camera.h:276
void set_screen(uvec2 s)
Definition camera.h:314
static constexpr float NEAR
Perspective projection near plane.
Definition camera.h:93
vec3 bl_limit
Bottom left point of the limit for p.
Definition camera.h:193
bool dash(void) const
Definition camera.h:238
float zoom_a
Scaling factor acceleration.
Definition camera.h:201
float z_for_fov(void) const
Derives Z coordinate from the camera's screen size and field of view.
Definition camera.h:284
void set_zoom(float z)
Definition camera.h:304
float zoom
Scaling factor.
Definition camera.h:197
bool perspective(void) const
Definition camera.h:242
uvec2 screen
Screen size.
Definition camera.h:220
vec3 a
Acceleration.
Definition camera.h:185
mat4 view
View matrix.
Definition camera.h:226
mat4 inv_view
Inverse matrix of view.
Definition camera.h:230
void set_fov_z(float z)
Definition camera.h:174
vec3 v
Velocity.
Definition camera.h:183
float max_rot_v
Maximum value for rot_v.
Definition camera.h:205
float max_zoom_v
Maximum value for zoom_v.
Definition camera.h:207
void set_limits(vec3 bl, vec3 tr)
Definition camera.h:319
static constexpr float fov(float z, float w)
Calculates field of view based on screen size and distance.
Definition camera.h:234
vec3 view_to_clip(vec3 p) const
Projects a point from view to clip space.
Definition camera.h:264
vec2 clip_to_screen(vec3 p)
Projects a point from clip to screen space.
Definition camera.h:268
float zoom_v
Scaling factor velocity.
Definition camera.h:199
vec3 rot_v
Rotational velocity.
Definition camera.h:189
vec3 center(void) const
p added to a unit vector in the view direction.
Definition camera.cpp:23
static constexpr float FAR
Perspective projection far plane.
Definition camera.h:95
Flag
State flags and configuration.
Definition camera.h:71
@ DASH
Increases the speed of displacement/rotation/etc.
Definition camera.h:86
@ PERSPECTIVE
Chooses between orthographic/perspective mode.
Definition camera.h:88
@ UPDATED
Parameters of the camera have changed.
Definition camera.h:77
@ SCREEN_UPDATED
The screen size has changed.
Definition camera.h:84
@ IGNORE_LIMITS
The camera limits do not restrict movement when set.
Definition camera.h:90
void set_pos(vec3 p)
Definition camera.h:294
mat4 inv_proj
Inverse matrix of proj.
Definition camera.h:228
void set_ignore_limits(bool b)
Definition camera.h:255
void set_rot(vec3 r)
Definition camera.h:299
vec3 screen_to_clip(vec2 p)
Projects a point from screen to clip space.
Definition camera.h:272
vec3 world_to_view(vec3 p) const
Projects a point from world to view space.
Definition camera.h:260
vec3 view_to_world(vec3 p) const
Projects a point from view to world space.
Definition camera.h:280
void set_dash(bool b)
Definition camera.h:246
float max_v
Maximum value for v.
Definition camera.h:203
vec3 rot
Rotational Euler angles (ZYX).
Definition camera.h:187
void look_at(vec3 center, vec3 pos, vec3 up)
Definition camera.cpp:33
vec3 p
Position.
Definition camera.h:181
Flags< Flag > flags
Definition camera.h:231
static constexpr float FOVY
Perspective projection field of view along the Y axis (default).
Definition camera.h:97
float scale_for_fov(void) const
Derives orthographic scaling factor from camera parameters.
Definition camera.h:290
mat4 proj
Projection matrix (orthographic/perspective).
Definition camera.h:222
Wrapper for a small unsigned integral representing flags.
Definition flags.h:18
constexpr bool is_set(AT a) const
Definition flags.h:45
constexpr Flags & set(AT a)
Definition flags.h:47
Definition timing.h:20
T y
Definition vec2.h:12
T z
Definition vec3.h:13
constexpr vec2_base< T > xy() const
Definition vec3.h:26
static constexpr nngn::vec2 tr
Definition collision.cpp:8
static constexpr nngn::vec2 bl
Definition collision.cpp:8
std::chrono::seconds s
Definition timing.cpp:6