28 const auto near = m_inv_view_projection *
math::fvec4{ndc[0], ndc[1], 1.0f, 1.0f};
32 return {origin, direction};
40 result.
x() = result.
x() * viewport[2] + viewport[0];
41 result.
y() = result.
y() * viewport[3] + viewport[1];
49 result[0] = ((window[0] - viewport[0]) / viewport[2]) * 2.0f - 1.0f;
50 result[1] = ((window[1] - viewport[1]) / viewport[3]) * 2.0f - 1.0f;
53 result[2] = 1.0f - window[2];
56 result = m_inv_view_projection * result;
64 m_orthographic =
false;
68 m_aspect_ratio = aspect_ratio;
73 if (m_clip_far == std::numeric_limits<float>::infinity())
83 m_view_projection = m_projection * m_view;
84 m_inv_view_projection = m_inv_view * m_inv_projection;
102 set_perspective(m_vertical_fov, aspect_ratio, m_clip_near, m_clip_far);
106 void camera::set_orthographic(
float clip_left,
float clip_right,
float clip_bottom,
float clip_top,
float clip_near,
float clip_far)
109 m_orthographic =
true;
112 m_clip_left = clip_left;
113 m_clip_right = clip_right;
114 m_clip_bottom = clip_bottom;
115 m_clip_top = clip_top;
116 m_clip_near = clip_near;
117 m_clip_far = clip_far;
120 std::tie(m_projection, m_inv_projection) =
math::ortho_half_z_inv(m_clip_left, m_clip_right, m_clip_bottom, m_clip_top, m_clip_far, m_clip_near);
123 m_view_projection = m_projection * m_view;
124 m_inv_view_projection = m_inv_view * m_inv_projection;
132 m_exposure_value = ev100;
134 m_exposure_normalization = 1.0f / (
std::exp2(m_exposure_value));
137 void camera::transformed()
147 m_view_projection = m_projection * m_view;
148 m_inv_view_projection = m_inv_view * m_inv_projection;
154 void camera::update_frustum()
157 m_view_frustum.
extract(m_view_projection);
174 for (std::size_t i = 0;
i < 8; ++
i)
176 const math::fvec4 frustum_corner = m_inv_view_projection * clip_space_cube[
i];
void set_aspect_ratio(float aspect_ratio)
Sets the camera's aspect ratio.
void set_vertical_fov(float vertical_fov)
Sets the camera's vertical field of view.
math::fvec3 project(const math::fvec3 &object, const math::fvec4 &viewport) const
Maps object coordinates to window coordinates.
math::fvec3 unproject(const math::fvec3 &window, const math::fvec4 &viewport) const
Maps window coordinates to object coordinates.
void set_orthographic(float clip_left, float clip_right, float clip_bottom, float clip_top, float clip_near, float clip_far)
Sets the camera's projection matrix using orthographic projection.
geom::ray< float, 3 > pick(const math::fvec2 &ndc) const
Constructs a picking ray from normalized device coordinates (NDC).
void set_exposure_value(float ev100)
Sets the camera's ISO 100 exposure value.
void set_perspective(float vertical_fov, float aspect_ratio, float near, float far=std::numeric_limits< float >::infinity())
Sets the camera's projection matrix using perspective projection.
constexpr const vector_type & get_translation() const noexcept
Returns the translation of the object.
constexpr const quaternion_type & get_rotation() const noexcept
Returns the rotation of the object.
consteval T exp2(T x) noexcept
Compile-time exp2 for unsigned integrals.
T vertical_fov(T h, T r)
Calculates a vertical FoV given a horizontal FoV and aspect ratio.
constexpr std::tuple< mat4< T >, mat4< T > > ortho_half_z_inv(T left, T right, T bottom, T top, T near, T far) noexcept
Constructs an orthographic projection matrix which will transform the near and far clipping planes to...
quaternion< T > normalize(const quaternion< T > &q)
Normalizes a quaternion.
constexpr std::tuple< mat4< T >, mat4< T > > look_at_rh_inv(const vec3< T > &position, const vec3< T > &target, const vec3< T > &up)
Constructs a right-handed viewing transformation matrix and its inverse.
std::tuple< mat4< T >, mat4< T > > perspective_half_z_inv(T vertical_fov, T aspect_ratio, T near, T far)
Constructs a perspective projection matrix which will transform the near and far clipping planes to [...
std::tuple< mat4< T >, mat4< T > > inf_perspective_half_z_reverse_inv(T vertical_fov, T aspect_ratio, T near)
Constructs a perspective projection matrix, with an infinite far plane, which will transform the near...
void extend(const vector_type &point) noexcept
Extends the hyperrectangle to include a point.
Half of a line proceeding from an initial point.
void extract(const matrix_type &matrix) noexcept
Extracts the view frustum planes from a view-projection matrix.
constexpr element_type & x() noexcept
Returns a reference to the first element.
constexpr element_type & y() noexcept
Returns a reference to the second element.
static constexpr vector infinity() noexcept
Returns a vector of infinities, where every element is equal to infinity.