Unravel Engine C++ Reference
Loading...
Searching...
No Matches
camera.h
Go to the documentation of this file.
1#pragma once
2#include <engine/engine_export.h>
3
4#include <base/basetypes.hpp>
5#include <context/context.hpp>
6#include <math/math.h>
9
10namespace unravel
11{
15enum class projection_mode : std::uint32_t
16{
17 perspective = 0,
18 orthographic = 1
19};
20
24enum class taa_jitter_mode : std::uint8_t
25{
29 halton_2_3 = 1,
46};
47
55
61class camera : public crtp_meta_type<camera>
62{
63public:
65
66
72
78 void set_fov(float degrees);
79
85 void set_near_clip(float distance);
86
92 void set_far_clip(float distance);
93
99 void set_orthographic_size(float size);
100
106 auto get_projection_mode() const -> projection_mode;
107
113 auto get_fov() const -> float;
114
120 auto get_near_clip() const -> float;
121
127 auto get_far_clip() const -> float;
128
134 auto get_ortho_size() const -> float;
135
141 auto get_zoom_factor() const -> float;
142
148 auto get_ppu() const -> float;
149
155 void set_viewport_size(const usize32_t& viewport_size);
156
162 void set_viewport_pos(const ipoint32_t& viewport_pos);
163
169 auto get_viewport_size() const -> const usize32_t&;
170
176 auto get_viewport_pos() const -> const ipoint32_t&;
177
184 void set_aspect_ratio(float aspect, bool locked = false);
185
191 auto get_aspect_ratio() const -> float;
192
198 auto is_aspect_locked() const -> bool;
199
205 auto is_frustum_locked() const -> bool;
206
212 void lock_frustum(bool locked);
213
219 auto get_frustum() const -> const math::frustum&;
220
226 auto get_clipping_volume() const -> const math::frustum&;
227
233 auto get_projection() const -> const math::transform&;
234 auto get_prev_projection() const -> const math::transform&;
235
241 auto get_view() const -> const math::transform&;
242 auto get_prev_view() const -> const math::transform&;
243 auto get_view_inverse() const -> const math::transform&;
244 auto get_view_relative() const -> const math::transform&;
245 auto get_prev_view_relative() const -> const math::transform&;
246 auto get_view_inverse_relative() const -> const math::transform&;
254
266 auto get_view_projection() const -> math::transform;
268
273
287 void set_aa_data(const usize32_t& viewport_size,
288 std::uint32_t temporal_frame_index,
289 std::uint32_t temporal_aa_samples,
291 float jitter_amplitude = 1.0f,
292 float jitter_temporal_phase_scale = 1.0f);
293
299 auto get_aa_data() const -> const math::vec4&;
300
307 auto classify_aabb(const math::bbox& bounds) const -> math::volume_query;
308
315 auto test_aabb(const math::bbox& bounds) const -> bool;
316
324 auto classify_obb(const math::bbox& bounds, const math::transform& t) const -> math::volume_query;
325
333 auto test_obb(const math::bbox& bounds, const math::transform& t) const -> bool;
334
335 auto test_billboard(float size, const math::transform& t) const -> bool;
336
337
346 auto viewport_to_ray(const math::vec2& point, math::vec3& vec_ray_start, math::vec3& vec_ray_dir) const -> bool;
347
357 auto viewport_to_world(const math::vec2& point, const math::plane& plane, math::vec3& position_out, bool clip) const
358 -> bool;
359
374 auto project_to_quad(const math::vec2& viewport_point,
375 const math::transform& quad_transform,
376 uint32_t quad_width,
377 uint32_t quad_height,
378 math::vec2& pixel_out) const -> bool;
379
389 auto viewport_to_major_axis(const math::vec2& point,
390 const math::vec3& axis_origin,
391 math::vec3& position_out,
392 math::vec3& major_axis_out) const -> bool;
393
404 auto viewport_to_major_axis(const math::vec2& point,
405 const math::vec3& axis_origin,
406 const math::vec3& align_normal,
407 math::vec3& position_out,
408 math::vec3& major_axis_out) const -> bool;
409
417 auto viewport_to_camera(const math::vec3& point, math::vec3& position_out) const -> bool;
418
425 auto world_to_viewport(const math::vec3& pos) const -> math::vec3;
426
433 auto estimate_zoom_factor(const math::plane& plane) const -> float;
434
441 auto estimate_zoom_factor(const math::vec3& position) const -> float;
442
450 auto estimate_zoom_factor(const math::plane& plane, float maximum_value) const -> float;
451
459 auto estimate_zoom_factor(const math::vec3& position, float maximum_value) const -> float;
460
469 auto estimate_pick_tolerance(float pixel_tolerance,
470 const math::vec3& reference_position,
471 const math::transform& object_transform) const -> math::vec3;
472
479 void look_at(const math::vec3& eye, const math::vec3& at);
480
488 void look_at(const math::vec3& eye, const math::vec3& at, const math::vec3& up);
489
495 auto get_position() const -> const math::vec3&;
496
502 auto x_unit_axis() const -> math::vec3;
503
509 auto y_unit_axis() const -> math::vec3;
510
516 auto z_unit_axis() const -> math::vec3;
517
524
528 void touch();
529
537 static auto get_face_camera(std::uint32_t face, const math::transform& transform) -> camera;
538
539protected:
541 math::vec4 aa_data_ = {0.0f, 0.0f, 0.0f, 0.0f};
545
548
554
557
568 float fov_ = 60.0f;
570 float near_clip_ = 0.1f;
572 float far_clip_ = 1000.0f;
574 float ortho_size_ = 5;
576 float aspect_ratio_ = 1.0f;
582 bool view_dirty_ = true;
584 mutable bool projection_dirty_ = true;
586 mutable bool aspect_dirty_ = true;
588 mutable bool frustum_dirty_ = true;
590 bool aspect_locked_ = false;
592 bool frustum_locked_ = false;
593};
594} // namespace unravel
Storage for frustum planes / values and wraps up common functionality.
Definition frustum.h:18
General purpose transformation class designed to maintain each component of the transformation separa...
Definition transform.hpp:27
Class representing a camera. Contains functionality for manipulating and updating a camera....
Definition camera.h:62
auto get_view_projection_relative() const -> math::transform
Definition camera.cpp:323
auto get_far_clip() const -> float
Retrieves the distance from the camera to the far clip plane.
Definition camera.cpp:69
float far_clip_
Far clip plane Distance.
Definition camera.h:572
auto get_fov() const -> float
Retrieves the current field of view angle in degrees.
Definition camera.cpp:59
auto classify_obb(const math::bbox &bounds, const math::transform &t) const -> math::volume_query
Determines if the specified OBB is within the frustum.
Definition camera.cpp:442
math::transform taa_prev_view_
Snapshot for temporal reprojection: view/projection before advancing jitter (see set_aa_data).
Definition camera.h:559
auto viewport_to_major_axis(const math::vec2 &point, const math::vec3 &axis_origin, math::vec3 &position_out, math::vec3 &major_axis_out) const -> bool
Converts a screen position into a world space intersection point on a major axis plane.
Definition camera.cpp:610
auto get_taa_prev_view_projection() const -> math::transform
View-projection used for the previous rendered frame (before the latest jitter sample).
Definition camera.cpp:318
auto viewport_to_world(const math::vec2 &point, const math::plane &plane, math::vec3 &position_out, bool clip) const -> bool
Converts a screen position into a world space position on the specified plane.
Definition camera.cpp:530
auto viewport_to_ray(const math::vec2 &point, math::vec3 &vec_ray_start, math::vec3 &vec_ray_dir) const -> bool
Converts the specified screen position into a ray origin and direction vector.
Definition camera.cpp:497
auto get_prev_view_projection() const -> math::transform
Retrieves the previous view matrix.
Definition camera.cpp:313
auto get_viewport_pos() const -> const ipoint32_t &
Retrieves the position of the viewport.
Definition camera.cpp:42
auto test_obb(const math::bbox &bounds, const math::transform &t) const -> bool
Tests if the specified OBB is within the frustum.
Definition camera.cpp:451
auto get_prev_view_relative() const -> const math::transform &
Definition camera.cpp:293
ipoint32_t viewport_pos_
Viewport position.
Definition camera.h:578
void set_fov(float degrees)
Sets the field of view angle of this camera (perspective only).
Definition camera.cpp:79
auto get_projection_mode() const -> projection_mode
Retrieves the current projection mode for this camera.
Definition camera.cpp:54
void look_at(const math::vec3 &eye, const math::vec3 &at)
Sets the camera to look at a specified target.
Definition camera.cpp:333
void set_orthographic_size(float size)
Sets the half of the vertical size of the viewing volume in world units.
Definition camera.cpp:47
bool aspect_locked_
Should the aspect ratio be automatically updated by the render driver?
Definition camera.h:590
math::transform view_relative_
Definition camera.h:546
math::frustum clipping_volume_
The near clipping volume (area of space between the camera position and the near plane).
Definition camera.h:564
auto get_ppu() const -> float
Retrieves the pixels per unit (PPU).
Definition camera.cpp:21
auto get_viewport_size() const -> const usize32_t &
Retrieves the size of the viewport.
Definition camera.cpp:37
auto x_unit_axis() const -> math::vec3
Retrieves the x-axis unit vector of the camera's local coordinate system.
Definition camera.cpp:358
projection_mode projection_mode_
The type of projection currently selected for this camera.
Definition camera.h:566
bool view_dirty_
View matrix dirty ?
Definition camera.h:582
auto get_projection() const -> const math::transform &
Retrieves the current projection matrix.
Definition camera.cpp:205
auto get_clipping_volume() const -> const math::frustum &
Retrieves the frustum representing the space between the camera position and its near plane.
Definition camera.cpp:412
auto get_zoom_factor() const -> float
Retrieves the zoom factor.
Definition camera.cpp:11
math::frustum frustum_
Details regarding the camera frustum.
Definition camera.h:562
float aspect_ratio_
The aspect ratio used to generate the correct horizontal degrees (perspective only)
Definition camera.h:576
auto get_local_bounding_box() -> math::bbox
Retrieves the bounding box of this object.
Definition camera.cpp:147
static auto get_face_camera(std::uint32_t face, const math::transform &transform) -> camera
Retrieves a camera for one of six cube faces.
Definition camera.cpp:872
void touch()
Marks the camera as modified.
Definition camera.cpp:863
float near_clip_
Near clip plane Distance.
Definition camera.h:570
bool projection_dirty_
Projection matrix dirty ?
Definition camera.h:584
auto get_view() const -> const math::transform &
Retrieves the current view matrix.
Definition camera.cpp:278
math::transform view_inverse_relative_
Definition camera.h:547
math::transform projection_
Cached projection matrix.
Definition camera.h:550
float ortho_size_
camera's half-size when in orthographic mode.
Definition camera.h:574
math::transform last_view_
Cached "previous" view matrix.
Definition camera.h:552
auto test_aabb(const math::bbox &bounds) const -> bool
Tests if the specified AABB is within the frustum.
Definition camera.cpp:433
auto test_billboard(float size, const math::transform &t) const -> bool
Definition camera.cpp:460
math::transform view_
Cached view matrix.
Definition camera.h:543
bool frustum_dirty_
Are the frustum planes dirty ?
Definition camera.h:588
void set_projection_mode(projection_mode mode)
Sets the current projection mode for this camera (i.e. orthographic or perspective).
Definition camera.cpp:93
auto estimate_zoom_factor(const math::plane &plane) const -> float
Estimates the zoom factor based on the specified plane.
Definition camera.cpp:675
math::vec4 aa_data_
Anti-aliasing data.
Definition camera.h:541
auto get_prev_view_projection_relative() const -> math::transform
Definition camera.cpp:328
auto is_aspect_locked() const -> bool
Determines if the aspect ratio is currently being updated by the render driver.
Definition camera.cpp:190
void set_far_clip(float distance)
Sets the far plane distance.
Definition camera.cpp:127
void set_aa_data(const usize32_t &viewport_size, std::uint32_t temporal_frame_index, std::uint32_t temporal_aa_samples, taa_jitter_mode jitter_mode=taa_jitter_mode::progressive_golden, float jitter_amplitude=1.0f, float jitter_temporal_phase_scale=1.0f)
Sets the current jitter value for temporal anti-aliasing.
Definition camera.cpp:767
auto estimate_pick_tolerance(float pixel_tolerance, const math::vec3 &reference_position, const math::transform &object_transform) const -> math::vec3
Estimates the pick tolerance based on the pixel tolerance and reference position.
Definition camera.cpp:745
bool frustum_locked_
Is the frustum locked?
Definition camera.h:592
auto y_unit_axis() const -> math::vec3
Retrieves the y-axis unit vector of the camera's local coordinate system.
Definition camera.cpp:362
auto get_view_relative() const -> const math::transform &
Definition camera.cpp:288
void set_viewport_size(const usize32_t &viewport_size)
Sets the size of the viewport.
Definition camera.cpp:26
auto is_frustum_locked() const -> bool
Checks if the frustum is currently locked.
Definition camera.cpp:195
auto z_unit_axis() const -> math::vec3
Retrieves the z-axis unit vector of the camera's local coordinate system.
Definition camera.cpp:367
math::transform last_view_relative_
Definition camera.h:553
auto viewport_to_camera(const math::vec3 &point, math::vec3 &position_out) const -> bool
Converts a screen position into a camera space position at the near plane.
Definition camera.cpp:661
void set_aspect_ratio(float aspect, bool locked=false)
Sets the aspect ratio to be used for generating the horizontal FOV angle (perspective only).
Definition camera.cpp:167
auto get_prev_view() const -> const math::transform &
Definition camera.cpp:283
void lock_frustum(bool locked)
Locks or unlocks the frustum.
Definition camera.cpp:200
float fov_
Vertical degrees angle (perspective only).
Definition camera.h:568
auto get_view_inverse() const -> const math::transform &
Definition camera.cpp:298
auto get_position() const -> const math::vec3 &
Retrieves the current position of the camera.
Definition camera.cpp:353
void set_viewport_pos(const ipoint32_t &viewport_pos)
Sets the position of the viewport.
Definition camera.cpp:32
auto world_to_viewport(const math::vec3 &pos) const -> math::vec3
Transforms a point from world space into screen space.
Definition camera.cpp:473
auto classify_aabb(const math::bbox &bounds) const -> math::volume_query
Determines if the specified AABB falls within the frustum.
Definition camera.cpp:424
auto get_aspect_ratio() const -> float
Retrieves the aspect ratio used to generate the horizontal FOV angle.
Definition camera.cpp:185
auto project_to_quad(const math::vec2 &viewport_point, const math::transform &quad_transform, uint32_t quad_width, uint32_t quad_height, math::vec2 &pixel_out) const -> bool
Raycasts from viewport point onto a quad plane and returns pixel coordinates.
Definition camera.cpp:582
auto get_ortho_size() const -> float
Retrieves the orthographic size.
Definition camera.cpp:74
bool aspect_dirty_
Has the aspect ratio changed?
Definition camera.h:586
usize32_t viewport_size_
Viewport size.
Definition camera.h:580
auto get_view_projection() const -> math::transform
Retrieves the current view-projection matrix.
Definition camera.cpp:308
auto get_prev_projection() const -> const math::transform &
Definition camera.cpp:273
void record_current_matrices()
Makes a copy of the current view and projection matrices before they are changed.
Definition camera.cpp:759
math::transform taa_prev_projection_
Definition camera.h:560
math::transform last_projection_
Cached "previous" projection matrix.
Definition camera.h:556
void set_near_clip(float distance)
Sets the near plane distance.
Definition camera.cpp:107
math::transform view_inverse_
Definition camera.h:544
auto get_view_inverse_relative() const -> const math::transform &
Definition camera.cpp:303
auto get_frustum() const -> const math::frustum &
Retrieves the current camera object frustum.
Definition camera.cpp:372
auto get_aa_data() const -> const math::vec4 &
Retrieves the anti-aliasing data.
Definition camera.cpp:858
auto get_near_clip() const -> float
Retrieves the distance from the camera to the near clip plane.
Definition camera.cpp:64
math::vec3 position
Definition defaults.cpp:52
transform_t< float > transform
volume_query
Definition math_types.h:13
projection_mode
Enum representing the projection mode of a camera.
Definition camera.h:16
taa_jitter_mode
Subpixel jitter sequence for temporal AA (see camera::set_aa_data).
Definition camera.h:25
@ r2_low_discrepancy
R2 / recurrence lattice pair; alternative progressive 2D coverage.
@ progressive_golden
Kronecker / golden-ratio; smooth incommensurable steps (default).
@ halton_2_3
Halton(base 2,3) in [-0.5,0.5]; strong low-discrepancy, larger frame-to-frame steps.
#define SERIALIZABLE(T)
Storage for box vector values and wraps up common functionality.
Definition bbox.h:21
Storage for infinite plane.
Definition plane.h:21
Structure for storing camera related context.
Definition camera.h:52
rtti::context ctx
RTTI context for the camera.
Definition camera.h:53