Skip to main content

camera.ixx File

Camera transform helpers. More...

Included Headers

#include <cmath> #include <helios.math.utils> #include <helios.math.types>

Namespaces Index

namespacehelios
namespacemath

Mathematical operations and types. More...

Description

Camera transform helpers.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file camera.ixx
3 * @brief Camera transform helpers.
4 */
5module;
6
7#include <cmath>
8
9export module helios.math.transform:camera;
10
11import helios.math.types;
12import helios.math.utils;
13
14
15export namespace helios::math {
16
17 /**
18 * @brief Computes the 4x4 perspective projection matrix based on the specified
19 * field of view, aspect ratio, and near and far clipping planes.
20 *
21 * This method returns a matrix suitable for projecting 3D points onto a
22 * 2D screen from the perspective of a camera. The perspective transformation
23 * maps 3D points inside the specified viewing frustum to normalized device
24 * coordinates, which are then clipped and transformed to screen space.
25 *
26 * @param fovY Field of view in the Y direction, specified in radians.
27 * @param aspect The aspect ratio of the view, defined as width divided by height.
28 * @param zNear The distance to the near clipping plane. Must be greater than zero.
29 * @param zFar The distance to the far clipping plane. Must be greater than zNear.
30 *
31 * @return A 4x4 matrix representing the perspective projection transformation.
32 *
33 * @see https://thorsten.suckow-homberg.de/docs/articles/computer-graphics/from-camera-to-clip-space-derivation-of-the-projection-matrices
34 */
35 inline mat4f perspective(float fovY, float aspect, float zNear, float zFar) noexcept {
36
37 float f = 1 / std::tan(fovY/2);
38
39 return mat4f{
40 f/aspect, 0.0f, 0.0f, 0.0f,
41 0.0f, f, 0.0f, 0.0f,
42 0.0f, 0.0f , -(zFar + zNear)/(zFar - zNear), -1.0f,
43 0.0f, 0.0f, -2 * (zFar * zNear) / (zFar - zNear), 0.0f
44 };
45 }
46
47
48 /**
49 * @brief Computes the 4x4 orthographic projection matrix.
50 *
51 * Creates a projection matrix that maps 3D coordinates to normalized device
52 * coordinates without perspective distortion. Parallel lines remain parallel,
53 * and object size does not change with distance from the camera.
54 *
55 * Suitable for 2D rendering, UI elements, and technical visualizations.
56 *
57 * @param left The left boundary of the view volume.
58 * @param right The right boundary of the view volume.
59 * @param bottom The bottom boundary of the view volume.
60 * @param top The top boundary of the view volume.
61 * @param zNear The distance to the near clipping plane (default: 0.1).
62 * @param zFar The distance to the far clipping plane (default: 100.0).
63 *
64 * @return A 4x4 orthographic projection matrix.
65 *
66 * @see perspective()
67 */
68 inline mat4f ortho(
69 const float left , const float right,
70 const float bottom, const float top,
71 const float zNear = 0.1f, const float zFar = 100.0f) noexcept {
72
73 return mat4f{
74 2.0f/(right - left), 0.0f,0.0f,0.0f,
75 0.0f, 2.0f/(top-bottom),0.0f,0.0f,
76 0.0f, 0.0f, -2.0f/(zFar - zNear),0.0f,
77 -(right +left)/(right-left),-(top+bottom)/(top-bottom),-(zFar+zNear)/(zFar-zNear),1.0f
78 };
79 }
80
81
82 /**
83 * @brief Returns the 4x4 lookAt-matrix for transforming world coordinates
84 * to camera space.
85 * The method creates the camera coordinate system based on the
86 * parameters eye, i.e. the vantage point in world space, the camera's
87 * up direction and center, representing the point of interest.
88 * Note that the returned matrix is essentially a composition of
89 * a change of coordinates matrix P(C <- W) and a translation
90 * to the origin, i.e. the camera is sitting at the origin (0, 0, 0)
91 * looking down the negative z-axis, i.e. conceptually
92 *
93 * P(W<-C) * T(-eye)
94 *
95 * The 4x4 matrix can then be used for computing the perspective projection
96 * for creating the clip space
97 *
98 * clip = P * V * W * M
99 *
100 * @param eye
101 * @param center
102 * @param up
103 *
104 * @return
105 */
106 inline mat4f lookAt(const vec3f& eye, const vec3f& center, const vec3f& up) noexcept {
107 const auto z = (eye - center).normalize();
108 const auto x = cross(up, z).normalize();
109 const auto y = cross(z, x).normalize();
110
111 return mat4f{
112 x[0], y[0], z[0], 0.0f,
113 x[1], y[1], z[1], 0.0f,
114 x[2], y[2], z[2], 0.0f,
115 -dot(x, eye), -dot(y, eye), -dot(z, eye), 1.0f,
116 };
117 }
118
119}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.