Wildvine Engine
Referencia Doxygen del codigo propio de Wildvine Engine.
Cargando...
Buscando...
Nada coincide
Matrix4x4.h
Ir a la documentación de este archivo.
1
6/*
7 * MIT License
8 *
9 * Copyright (c) 2024 Roberto Charreton
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in all
19 * copies or substantial portions of the Software.
20 *
21 * In addition, any project or software that uses this library or class must include
22 * the following acknowledgment in the credits:
23 *
24 * "This project uses software developed by Roberto Charreton and Attribute Overload."
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33*/
34#pragma once
35namespace EU {
42 class Matrix4x4 {
43 public:
44 float m[4][4];
46 // Default constructor initializes to identity matrix
48 for (int i = 0; i < 4; ++i) {
49 for (int j = 0; j < 4; ++j) {
50 m[i][j] = (i == j) ? 1.0f : 0.0f;
51 }
52 }
53 }
54
61 m[0][0] = 1; m[0][1] = 0; m[0][2] = 0; m[0][3] = 0;
62 m[1][0] = 0; m[1][1] = 1; m[1][2] = 0; m[1][3] = 0;
63 m[2][0] = 0; m[2][1] = 0; m[2][2] = 1; m[2][3] = 0;
64 m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
65 }
66
67
90 Matrix4x4(float a11, float a12, float a13, float a14,
91 float a21, float a22, float a23, float a24,
92 float a31, float a32, float a33, float a34,
93 float a41, float a42, float a43, float a44) {
94 m[0][0] = a11; m[0][1] = a12; m[0][2] = a13; m[0][3] = a14;
95 m[1][0] = a21; m[1][1] = a22; m[1][2] = a23; m[1][3] = a24;
96 m[2][0] = a31; m[2][1] = a32; m[2][2] = a33; m[2][3] = a34;
97 m[3][0] = a41; m[3][1] = a42; m[3][2] = a43; m[3][3] = a44;
98 }
99
100 // Copy constructor
101 Matrix4x4(const Matrix4x4& other) {
102 for (int i = 0; i < 4; ++i) {
103 for (int j = 0; j < 4; ++j) {
104 m[i][j] = other.m[i][j];
105 }
106 }
107 }
108
115 Matrix4x4 operator+(const Matrix4x4& other) const {
116 return Matrix4x4(
117 m[0][0] + other.m[0][0], m[0][1] + other.m[0][1], m[0][2] + other.m[0][2], m[0][3] + other.m[0][3],
118 m[1][0] + other.m[1][0], m[1][1] + other.m[1][1], m[1][2] + other.m[1][2], m[1][3] + other.m[1][3],
119 m[2][0] + other.m[2][0], m[2][1] + other.m[2][1], m[2][2] + other.m[2][2], m[2][3] + other.m[2][3],
120 m[3][0] + other.m[3][0], m[3][1] + other.m[3][1], m[3][2] + other.m[3][2], m[3][3] + other.m[3][3]
121 );
122 }
123
130 Matrix4x4 operator-(const Matrix4x4& other) const {
131 return Matrix4x4(
132 m[0][0] - other.m[0][0], m[0][1] - other.m[0][1], m[0][2] - other.m[0][2], m[0][3] - other.m[0][3],
133 m[1][0] - other.m[1][0], m[1][1] - other.m[1][1], m[1][2] - other.m[1][2], m[1][3] - other.m[1][3],
134 m[2][0] - other.m[2][0], m[2][1] - other.m[2][1], m[2][2] - other.m[2][2], m[2][3] - other.m[2][3],
135 m[3][0] - other.m[3][0], m[3][1] - other.m[3][1], m[3][2] - other.m[3][2], m[3][3] - other.m[3][3]
136 );
137 }
138
145 Matrix4x4 operator*(const Matrix4x4& other) const {
146 return Matrix4x4(
147 m[0][0] * other.m[0][0] + m[0][1] * other.m[1][0] + m[0][2] * other.m[2][0] + m[0][3] * other.m[3][0],
148 m[0][0] * other.m[0][1] + m[0][1] * other.m[1][1] + m[0][2] * other.m[2][1] + m[0][3] * other.m[3][1],
149 m[0][0] * other.m[0][2] + m[0][1] * other.m[1][2] + m[0][2] * other.m[2][2] + m[0][3] * other.m[3][2],
150 m[0][0] * other.m[0][3] + m[0][1] * other.m[1][3] + m[0][2] * other.m[2][3] + m[0][3] * other.m[3][3],
151
152 m[1][0] * other.m[0][0] + m[1][1] * other.m[1][0] + m[1][2] * other.m[2][0] + m[1][3] * other.m[3][0],
153 m[1][0] * other.m[0][1] + m[1][1] * other.m[1][1] + m[1][2] * other.m[2][1] + m[1][3] * other.m[3][1],
154 m[1][0] * other.m[0][2] + m[1][1] * other.m[1][2] + m[1][2] * other.m[2][2] + m[1][3] * other.m[3][2],
155 m[1][0] * other.m[0][3] + m[1][1] * other.m[1][3] + m[1][2] * other.m[2][3] + m[1][3] * other.m[3][3],
156
157 m[2][0] * other.m[0][0] + m[2][1] * other.m[1][0] + m[2][2] * other.m[2][0] + m[2][3] * other.m[3][0],
158 m[2][0] * other.m[0][1] + m[2][1] * other.m[1][1] + m[2][2] * other.m[2][1] + m[2][3] * other.m[3][1],
159 m[2][0] * other.m[0][2] + m[2][1] * other.m[1][2] + m[2][2] * other.m[2][2] + m[2][3] * other.m[3][2],
160 m[2][0] * other.m[0][3] + m[2][1] * other.m[1][3] + m[2][2] * other.m[2][3] + m[2][3] * other.m[3][3],
161
162 m[3][0] * other.m[0][0] + m[3][1] * other.m[1][0] + m[3][2] * other.m[2][0] + m[3][3] * other.m[3][0],
163 m[3][0] * other.m[0][1] + m[3][1] * other.m[1][1] + m[3][2] * other.m[2][1] + m[3][3] * other.m[3][1],
164 m[3][0] * other.m[0][2] + m[3][1] * other.m[1][2] + m[3][2] * other.m[2][2] + m[3][3] * other.m[3][2],
165 m[3][0] * other.m[0][3] + m[3][1] * other.m[1][3] + m[3][2] * other.m[2][3] + m[3][3] * other.m[3][3]
166 );
167 }
168
174 float determinant() const {
175 return
176 m[0][0] * (
177 m[1][1] * (m[2][2] * m[3][3] - m[2][3] * m[3][2]) -
178 m[1][2] * (m[2][1] * m[3][3] - m[2][3] * m[3][1]) +
179 m[1][3] * (m[2][1] * m[3][2] - m[2][2] * m[3][1])
180 ) -
181 m[0][1] * (
182 m[1][0] * (m[2][2] * m[3][3] - m[2][3] * m[3][2]) -
183 m[1][2] * (m[2][0] * m[3][3] - m[2][3] * m[3][0]) +
184 m[1][3] * (m[2][0] * m[3][2] - m[2][2] * m[3][0])
185 ) +
186 m[0][2] * (
187 m[1][0] * (m[2][1] * m[3][3] - m[2][3] * m[3][1]) -
188 m[1][1] * (m[2][0] * m[3][3] - m[2][3] * m[3][0]) +
189 m[1][3] * (m[2][0] * m[3][1] - m[2][1] * m[3][0])
190 ) -
191 m[0][3] * (
192 m[1][0] * (m[2][1] * m[3][2] - m[2][2] * m[3][1]) -
193 m[1][1] * (m[2][0] * m[3][2] - m[2][2] * m[3][0]) +
194 m[1][2] * (m[2][0] * m[3][1] - m[2][1] * m[3][0])
195 );
196 }
197
204 //Matrix4x4 inverse() const {
205 // float det = determinant();
206 // if (det == 0) {
207 // // Return identity matrix for simplicity when the matrix is singular.
208 // return Matrix4x4();
209 // }
210
211 // float invDet = 1.0f / det;
212
213 // return Matrix4x4(
214 // (m[1][1] * (m[2][2] * m[3][3] - m[2][3] * m[3][2]) -
215 // m[1][2] * (m[2][1] * m[3][3] - m[2][3] * m[3][1]) +
216 // m[1][3] * (m[2][1] * m[3][2] - m[2][2] * m[3][1])) * invDet,
217 // -(m[0][1] * (m[2][2] * m[3][3] - m[2][3] * m[3][2]) -
218 // m[0][2] * (m[2][1] * m[3][3] - m[2][3] * m[3][1]) +
219 // m[0][3] * (m[2][1] * m[3][2] - m[2][2] * m[3][1])) * invDet,
220 // (m[0][1] * (m[1][2] * m[3][3] - m[1][3] * m[3][2]) -
221 // m[0][2] * (m[1][1] * m[3][3] - m[1][3] * m[3][1]) +
222 // m[0][3] * (m[1][1] * m[3][2] - m[1][2] * m[3][1])) * invDet,
223 // -(m[0][1] * (m[1][2] * m[2][3] - m[1][3] * m[2][2]) -
224 // m[0][2] * (m[1][1] * m[2][3] - m[1][3] * m[2][1]) +
225 // m[0][3] * (m[1][1] * m[2][2] - m[1][2] * m[2][1])) * invDet,
226
227 // -(m[1][0] * (m[2][2] * m[3][3] - m[2][3] * m[3][2]) -
228 // m[1][2] * (m[2][0] * m[3][3] - m[2][3] * m[3][0]) +
229 // m[1][3] * (m[2][0] * m[3][2] - m[2][2] * m[3][0])) * invDet,
230 // (m[0][0] * (m[2][2] * m[3][3] - m[2][3] * m[3][2]) -
231 // m[0][2] * (m[2][0] * m[3][3] - m[2][3] * m[3][0]) +
232 // m[0][3] * (m[2][0] * m[3][2] - m[2][2] * m[3][0])) * invDet,
233 // -(m[0][0] * (m[1][2] * m[3][3] - m[1][3] * m[3][2]) -
234 // m[0][2] * (m[1][0] * m[3][3] - m[1][3] * m[3][0]) +
235 // m[0][3] * (m[1][0] * m[3][2] - m[1][2] * m[3][0])) * invDet,
236 // (m[0][0] * (m[1][2] * m[2][3] - m[1][3] * m[2][2]) -
237 // m[0][2] * (m[1][0] * m[2][3] - m[1][3] * m[2][0]) +
238 // m[0][3] * (m[1][0] * m[2][2] - m[1][2] * m[2][0])) * invDet,
239
240 // (m[1][0] * (m[2][1] * m[3][3] - m[2][3] * m[3][1]) -
241 // m[1][1] * (m[2][0] * m[3][3] - m[2][3] * m[3][0]) +
242 // m[1][3] * (m[2][0] * m[3][1] - m[2][1] * m[3][0])) * invDet,
243 // -(m[0][0] * (m[2][1] * m[3][3] - m[2][3] * m[3][1]) -
244 // m[0][1] * (m[2][0] * m[3][3] - m[2][3] * m[3][0]) +
245 // m[0][3] * (m[2][0] * m[3][1] - m[2][1] * m[3][0])) * invDet,
246 // (m[0][0] * (m[1][1] * m[3][3] - m[1][3] * m[3][1]) -
247 // m[0][1] * (m[1][0] * m[3][3] - m[1][3] * m[3][0]) +
248 // m[0][3] * (m[1][0] * m[3][1] - m[1][1] * m[3][0])) * invDet,
249 // -(m[0][0] * (m[1][1] * m[2][3] - m[1][3] * m[2][1]) -
250 // m[0][1] * (m[1][0] * m[2][3] - m[1][3] * m[2][0]) +
251 // m[0][3] * (m[1][0] * m[2][1] - m[1][1] * m[2][0])) * invDet
252 // );
253 //}
254
255
256 };
257}
258
A 4x4 matrix class.
Definition Matrix4x4.h:42
float determinant() const
Computes the determinant of the matrix.
Definition Matrix4x4.h:174
Matrix4x4 operator*(const Matrix4x4 &other) const
Multiplies this matrix by another matrix.
Definition Matrix4x4.h:145
Matrix4x4(const Matrix4x4 &other)
Definition Matrix4x4.h:101
Matrix4x4(float a11, float a12, float a13, float a14, float a21, float a22, float a23, float a24, float a31, float a32, float a33, float a34, float a41, float a42, float a43, float a44)
Parameterized constructor.
Definition Matrix4x4.h:90
float m[4][4]
The elements of the matrix.
Definition Matrix4x4.h:44
Matrix4x4 operator-(const Matrix4x4 &other) const
Subtracts another matrix from this matrix.
Definition Matrix4x4.h:130
Matrix4x4 operator+(const Matrix4x4 &other) const
Adds another matrix to this matrix.
Definition Matrix4x4.h:115
Definition Matrix2x2.h:35