Orrery
A GPU-accelerated N-body gravitational simulator
Loading...
Searching...
No Matches
matrix4.hpp
Go to the documentation of this file.
1#pragma once
2
34
35#include <array>
36#include <cstddef>
37
38#include "orrery/core/vec3.hpp"
39
40namespace orrery::viz {
41
48struct Vec4 {
49 float x{};
50 float y{};
51 float z{};
52 float w{};
53
54 [[nodiscard]] constexpr bool operator==(const Vec4&) const = default;
55};
56
58struct Mat4 {
59 std::array<float, 16> values{};
60
61 [[nodiscard]] constexpr float element(std::size_t row, std::size_t column) const noexcept {
62 return values[(column * 4) + row];
63 }
64
65 [[nodiscard]] constexpr bool operator==(const Mat4&) const = default;
66};
67
68[[nodiscard]] constexpr Mat4 identity() noexcept {
69 return Mat4{{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}};
70}
71
74[[nodiscard]] constexpr Mat4 operator*(const Mat4& a, const Mat4& b) noexcept {
75 Mat4 product;
76 for (std::size_t column = 0; column < 4; ++column) {
77 for (std::size_t row = 0; row < 4; ++row) {
78 float sum = 0;
79 for (std::size_t index = 0; index < 4; ++index) {
80 sum += a.element(row, index) * b.element(index, column);
81 }
82 product.values[(column * 4) + row] = sum;
83 }
84 }
85 return product;
86}
87
88[[nodiscard]] constexpr Vec4 operator*(const Mat4& matrix, const Vec4& vector) noexcept {
89 const std::array<float, 4> in{vector.x, vector.y, vector.z, vector.w};
90 std::array<float, 4> out{};
91 for (std::size_t row = 0; row < 4; ++row) {
92 for (std::size_t column = 0; column < 4; ++column) {
93 out[row] += matrix.element(row, column) * in[column];
94 }
95 }
96 return {out[0], out[1], out[2], out[3]};
97}
98
100[[nodiscard]] constexpr Vec4 homogeneous(core::Vec3 point) noexcept {
101 return {static_cast<float>(point.x), static_cast<float>(point.y), static_cast<float>(point.z),
102 1};
103}
104
114[[nodiscard]] Mat4 look_at(core::Vec3 eye, core::Vec3 centre, core::Vec3 up) noexcept;
115
123[[nodiscard]] Mat4 perspective(float vertical_field_of_view, float aspect_ratio, float near,
124 float far) noexcept;
125
126} // namespace orrery::viz
Mat4 perspective(float vertical_field_of_view, float aspect_ratio, float near, float far) noexcept
The perspective projection of a symmetric frustum.
constexpr Vec4 homogeneous(core::Vec3 point) noexcept
A point in world space, ready to be transformed.
Definition matrix4.hpp:100
Mat4 look_at(core::Vec3 eye, core::Vec3 centre, core::Vec3 up) noexcept
The view transform of a camera at eye looking at centre.
constexpr Mat4 operator*(const Mat4 &a, const Mat4 &b) noexcept
The transform that applies b and then a, as matrix products conventionally read.
Definition matrix4.hpp:74
A vector in three-dimensional Euclidean space.
Definition vec3.hpp:26
A 4-by-4 transform in column-major order.
Definition matrix4.hpp:58
A point or direction in homogeneous coordinates.
Definition matrix4.hpp:48
A three-component vector for interfaces, not for storage.