6#include "math/matrix_3.h"
7#include "math/vector_3.h"
8#include "math/vector_4.h"
14 Mat4(
const float a[16]);
71 static Mat4 Scale(
float x,
float y,
float z);
92 const Vec3& rotation);
101 float rotateX,
float rotateY,
float rotateZ);
114 float scale_x,
float scale_y,
float scale_Z,
115 float rotateX,
float rotateY,
float rotateZ);
122 Vec3 direction = (target - position).Normalized();
129 return LookAt(position, target);
139 Vec3 direction = (target - position).Normalized();
150 res.m[2] = -direction.x;
151 res.m[6] = -direction.y;
152 res.m[10] = -direction.z;
174 static Mat4 OrthoMatrix(
float right,
float left,
float top,
float bottom,
175 float near,
float far);
199 Mat4 operator+(
const Mat4& other)
const;
200 Mat4& operator+=(
const Mat4& other);
201 Mat4 operator+(
float value)
const;
202 Mat4& operator+=(
float value);
203 Mat4 operator-(
const Mat4& other)
const;
204 Mat4& operator-=(
const Mat4& other);
205 Mat4 operator-(
float value)
const;
206 Mat4& operator-=(
float value);
207 Mat4& operator*=(
float value);
208 Mat4 operator*(
float value)
const;
211 Mat4& operator/=(
float value);
212 Mat4 operator/(
float value)
const;
213 bool operator==(
const Mat4& other);
214 bool operator!=(
const Mat4& other);
216 Mat4& operator=(
Mat4&& other)
noexcept;
223 float s[16] = {1.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f,
224 0.0f,0.0f,1.0f,1.0f,0.0f,0.0f,0.0f,0.0f};
229 float w = (this->m[3] * vector->x) + (this->m[7] * vector->y) +
230 (this->m[11] * vector->z) + this->m[15];
232 ((this->m[0] * vector->x) + (this->m[4] * vector->y) +
233 (this->m[8] * vector->z) + this->m[12]) /
235 ((this->m[1] * vector->x) + (this->m[5] * vector->y) +
236 (this->m[9] * vector->z) + this->m[13]) /
238 ((this->m[2] * vector->z) + (this->m[6] * vector->z) +
239 (this->m[10] * vector->z) + this->m[14]) /
247 for (
int i = 0; i < 4; i++) {
248 res[i] = m[i + 0 * 4] * v.x + m[i + 1 * 4] * v.y + m[i + 2 * 4] * v.z +
255 float s[16] = {1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1};
261 for (
int row = 0; row < 4; row++) {
262 for (
int col = 0; col < 4; col++) {
263 res.m[row * 4 + col] = m[row * 4 + 0] * other.m[0 * 4 + col] +
264 m[row * 4 + 1] * other.m[1 * 4 + col] +
265 m[row * 4 + 2] * other.m[2 * 4 + col] +
266 m[row * 4 + 3] * other.m[3 * 4 + col];
273 return Mat3(
Vec3(m[5], m[6], m[7]),
Vec3(m[9], m[10], m[11]),
274 Vec3(m[13], m[14], m[15]))
278 Vec3(m[13], m[14], m[15]))
282 Vec3(m[13], m[14], m[15]))
286 Vec3(m[9], m[10], m[11]))
294 for (
int i = 0; i < 4; i++) {
295 for (
int j = 0; j < 4; j++) {
296 if (r != i && c != j) {
297 s[k] = this->m[i * 4 + j];
332 if (det == 0)
return false;
333 operator=(this->
Adjoint() / det);
339 if (det == 0)
return false;
340 *out = (this->
Adjoint() / det);
346 m[0],m[4],m[8],m[12],m[1],m[5],m[9],m[13],
347 m[2],m[6],m[10],m[14],m[3],m[7],m[11],m[15],
354 1,0,0,0,0,1,0,0,0,0,1,0,distance.x,distance.y,distance.z,1,
361 1,0,0,0,0,1,0,0,0,0,1,0,x,y,z,1,
368 scale.x,0,0,0,0,scale.y,0,0,0,0,scale.z,0,0,0,0,1,
375 x,0,0,0,0,y,0,0,0,0,z,0,0,0,0,1,
404 cosf(radians),0,sinf(radians),0,0,1,0,0,
405 -sinf(radians),0,cosf(radians),0,0,0,0,1,
433 const Vec3& rotation) {
441 float rotateX,
float rotateY,
float rotateZ) {
450 float scale_x,
float scale_y,
float scale_z,
451 float rotateX,
float rotateY,
float rotateZ) {
452 return Scale(scale_x, scale_y, scale_z)
460 return Vec4(m[0 + colum], m[4 + colum], m[8 + colum], m[12 + colum]);
464 return Vec4(m[line * 4 + 0], m[line * 4 + 1], m[line * 4 + 2],
470 float const tan_half_fov = tanf(fov * 0.5f);
472 result.m[0] = 1.0f / (aspect * tan_half_fov);
473 result.m[5] = 1.0f / tan_half_fov;
474 result.m[10] = -(far + near) / (far - near);
475 result.m[11] = -1.0f;
476 result.m[14] = -(2.0f * far * near) / (far - near);
481 float bottom,
float near,
float far) {
483 result.m[0] = -2.0f / (right - left);
484 result.m[5] = -2.0f / (top - bottom);
485 result.m[10] = -2.0f / (far - near);
486 result.m[12] = (right + left) / (right - left);
487 result.m[13] = (top + bottom) / (top - bottom);
488 result.m[14] = -(far + near) / (far - near);
493inline Mat4 Mat4::operator+(
const Mat4& other)
const {
495 for (
int i = 0; i < 16; i++) {
496 res.m[i] = m[i] + other.m[i];
501inline Mat4& Mat4::operator+=(
const Mat4& other) {
502 for (
int i = 0; i < 16; i++) {
508inline Mat4 Mat4::operator+(
float value)
const {
510 for (
int i = 0; i < 16; i++) {
511 res.m[i] = m[i] + value;
516inline Mat4& Mat4::operator+=(
float value) {
517 for (
int i = 0; i < 16; i++) {
523inline Mat4 Mat4::operator-(
const Mat4& other)
const {
525 for (
int i = 0; i < 16; i++) {
526 res.m[i] = m[i] - other.m[i];
531inline Mat4& Mat4::operator-=(
const Mat4& other) {
532 for (
int i = 0; i < 16; i++) {
538inline Mat4 Mat4::operator-(
float value)
const {
540 for (
int i = 0; i < 16; i++) {
541 res.m[i] = m[i] - value;
546inline Mat4& Mat4::operator-=(
float value) {
547 for (
int i = 0; i < 16; i++) {
553inline Mat4& Mat4::operator*=(
float value) {
554 for (
int i = 0; i < 16; i++) {
560inline Mat4 Mat4::operator*(
float value)
const {
562 for (
int i = 0; i < 16; i++) {
563 res.m[i] = m[i] * value;
568inline Mat4& Mat4::operator*=(Mat4 value) {
569 for (
int row = 0; row < 4; row++) {
570 for (
int col = 0; col < 4; col++) {
571 m[row * 4 + col] = m[row * 4 + 0] * value.m[0 * 4 + col] +
572 m[row * 4 + 1] * value.m[1 * 4 + col] +
573 m[row * 4 + 2] * value.m[2 * 4 + col] +
574 m[row * 4 + 3] * value.m[3 * 4 + col];
579inline Mat4 Mat4::operator*(Mat4 value)
const {
581 for (
int row = 0; row < 4; row++) {
582 for (
int col = 0; col < 4; col++) {
583 res.m[row * 4 + col] = value.m[row * 4 + 0] * m[0 * 4 + col] +
584 value.m[row * 4 + 1] * m[1 * 4 + col] +
585 value.m[row * 4 + 2] * m[2 * 4 + col] +
586 value.m[row * 4 + 3] * m[3 * 4 + col];
592inline Mat4& Mat4::operator/=(
float value) {
593 for (
int i = 0; i < 16; i++) {
599inline Mat4 Mat4::operator/(
float value)
const {
601 for (
int i = 0; i < 16; i++) {
602 res.m[i] = m[i] / value;
607inline bool Mat4::operator==(
const Mat4& other) {
608 for (
int i = 0; i < 16; i++) {
609 if (m[i] != other.m[i])
return false;
614inline bool Mat4::operator!=(
const Mat4& other) {
615 for (
int i = 0; i < 16; i++) {
616 if (m[i] != other.m[i])
return true;
621inline Mat4& Mat4::operator=(
const Mat4& other) {
622 for (
int i = 0; i < 16; i++) {
628inline Mat4& Mat4::operator=(Mat4&& other)
noexcept {
629 for (
int i = 0; i < 16; i++) {
represents mathematical matrix with 3 cols and 3 rows
Definition: matrix_3.h:18
float Determinant() const
Definition: matrix_3.h:211
represents mathematical matrix with 4 cols and 4 rows
Definition: matrix_4.h:11
static Mat4 LookAtZeroCheck(const Vec3 &position, const Vec3 &target)
Like LoockAt but checking that no divisions by 0 are made.
Definition: matrix_4.h:121
bool GetInverse(Mat4 *out) const
Definition: matrix_4.h:337
static Mat4 PerspectiveMatrix(float fov, float aspect, float near, float far)
Definition: matrix_4.h:468
Mat4 Transpose() const
Definition: matrix_4.h:344
Mat3 getSubMatrix(int r, int c) const
Definition: matrix_4.h:291
bool Inverse()
Definition: matrix_4.h:330
Vec4 GetColum(int colum) const
Definition: matrix_4.h:459
float Determinant() const
Definition: matrix_4.h:272
Mat4 Multiply(const Mat4 &other) const
Definition: matrix_4.h:259
Vec4 Transform(const Vec4 &v)
Definition: matrix_4.h:245
static Mat4 GetTransform(const Vec3 &translate, const Vec3 &scale, const Vec3 &rotation)
Definition: matrix_4.h:432
static Mat4 RotateY(float radians)
Definition: matrix_4.h:402
Mat4 Adjoint() const
Definition: matrix_4.h:305
static Mat4 Scale(const Vec3 &scale)
Definition: matrix_4.h:366
Mat4 Projection() const
Definition: matrix_4.h:222
Vec4 GetLine(int line) const
Definition: matrix_4.h:463
static Mat4 LookAt(const Vec3 &position, const Vec3 &target, const Vec3 &upDir=Vec3::up)
Calculate the view matrix based on the position and a target.
Definition: matrix_4.h:137
static Mat4 Identity()
Definition: matrix_4.h:254
Vec3 TransformVec3(Vec3 *vector)
Definition: matrix_4.h:228
static Mat4 RotateX(float radians)
Definition: matrix_4.h:380
static Mat4 OrthoMatrix(float right, float left, float top, float bottom, float near, float far)
Definition: matrix_4.h:480
static Mat4 Translate(const Vec3 &distance)
Definition: matrix_4.h:352
static Mat4 RotateZ(float radians)
Definition: matrix_4.h:410
represents mathematical vector with 3 components
Definition: vector_3.h:12
static const Vec3 up
Vec3(0, 1, 0)
Definition: vector_3.h:117
static const Vec3 down
Vec3(0, -1, 0)
Definition: vector_3.h:119
static const Vec3 back
Vec3(0, 0, -1)
Definition: vector_3.h:127
static Vec3 CrossProduct(const Vec3 &a, const Vec3 &b)
Definition: vector_3.h:155
static float DotProduct(const Vec3 &a, const Vec3 &b)
Definition: vector_3.h:147
static const Vec3 forward
Vec3(0, 0, 1)
Definition: vector_3.h:125
Vec3 Normalized() const
Definition: vector_3.h:145
represents mathematical vector with 4 components
Definition: vector_4.h:8
copperdielf Math Library
Definition: buffer.hpp:5