MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ViewTransform.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <glm/gtc/matrix_transform.hpp>
4
5namespace MayaFlux::Kinesis {
6
7/**
8 * @struct ViewTransform
9 * @brief View and projection matrices as a named push constant slot
10 *
11 * Two glm::mat4 fields (128 bytes total) that map directly to a vertex
12 * shader push constant block. No Camera class, no scene graph, no
13 * transform hierarchy. The third dimension is not special: it is a
14 * push constant slot that the user may optionally supply, which
15 * enables depth testing and provides view/projection matrices to the
16 * vertex shader.
17 *
18 * Construction uses free functions (look_at, perspective, ortho) or
19 * direct assignment. Both matrices can be driven by signal graph node
20 * outputs for audio-reactive or algorithmically controlled viewpoints.
21 */
23 glm::mat4 view { 1.0F };
24 glm::mat4 projection { 1.0F };
25};
26
27static_assert(sizeof(ViewTransform) == 128,
28 "ViewTransform must be exactly 128 bytes (Vulkan minimum push constant size)");
29
30/**
31 * @brief Construct view matrix from eye position, target, and up vector
32 * @param eye Observer position in source space
33 * @param target Point the observer is looking at
34 * @param up Up direction (default: positive Y)
35 * @return ViewTransform with view matrix set, projection identity
36 */
37[[nodiscard]] inline ViewTransform look_at(
38 const glm::vec3& eye,
39 const glm::vec3& target,
40 const glm::vec3& up = glm::vec3(0.0F, 1.0F, 0.0F))
41{
42 return { .view = glm::lookAt(eye, target, up) };
43}
44
45/**
46 * @brief Construct perspective projection matrix
47 * @param fov_radians Vertical field of view in radians
48 * @param aspect Width / height aspect ratio
49 * @param near Near clip plane distance
50 * @param far Far clip plane distance
51 * @return ViewTransform with projection matrix set, view identity
52 */
53[[nodiscard]] inline ViewTransform perspective(
54 float fov_radians,
55 float aspect,
56 float near,
57 float far)
58{
59 return { .projection = glm::perspective(fov_radians, aspect, near, far) };
60}
61
62/**
63 * @brief Construct orthographic projection matrix
64 * @param left Left clipping plane
65 * @param right Right clipping plane
66 * @param bottom Bottom clipping plane
67 * @param top Top clipping plane
68 * @param near Near clipping plane (default: -1.0)
69 * @param far Far clipping plane (default: 1.0)
70 * @return ViewTransform with projection matrix set, view identity
71 */
72[[nodiscard]] inline ViewTransform ortho(
73 float left, float right,
74 float bottom, float top,
75 float near = -1.0F, float far = 1.0F)
76{
77 return { .projection = glm::ortho(left, right, bottom, top, near, far) };
78}
79
80/**
81 * @brief Construct complete ViewTransform from look-at and perspective parameters
82 * @param eye Observer position in source space
83 * @param target Point the observer is looking at
84 * @param fov_radians Vertical field of view in radians
85 * @param aspect Width / height aspect ratio
86 * @param near Near clip plane distance
87 * @param far Far clip plane distance
88 * @param up Up direction (default: positive Y)
89 * @return ViewTransform with both view and projection matrices set
90 */
92 const glm::vec3& eye,
93 const glm::vec3& target,
94 float fov_radians,
95 float aspect,
96 float near,
97 float far,
98 const glm::vec3& up = glm::vec3(0.0F, 1.0F, 0.0F))
99{
100 return {
101 .view = glm::lookAt(eye, target, up),
102 .projection = glm::perspective(fov_radians, aspect, near, far)
103 };
104}
105
106} // namespace MayaFlux::Kinesis
ViewTransform look_at(const glm::vec3 &eye, const glm::vec3 &target, const glm::vec3 &up=glm::vec3(0.0F, 1.0F, 0.0F))
Construct view matrix from eye position, target, and up vector.
ViewTransform look_at_perspective(const glm::vec3 &eye, const glm::vec3 &target, float fov_radians, float aspect, float near, float far, const glm::vec3 &up=glm::vec3(0.0F, 1.0F, 0.0F))
Construct complete ViewTransform from look-at and perspective parameters.
ViewTransform ortho(float left, float right, float bottom, float top, float near=-1.0F, float far=1.0F)
Construct orthographic projection matrix.
ViewTransform perspective(float fov_radians, float aspect, float near, float far)
Construct perspective projection matrix.
View and projection matrices as a named push constant slot.