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
28
34class camera : public crtp_meta_type<camera>
35{
36public:
38
39
45
51 void set_fov(float degrees);
52
58 void set_near_clip(float distance);
59
65 void set_far_clip(float distance);
66
72 void set_orthographic_size(float size);
73
80
86 auto get_fov() const -> float;
87
93 auto get_near_clip() const -> float;
94
100 auto get_far_clip() const -> float;
101
107 auto get_ortho_size() const -> float;
108
114 auto get_zoom_factor() const -> float;
115
121 auto get_ppu() const -> float;
122
128 void set_viewport_size(const usize32_t& viewportSize);
129
135 void set_viewport_pos(const upoint32_t& viewportPos);
136
142 auto get_viewport_size() const -> const usize32_t&;
143
149 auto get_viewport_pos() const -> const upoint32_t&;
150
157 void set_aspect_ratio(float aspect, bool locked = false);
158
164 auto get_aspect_ratio() const -> float;
165
171 auto is_aspect_locked() const -> bool;
172
178 auto is_frustum_locked() const -> bool;
179
185 void lock_frustum(bool locked);
186
192 auto get_frustum() const -> const math::frustum&;
193
199 auto get_clipping_volume() const -> const math::frustum&;
200
206 auto get_projection() const -> const math::transform&;
207 auto get_prev_projection() const -> const math::transform&;
208
214 auto get_view() const -> const math::transform&;
215 auto get_prev_view() const -> const math::transform&;
216 auto get_view_inverse() const -> const math::transform&;
217 auto get_view_relative() const -> const math::transform&;
218 auto get_prev_view_relative() const -> const math::transform&;
219 auto get_view_inverse_relative() const -> const math::transform&;
232 auto get_view_projection() const -> math::transform;
234
239
247 void set_aa_data(const usize32_t& viewportSize,
248 std::uint32_t currentSubpixelIndex,
249 std::uint32_t temporalAASamples);
250
256 auto get_aa_data() const -> const math::vec4&;
257
264 auto classify_aabb(const math::bbox& bounds) const -> math::volume_query;
265
272 auto test_aabb(const math::bbox& bounds) const -> bool;
273
281 auto classify_obb(const math::bbox& bounds, const math::transform& t) const -> math::volume_query;
282
290 auto test_obb(const math::bbox& bounds, const math::transform& t) const -> bool;
291
292 auto test_billboard(float size, const math::transform& t) const -> bool;
293
294
303 auto viewport_to_ray(const math::vec2& point, math::vec3& vec_ray_start, math::vec3& vec_ray_dir) const -> bool;
304
314 auto viewport_to_world(const math::vec2& point, const math::plane& plane, math::vec3& position_out, bool clip) const
315 -> bool;
316
326 auto viewport_to_major_axis(const math::vec2& point,
327 const math::vec3& axis_origin,
328 math::vec3& position_out,
329 math::vec3& major_axis_out) const -> bool;
330
341 auto viewport_to_major_axis(const math::vec2& point,
342 const math::vec3& axis_origin,
343 const math::vec3& align_normal,
344 math::vec3& position_out,
345 math::vec3& major_axis_out) const -> bool;
346
354 auto viewport_to_camera(const math::vec3& point, math::vec3& position_out) const -> bool;
355
362 auto world_to_viewport(const math::vec3& pos) const -> math::vec3;
363
370 auto estimate_zoom_factor(const math::plane& plane) const -> float;
371
378 auto estimate_zoom_factor(const math::vec3& position) const -> float;
379
387 auto estimate_zoom_factor(const math::plane& plane, float maximum_value) const -> float;
388
396 auto estimate_zoom_factor(const math::vec3& position, float maximum_value) const -> float;
397
406 auto estimate_pick_tolerance(float pixel_tolerance,
407 const math::vec3& reference_position,
408 const math::transform& object_transform) const -> math::vec3;
409
416 void look_at(const math::vec3& eye, const math::vec3& at);
417
425 void look_at(const math::vec3& eye, const math::vec3& at, const math::vec3& vUp);
426
432 auto get_position() const -> const math::vec3&;
433
439 auto x_unit_axis() const -> math::vec3;
440
446 auto y_unit_axis() const -> math::vec3;
447
453 auto z_unit_axis() const -> math::vec3;
454
461
465 void touch();
466
474 static auto get_face_camera(std::uint32_t face, const math::transform& transform) -> camera;
475
476protected:
478 math::vec4 aa_data_ = {0.0f, 0.0f, 0.0f, 0.0f};
482
485
491
501 float fov_ = 60.0f;
503 float near_clip_ = 0.1f;
505 float far_clip_ = 1000.0f;
507 float ortho_size_ = 5;
509 float aspect_ratio_ = 1.0f;
515 bool view_dirty_ = true;
517 mutable bool projection_dirty_ = true;
519 mutable bool aspect_dirty_ = true;
521 mutable bool frustum_dirty_ = true;
523 bool aspect_locked_ = false;
525 bool frustum_locked_ = false;
526};
527} // 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:35
auto get_view_projection_relative() const -> math::transform
Definition camera.cpp:316
auto get_far_clip() const -> float
Retrieves the distance from the camera to the far clip plane.
Definition camera.cpp:67
float far_clip_
Far clip plane Distance.
Definition camera.h:505
auto get_fov() const -> float
Retrieves the current field of view angle in degrees.
Definition camera.cpp:57
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:435
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:575
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:523
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:490
auto get_prev_view_projection() const -> math::transform
Retrieves the previous view matrix.
Definition camera.cpp:311
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:444
auto get_prev_view_relative() const -> const math::transform &
Definition camera.cpp:291
void set_fov(float degrees)
Sets the field of view angle of this camera (perspective only).
Definition camera.cpp:77
auto get_projection_mode() const -> projection_mode
Retrieves the current projection mode for this camera.
Definition camera.cpp:52
void look_at(const math::vec3 &eye, const math::vec3 &at)
Sets the camera to look at a specified target.
Definition camera.cpp:326
void set_orthographic_size(float size)
Sets the half of the vertical size of the viewing volume in world units.
Definition camera.cpp:45
bool aspect_locked_
Should the aspect ratio be automatically updated by the render driver?
Definition camera.h:523
math::transform view_relative_
Definition camera.h:483
math::frustum clipping_volume_
The near clipping volume (area of space between the camera position and the near plane).
Definition camera.h:497
auto get_ppu() const -> float
Retrieves the pixels per unit (PPU).
Definition camera.cpp:19
auto get_viewport_size() const -> const usize32_t &
Retrieves the size of the viewport.
Definition camera.cpp:35
auto x_unit_axis() const -> math::vec3
Retrieves the x-axis unit vector of the camera's local coordinate system.
Definition camera.cpp:351
projection_mode projection_mode_
The type of projection currently selected for this camera.
Definition camera.h:499
bool view_dirty_
View matrix dirty ?
Definition camera.h:515
auto get_projection() const -> const math::transform &
Retrieves the current projection matrix.
Definition camera.cpp:203
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:405
auto get_zoom_factor() const -> float
Retrieves the zoom factor.
Definition camera.cpp:9
math::frustum frustum_
Details regarding the camera frustum.
Definition camera.h:495
float aspect_ratio_
The aspect ratio used to generate the correct horizontal degrees (perspective only)
Definition camera.h:509
auto get_local_bounding_box() -> math::bbox
Retrieves the bounding box of this object.
Definition camera.cpp:145
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:822
void touch()
Marks the camera as modified.
Definition camera.cpp:813
float near_clip_
Near clip plane Distance.
Definition camera.h:503
bool projection_dirty_
Projection matrix dirty ?
Definition camera.h:517
auto get_view() const -> const math::transform &
Retrieves the current view matrix.
Definition camera.cpp:276
math::transform view_inverse_relative_
Definition camera.h:484
void set_aa_data(const usize32_t &viewportSize, std::uint32_t currentSubpixelIndex, std::uint32_t temporalAASamples)
Sets the current jitter value for temporal anti-aliasing.
Definition camera.cpp:732
math::transform projection_
Cached projection matrix.
Definition camera.h:487
float ortho_size_
camera's half-size when in orthographic mode.
Definition camera.h:507
math::transform last_view_
Cached "previous" view matrix.
Definition camera.h:489
auto test_aabb(const math::bbox &bounds) const -> bool
Tests if the specified AABB is within the frustum.
Definition camera.cpp:426
void set_viewport_pos(const upoint32_t &viewportPos)
Sets the position of the viewport.
Definition camera.cpp:30
auto test_billboard(float size, const math::transform &t) const -> bool
Definition camera.cpp:453
math::transform view_
Cached view matrix.
Definition camera.h:480
bool frustum_dirty_
Are the frustum planes dirty ?
Definition camera.h:521
void set_projection_mode(projection_mode mode)
Sets the current projection mode for this camera (i.e. orthographic or perspective).
Definition camera.cpp:91
auto estimate_zoom_factor(const math::plane &plane) const -> float
Estimates the zoom factor based on the specified plane.
Definition camera.cpp:640
math::vec4 aa_data_
Anti-aliasing data.
Definition camera.h:478
auto get_prev_view_projection_relative() const -> math::transform
Definition camera.cpp:321
auto is_aspect_locked() const -> bool
Determines if the aspect ratio is currently being updated by the render driver.
Definition camera.cpp:188
void set_far_clip(float distance)
Sets the far plane distance.
Definition camera.cpp:125
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:710
bool frustum_locked_
Is the frustum locked?
Definition camera.h:525
auto y_unit_axis() const -> math::vec3
Retrieves the y-axis unit vector of the camera's local coordinate system.
Definition camera.cpp:355
auto get_view_relative() const -> const math::transform &
Definition camera.cpp:286
auto is_frustum_locked() const -> bool
Checks if the frustum is currently locked.
Definition camera.cpp:193
auto z_unit_axis() const -> math::vec3
Retrieves the z-axis unit vector of the camera's local coordinate system.
Definition camera.cpp:360
math::transform last_view_relative_
Definition camera.h:490
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:626
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:165
auto get_prev_view() const -> const math::transform &
Definition camera.cpp:281
void lock_frustum(bool locked)
Locks or unlocks the frustum.
Definition camera.cpp:198
float fov_
Vertical degrees angle (perspective only).
Definition camera.h:501
auto get_view_inverse() const -> const math::transform &
Definition camera.cpp:296
auto get_position() const -> const math::vec3 &
Retrieves the current position of the camera.
Definition camera.cpp:346
auto world_to_viewport(const math::vec3 &pos) const -> math::vec3
Transforms a point from world space into screen space.
Definition camera.cpp:466
auto classify_aabb(const math::bbox &bounds) const -> math::volume_query
Determines if the specified AABB falls within the frustum.
Definition camera.cpp:417
auto get_aspect_ratio() const -> float
Retrieves the aspect ratio used to generate the horizontal FOV angle.
Definition camera.cpp:183
upoint32_t viewport_pos_
Viewport position.
Definition camera.h:511
auto get_ortho_size() const -> float
Retrieves the orthographic size.
Definition camera.cpp:72
bool aspect_dirty_
Has the aspect ratio changed?
Definition camera.h:519
usize32_t viewport_size_
Viewport size.
Definition camera.h:513
auto get_viewport_pos() const -> const upoint32_t &
Retrieves the position of the viewport.
Definition camera.cpp:40
auto get_view_projection() const -> math::transform
Retrieves the current view-projection matrix.
Definition camera.cpp:306
auto get_prev_projection() const -> const math::transform &
Definition camera.cpp:271
void record_current_matrices()
Makes a copy of the current view and projection matrices before they are changed.
Definition camera.cpp:724
math::transform last_projection_
Cached "previous" projection matrix.
Definition camera.h:493
void set_viewport_size(const usize32_t &viewportSize)
Sets the size of the viewport.
Definition camera.cpp:24
void set_near_clip(float distance)
Sets the near plane distance.
Definition camera.cpp:105
math::transform view_inverse_
Definition camera.h:481
auto get_view_inverse_relative() const -> const math::transform &
Definition camera.cpp:301
auto get_frustum() const -> const math::frustum &
Retrieves the current camera object frustum.
Definition camera.cpp:365
auto get_aa_data() const -> const math::vec4 &
Retrieves the anti-aliasing data.
Definition camera.cpp:808
auto get_near_clip() const -> float
Retrieves the distance from the camera to the near clip plane.
Definition camera.cpp:62
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
float distance
#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:25
rtti::context ctx
RTTI context for the camera.
Definition camera.h:26