HPC_Voxel_Engine 0.2.0
High-Performance C++ Voxel Engine
Loading...
Searching...
No Matches
Matrix.h
Go to the documentation of this file.
1#pragma once
2#include <cmath>
3#include <cstring>
4#include "MathUtils.h"
5
6namespace Core {
7
18struct Mat4 {
19 float m_fElements[16];
20
21 // --- Constructors ---
22
26 constexpr Mat4() noexcept {
27 // Initialize all to 0.0f
28 for (int i = 0; i < 16; i++) m_fElements[i] = 0.0f;
29 // Diagonal = 1.0f (Identity)
30 m_fElements[0] = 1.0f;
31 m_fElements[5] = 1.0f;
32 m_fElements[10] = 1.0f;
33 m_fElements[15] = 1.0f;
34 }
35
36 // Helper to get Identity (matches default constructor)
37 static Mat4 Identity() { return Mat4(); }
38
39 // Helper to get a pure Zero matrix (for Projections)
40 static Mat4 Zero() {
41 Mat4 result;
42 // We must manually zero it out because the constructor sets diagonals to 1
43 for (int i = 0; i < 16; i++) result.m_fElements[i] = 0.0f;
44 return result;
45 }
46
47 // --- Operations ---
48
53 Mat4 operator*(const Mat4& other) const {
54 Mat4 result = Mat4::Zero();
55 for (int col = 0; col < 4; ++col) {
56 for (int row = 0; row < 4; ++row) {
57 float sum = 0.0f;
58 for (int k = 0; k < 4; ++k) {
59 // Row of A * Column of B
60 sum += m_fElements[row + k * 4] * other.m_fElements[k + col * 4];
61 }
62 result.m_fElements[row + col * 4] = sum;
63 }
64 }
65 return result;
66 }
67
68 // --- Projections & Transforms ---
69
77 static Mat4 Perspective(float fov, float aspect, float nearplane, float farplane) {
78 Mat4 result = Mat4::Zero();
79 float tanHalfFOV = std::tan(fov * 3.1415926535f * 0.5f / 180.0f);
80
81 // [0][0] = 1 / (aspect * tan(fov/2))
82 result.m_fElements[0] = 1.0f / (aspect * tanHalfFOV);
83
84 // [1][1] = 1 / tan(fov/2)
85 result.m_fElements[5] = 1.0f / (tanHalfFOV);
86
87 // [2][2] = -(far + near) / (far - near)
88 result.m_fElements[10] = -(farplane + nearplane) / (farplane - nearplane);
89
90 // [2][3] = -1 (This is Index 11 in Column-Major: Row 3, Col 2)
91 // This puts -Z into W for perspective divide
92 result.m_fElements[11] = -1.0f;
93
94 // [3][2] = -(2 * far * near) / (far - near) (Index 14: Row 2, Col 3)
95 result.m_fElements[14] = -(2.0f * farplane * nearplane) / (farplane - nearplane);
96
97 // [3][3] = 0
98 result.m_fElements[15] = 0.0f;
99
100 return result;
101 }
102
106 static Mat4 LookAt(const Vec3& eye, const Vec3& tgt, const Vec3& up) {
107 Mat4 result;
108 Vec3 fwd = (tgt - eye).normalize();
109 Vec3 right = fwd.cross(up).normalize();
110 Vec3 trueUp = right.cross(fwd);
111
112 // Rotation Part (Orthonormal Basis)
113 // Row 0: Right
114 result.m_fElements[0] = right.x;
115 result.m_fElements[4] = right.y;
116 result.m_fElements[8] = right.z;
117
118 // Row 1: Up
119 result.m_fElements[1] = trueUp.x;
120 result.m_fElements[5] = trueUp.y;
121 result.m_fElements[9] = trueUp.z;
122
123 // Row 2: -Forward (Looking down -Z)
124 result.m_fElements[2] = -fwd.x;
125 result.m_fElements[6] = -fwd.y;
126 result.m_fElements[10] = -fwd.z;
127
128 // Translation Part (Dot products)
129 result.m_fElements[12] = -right.dot(eye);
130 result.m_fElements[13] = -trueUp.dot(eye);
131 result.m_fElements[14] = fwd.dot(eye); // Dot with positive fwd because Z is negated
132
133 return result;
134 }
135
137 float left, float right, float bottom, float top, float nearplane, float farplane) {
138 Mat4 result;
139
140 result.m_fElements[0] = 2.0f / (right - left);
141 result.m_fElements[5] = 2.0f / (top - bottom);
142 result.m_fElements[10] = -2.0f / (farplane - nearplane);
143
144 result.m_fElements[12] = -(right + left) / (right - left);
145 result.m_fElements[13] = -(top + bottom) / (top - bottom);
146 result.m_fElements[14] = -(farplane + nearplane) / (farplane - nearplane);
147
148 return result;
149 }
150
151 static Mat4 Translation(const Vec3& translation) {
152 Mat4 result;
153 result.m_fElements[12] = translation.x;
154 result.m_fElements[13] = translation.y;
155 result.m_fElements[14] = translation.z;
156 return result;
157 }
158
159 static Mat4 Scale(float scaleX, float scaleY, float scaleZ) {
160 Mat4 result;
161 result.m_fElements[0] = scaleX;
162 result.m_fElements[5] = scaleY;
163 result.m_fElements[10] = scaleZ;
164 return result;
165 }
166};
167} // namespace Core
Definition camera.h:6
A 4x4 Matrix structure stored in Column-Major order (OpenGL Standard).
Definition Matrix.h:18
static Mat4 LookAt(const Vec3 &eye, const Vec3 &tgt, const Vec3 &up)
Creates a View Matrix using Eye, Target, and Up vectors.
Definition Matrix.h:106
float m_fElements[16]
Definition Matrix.h:19
static Mat4 Perspective(float fov, float aspect, float nearplane, float farplane)
Creates a Perspective Projection Matrix (FOV Y).
Definition Matrix.h:77
static Mat4 Zero()
Definition Matrix.h:40
constexpr Mat4() noexcept
Default constructor initializes to Identity Matrix.
Definition Matrix.h:26
static Mat4 Translation(const Vec3 &translation)
Definition Matrix.h:151
static Mat4 Scale(float scaleX, float scaleY, float scaleZ)
Definition Matrix.h:159
static Mat4 Orthographic(float left, float right, float bottom, float top, float nearplane, float farplane)
Definition Matrix.h:136
Mat4 operator*(const Mat4 &other) const
Matrix Multiplication.
Definition Matrix.h:53
static Mat4 Identity()
Definition Matrix.h:37
A 3-component vector structure (x, y, z) with standard math operations.
Definition MathUtils.h:11
float x
Definition MathUtils.h:12
constexpr float dot(const Vec3 &other) const noexcept
Definition MathUtils.h:68
float z
Definition MathUtils.h:12
constexpr Vec3 cross(const Vec3 &other) const noexcept
Definition MathUtils.h:71
Vec3 normalize() const
Definition MathUtils.h:51
float y
Definition MathUtils.h:12