Euphoria
mat4.h
Go to the documentation of this file.
1#pragma once
2
3#include "eu/base/vec3.h"
4#include "eu/base/vec4.h"
5#include "eu/base/angle.h"
6#include "eu/base/axisangle.h"
7
8
9namespace eu
10{
14
15 struct Q;
16
17 struct Rui
18 {
19 float right = 0;
20 float up = 0;
21 float in = 0;
22 };
23
25 struct m4
26 {
28 [[nodiscard]] static m4 from_col_major(
29 float t00, float t01, float t02, float t03,
30 float t10, float t11, float t12, float t13,
31 float t20, float t21, float t22, float t23,
32 float t30, float t31, float t32, float t33);
33
35 [[nodiscard]] constexpr static m4 from_row_major(
36 float t00, float t10, float t20, float t30,
37 float t01, float t11, float t21, float t31,
38 float t02, float t12, float t22, float t32,
39 float t03, float t13, float t23, float t33)
40 {
41 return {
42 t00, t01, t02, t03,
43 t10, t11, t12, t13,
44 t20, t21, t22, t23,
45 t30, t31, t32, t33};
46 }
47
50 [[nodiscard]] static m4 from_major(const v4 &major);
51
54 [[nodiscard]] constexpr static m4 from_scalar(float scalar)
55 {
56 const float z = 0;
57 return from_row_major(
58 scalar, z, z, z,
59 z, scalar, z, z,
60 z, z, scalar, z,
61 z, z, z, scalar);
62 }
63
64 [[nodiscard]] static m4 from_basis(const n3& a, const n3& b, const n3& c);
65 [[nodiscard]] static m4 from_scale(const v3& s);
66
68 [[nodiscard]] static m4 from_translation(const v3 &v);
69
71 [[nodiscard]] static m4 from_rot_x(const An &a);
72
74 [[nodiscard]] static m4 from_rot_y(const An &a);
75
77 [[nodiscard]] static m4 from_rot_z(const An &a);
78
80 [[nodiscard]] static m4 from(const AA &aa);
81
83 [[nodiscard]] static m4 from(const Q& q);
84
94 [[nodiscard]] static m4 create_ortho_lrud(float l, float r, float t, float b, float n, float f);
95
97 [[nodiscard]] static m4 create_perspective(const An &fov, float aspect_ratio, float near, float far);
98
101
103 [[nodiscard]] const float* get_column_major_data_ptr() const;
104
107 bool invert();
108
111 [[nodiscard]] m4 get_inverted() const;
112
114 [[nodiscard]] float get(int row, int col) const;
115
117 [[nodiscard]] float get1(int row, int col) const;
118
120 [[nodiscard]] v4 get_transformed(const v4 &p) const;
121
123 [[nodiscard]] v3 get_transformed_point(const v3 &p) const;
124
126 [[nodiscard]] v3 get_transformed(const Rui& rui) const;
127
129 [[nodiscard]] v3 get_transformed_vec(const v3 &p) const;
130
132 [[nodiscard]] n3 get_transformed_vec(const n3 &p) const;
133
135 [[nodiscard]] v4 get_row(int r) const;
136
138 [[nodiscard]] v4 get_column(int c) const;
139
141 [[nodiscard]] m4 get_translated(const v3 &t) const;
142
144 [[nodiscard]] m4 get_rotated(const AA &aa) const;
145
147 [[nodiscard]] v3 get_translation() const;
148
150 [[nodiscard]] v4 get_major() const;
151
153 [[nodiscard]] n3 get_x_axis() const;
154
156 [[nodiscard]] n3 get_y_axis() const;
157
159 [[nodiscard]] n3 get_z_axis() const;
160
163 [[nodiscard]] m4 get_transposed() const;
164
165
166 void operator+=(const m4 &rhs);
167 void operator-=(const m4 &rhs);
168
169
170 private:
172 float data[16];
173 m4() = default;
174
175 constexpr m4(
176 float t00, float t01, float t02, float t03,
177 float t10, float t11, float t12, float t13,
178 float t20, float t21, float t22, float t23,
179 float t30, float t31, float t32, float t33)
180 : data{
181 t00, t01, t02, t03,
182 t10, t11, t12, t13,
183 t20, t21, t22, t23,
184 t30, t31, t32, t33}
185 {
186 }
187 };
188
191
193 std::string string_from(const m4 &m);
194
195 m4 operator+(const m4 &lhs, const m4 &rhs);
196 m4 operator-(const m4 &lhs, const m4 &rhs);
197 m4 operator*(const m4 &lhs, const m4 &rhs);
198 v4 operator*(const m4 &lhs, const v4 &rhs);
199
201
203}
204
205ADD_DEFAULT_FORMATTER(eu::m4, std::string, eu::string_from);
#define ADD_CATCH_FORMATTER_DEF(TYPE)
Definition pch.public.h:42
constexpr m4 m4_identity
The identity matrix.
Definition mat4.h:190
An operator-(const An &lhs, const An &rhs)
An operator+(const An &lhs, const An &rhs)
An operator*(const An &lhs, float rhs)
std::string string_from(const An &a)
Definition assert.h:118
Axis + Angle.
Definition axisangle.h:19
An angle in both degrees and radians.
Definition angle.h:13
A quaternion representing a rotation in 3d.
Definition quat.h:19
Definition mat4.h:18
float up
Definition mat4.h:20
float in
Definition mat4.h:21
float right
Definition mat4.h:19
4x4 matrix
Definition mat4.h:26
static m4 from_rot_y(const An &a)
Create a rotation matrix, around the Y axis.
v4 get_column(int c) const
Get a column as vec4.
m4 get_transposed() const
Gets the transpose of a matrix.
n3 get_transformed_vec(const n3 &p) const
Get a transformed unit3.
void operator-=(const m4 &rhs)
v3 get_transformed_point(const v3 &p) const
Get a transformed vec3 assuming it's a point.
n3 get_z_axis() const
Get the local Z axis.
float get1(int row, int col) const
Return a single value given a row and column (note: one/1 based index).
bool invert()
Invert the current matrix.
v3 get_transformed(const Rui &rui) const
Get a transformed (right, up, in).
v4 get_row(int r) const
Get a row as a vec4.
static m4 from_basis(const n3 &a, const n3 &b, const n3 &c)
static m4 from_rot_x(const An &a)
Create a rotation matrix, around the X axis.
v3 get_transformed_vec(const v3 &p) const
Get a transformed vec3, assuming it's a normal vector.
static m4 create_ortho_lrud(float l, float r, float t, float b, float n, float f)
Create an orthographic projection matrix.
static m4 from_col_major(float t00, float t01, float t02, float t03, float t10, float t11, float t12, float t13, float t20, float t21, float t22, float t23, float t30, float t31, float t32, float t33)
Create a new matrix from column major format.
float get(int row, int col) const
Return a single value given a row and column (note: zero/0 based index).
static m4 from(const Q &q)
Create a rotation matrix, from a quaternion.
m4 get_inverted() const
Return the inverted matrix.
static m4 from_rot_z(const An &a)
Create a rotation matrix, around the Z axis.
void operator+=(const m4 &rhs)
static constexpr m4 from_row_major(float t00, float t10, float t20, float t30, float t01, float t11, float t21, float t31, float t02, float t12, float t22, float t32, float t03, float t13, float t23, float t33)
Create a new matrix from row major format.
Definition mat4.h:35
m4 get_rotated(const AA &aa) const
Combine this with a rotation matrix.
static constexpr m4 from_scalar(float scalar)
Create a matrix from a single scalar.
Definition mat4.h:54
v4 get_major() const
Get the major vector.
float * get_column_major_data_ptr()
Get a direct pointer to the data in column major format, for API integration.
static m4 from_scale(const v3 &s)
static m4 create_perspective(const An &fov, float aspect_ratio, float near, float far)
Create a perspective projection matrix.
static m4 from_translation(const v3 &v)
Create a translation matrix.
m4 get_translated(const v3 &t) const
Combine this with a translation matrix.
v4 get_transformed(const v4 &p) const
Get a transformed vec4.
const float * get_column_major_data_ptr() const
Get a direct pointer to the const data in column major format, for API integration.
v3 get_translation() const
Get the current translation of the transformation matrix.
static m4 from_major(const v4 &major)
Create a matrix from the major, aka the diagonal.
n3 get_x_axis() const
Get the local X axis.
static m4 from(const AA &aa)
Create a rotation matrix, from an axis angle.
n3 get_y_axis() const
Get the local Y axis.
a 3d unit (vector)
Definition vec3.h:94
a 3d vector
Definition vec3.h:27
A 4d vector.
Definition vec4.h:17