Skip to main content

camera.ixx File

Camera transform helpers. More...

Included Headers​

#include <cmath> #include <array> #include <utility> #include <helios.math.utils> #include <helios.math.TransformType> #include <helios.math.types>

Namespaces Index​

namespacehelios
namespacemath

Classes Index​

structFrustumPlane

Struct for providing FrustumPlane information, i.e. it's normal (facing inward) and the distance of the plane to the origin. More...

Description​

Camera transform helpers.

File Listing​

The file content with the documentation metadata removed is:

1
5module;
6
7#include <cmath>
8#include <array>
9#include <utility>
10
11export module helios.math.transform:camera;
12
13import helios.math.types;
14import helios.math.TransformType;
15import helios.math.utils;
16
17
18export namespace helios::math {
19
23 enum class FrustumPlanePosition : uint8_t {
24 Near,
25 Far,
26 Left,
27 Right,
28 Top,
30 };
31
36 struct FrustumPlane {
38 float distance{};
39 };
40
41
60 inline mat4f perspective(const float fovY, const float aspect, const float zNear, const float zFar) noexcept {
61
62 const float f = 1 / std::tan(fovY/2);
63
64 return mat4f{
65 f/aspect, 0.0f, 0.0f, 0.0f,
66 0.0f, f, 0.0f, 0.0f,
67 0.0f, 0.0f , (zFar + zNear)/(zFar - zNear), 1.0f,
68 0.0f, 0.0f, - 2.0f * (zFar * zNear) / (zFar - zNear), 0.0f
69 };
70 }
71
72
95 inline mat4f ortho(
96 const float left , const float right,
97 const float bottom, const float top,
98 const float zNear = 0.1f, const float zFar = 100.0f) noexcept {
99
100 return mat4f{
101 2.0f/(right - left), 0.0f,0.0f,0.0f,
102 0.0f, 2.0f/(top-bottom),0.0f,0.0f,
103 0.0f, 0.0f, -2.0f/(zFar - zNear),0.0f,
104 -(right +left)/(right-left),-(top+bottom)/(top-bottom),-(zFar+zNear)/(zFar-zNear),1.0f
105 };
106 }
107
108
133 inline mat4f lookAt(const vec3f& eye, const vec3f& center, const vec3f& up) noexcept {
134 const auto z = (center - eye).normalize();
135 const auto x = cross(up, z).normalize();
136 const auto y = cross(z, x).normalize();
137
138 return mat4f{
139 x[0], y[0], z[0], 0.0f,
140 x[1], y[1], z[1], 0.0f,
141 x[2], y[2], z[2], 0.0f,
142 -dot(x, eye), -dot(y, eye), -dot(z, eye), 1.0f,
143 };
144 }
145
146
159 inline std::array<FrustumPlane, 6> frustumPlanes(
160 const float fovY, const float aspect, const float zNear, const float zFar, const helios::math::mat4f& viewMatrix) noexcept {
161
162 auto arr = std::array<FrustumPlane, 6>{};
163
164 const auto g = 1.0f / std::tan(fovY / 2.0f);
165 const auto s = aspect;
166
167 const auto translation = viewMatrix.translation().toVec4(1.0f);
168 const auto cameraRotation = viewMatrix.decompose(TransformType::Rotation).transpose();
169 const auto eye = (cameraRotation * translation).toVec3() * -1.0f;
170
171 const auto right = helios::math::vec3f{cameraRotation(0, 0), cameraRotation(1, 0), cameraRotation(2, 0)};
172 const auto up = helios::math::vec3f{cameraRotation(0, 1), cameraRotation(1, 1), cameraRotation(2, 1)};
173 const auto forward = helios::math::vec3f{cameraRotation(0, 2), cameraRotation(1, 2), cameraRotation(2, 2)};
174
175 static auto makePlane = [](helios::math::vec3f normal, const helios::math::vec3f& point) noexcept {
176 normal = normal.normalize();
177 return FrustumPlane{normal, dot(normal, point)};
178 };
179
180 arr[std::to_underlying(FrustumPlanePosition::Near)] = makePlane(forward, eye + forward * zNear);
181 arr[std::to_underlying(FrustumPlanePosition::Far)] = makePlane(forward * -1.0f, eye + forward * zFar);
182 arr[std::to_underlying(FrustumPlanePosition::Left)] = makePlane(g * right + s * forward, eye);
183 arr[std::to_underlying(FrustumPlanePosition::Right)] = makePlane(-g * right + s * forward, eye);
184 arr[std::to_underlying( FrustumPlanePosition::Bottom)] = makePlane(g * up + forward, eye);
185 arr[std::to_underlying(FrustumPlanePosition::Top)] = makePlane(-g * up + forward, eye);
186
187 return arr;
188 }
189
190}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.