HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
perspective.hpp
1// Copyright Take Vos 2021-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "matrix3.hpp"
8#include "../utility/utility.hpp"
9#include "../macros.hpp"
10#include <exception>
11#include <compare>
12#include <cmath>
13
14hi_export_module(hikogui.geometry : perspective);
15
16hi_export namespace hi { inline namespace v1 {
17
22public:
23 perspective() = delete;
24 constexpr perspective(perspective const&) noexcept = default;
25 constexpr perspective(perspective&&) noexcept = default;
26 constexpr perspective& operator=(perspective const&) noexcept = default;
27 constexpr perspective& operator=(perspective&&) noexcept = default;
28
37 perspective(float fov_y, float aspect_ratio, float znear, float zfar) noexcept :
38 _tan_half_fov_y(std::tan(fov_y * 0.5f)), _aspect_ratio(aspect_ratio), _znear(znear), _zfar(zfar)
39 {
40 hi_axiom(fov_y > std::numeric_limits<float>::epsilon());
41 hi_axiom(aspect_ratio > std::numeric_limits<float>::epsilon());
42 }
43
52 perspective(float fov_y, extent2 view_port, float znear, float zfar) noexcept :
53 perspective(fov_y, view_port.width() / view_port.height(), znear, zfar)
54 {
55 }
56
57 [[nodiscard]] constexpr operator matrix3() noexcept
58 {
59 auto const a = _aspect_ratio;
60 auto const t = _tan_half_fov_y;
61 auto const f = _zfar;
62 auto const n = _znear;
63
64 // clang-format off
65 return {
66 1.0f / (a * t), 0.0f , 0.0f , 0.0f,
67 0.0f , 1.0f / t, 0.0f , 0.0f,
68 0.0f , 0.0f , f / (n - f), -(f * n) / (f - n),
69 0.0f , 0.0f , -1.0f , 0.0f
70 };
71 // clang-format on
72 }
73
74private:
75 float _tan_half_fov_y;
76 float _aspect_ratio;
77 float _znear;
78 float _zfar;
79};
80
81}} // namespace hi::v1
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A high-level geometric extent.
Definition extent2.hpp:32
A 2D or 3D homogenius matrix for transforming homogenious vectors and points.
Definition matrix3.hpp:36
Perspective transform.
Definition perspective.hpp:21
perspective(float fov_y, float aspect_ratio, float znear, float zfar) noexcept
Create a right-handed perspective transform.
Definition perspective.hpp:37
perspective(float fov_y, extent2 view_port, float znear, float zfar) noexcept
Create a right-handed perspective transform.
Definition perspective.hpp:52
T tan(T... args)