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
18 struct m4
19 {
21 [[nodiscard]] static m4 from_col_major(
22 float t00, float t01, float t02, float t03,
23 float t10, float t11, float t12, float t13,
24 float t20, float t21, float t22, float t23,
25 float t30, float t31, float t32, float t33);
26
28 [[nodiscard]] constexpr static m4 from_row_major(
29 float t00, float t10, float t20, float t30,
30 float t01, float t11, float t21, float t31,
31 float t02, float t12, float t22, float t32,
32 float t03, float t13, float t23, float t33)
33 {
34 return {
35 t00, t01, t02, t03,
36 t10, t11, t12, t13,
37 t20, t21, t22, t23,
38 t30, t31, t32, t33};
39 }
40
43 [[nodiscard]] static m4 from_major(const v4 &major);
44
47 [[nodiscard]] constexpr static m4 from_scalar(float scalar)
48 {
49 const float z = 0;
50 return from_row_major(
51 scalar, z, z, z,
52 z, scalar, z, z,
53 z, z, scalar, z,
54 z, z, z, scalar);
55 }
56
57 [[nodiscard]] static m4 from_basis(const n3& a, const n3& b, const n3& c);
58 [[nodiscard]] static m4 from_scale(const v3& s);
59
61 [[nodiscard]] static m4 from_translation(const v3 &v);
62
64 [[nodiscard]] static m4 from_rot_x(const An &a);
65
67 [[nodiscard]] static m4 from_rot_y(const An &a);
68
70 [[nodiscard]] static m4 from_rot_z(const An &a);
71
73 [[nodiscard]] static m4 from(const AA &aa);
74
76 [[nodiscard]] static std::optional<m4> from(const Q& q);
77
87 [[nodiscard]] static m4 create_ortho_lrud(float l, float r, float t, float b, float n, float f);
88
90 [[nodiscard]] static m4 create_perspective(const An &fov, float aspect_ratio, float near, float far);
91
94
96 [[nodiscard]] const float* get_column_major_data_ptr() const;
97
100 bool invert();
101
104 [[nodiscard]] m4 get_inverted() const;
105
107 [[nodiscard]] float get(int row, int col) const;
108
110 [[nodiscard]] float get1(int row, int col) const;
111
113 [[nodiscard]] v4 get_transformed(const v4 &p) const;
114
116 [[nodiscard]] v3 get_transformed_point(const v3 &p) const;
117
119 [[nodiscard]] v3 get_transformed_vec(const v3 &p) const;
120
122 [[nodiscard]] n3 get_transformed_vec(const n3 &p) const;
123
125 [[nodiscard]] v4 get_row(int r) const;
126
128 [[nodiscard]] v4 get_column(int c) const;
129
131 [[nodiscard]] m4 get_translated(const v3 &t) const;
132
134 [[nodiscard]] m4 get_rotated(const AA &aa) const;
135
137 [[nodiscard]] v3 get_translation() const;
138
140 [[nodiscard]] v4 get_major() const;
141
143 [[nodiscard]] n3 get_x_axis() const;
144
146 [[nodiscard]] n3 get_y_axis() const;
147
149 [[nodiscard]] n3 get_z_axis() const;
150
153 [[nodiscard]] m4 get_transposed() const;
154
155
156 void operator+=(const m4 &rhs);
157 void operator-=(const m4 &rhs);
158
159 private:
161 float data[16];
162
163 m4() = default;
164
165 constexpr m4(
166 float t00, float t01, float t02, float t03,
167 float t10, float t11, float t12, float t13,
168 float t20, float t21, float t22, float t23,
169 float t30, float t31, float t32, float t33)
170 : data{
171 t00, t01, t02, t03,
172 t10, t11, t12, t13,
173 t20, t21, t22, t23,
174 t30, t31, t32, t33}
175 {
176 }
177 };
178
181
183 std::string string_from(const m4 &m);
184
185 m4 operator+(const m4 &lhs, const m4 &rhs);
186 m4 operator-(const m4 &lhs, const m4 &rhs);
187 m4 operator*(const m4 &lhs, const m4 &rhs);
188 v4 operator*(const m4 &lhs, const v4 &rhs);
189
191
193}
194
195ADD_DEFAULT_FORMATTER(eu::m4, std::string, eu::string_from);
#define ADD_CATCH_FORMATTER_DEF(TYPE)
Definition pch.public.h:22
constexpr m4 m4_identity
The identity matrix.
Definition mat4.h:180
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
4x4 matrix
Definition mat4.h:19
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 azis.
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.
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).
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:28
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:47
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.
static std::optional< m4 > from(const Q &q)
Create a rotation matrix, from a quaternion.
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