summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/mini_math.h73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/util/mini_math.h b/src/util/mini_math.h
index 7314933..a1b1363 100644
--- a/src/util/mini_math.h
+++ b/src/util/mini_math.h
@@ -137,6 +137,79 @@ struct mat4 {
float m[16] = {1, 0, 0, 0, 0, 1, 0, 0,
0, 0, 1, 0, 0, 0, 0, 1}; // Identity (Column-Major)
+ // Array access
+ float& operator[](int i) {
+ return m[i];
+ }
+ const float& operator[](int i) const {
+ return m[i];
+ }
+
+ // Matrix multiplication
+ mat4 operator*(const mat4& r) const {
+ mat4 res;
+ for (int col = 0; col < 4; ++col) {
+ for (int row = 0; row < 4; ++row) {
+ float sum = 0.0f;
+ for (int k = 0; k < 4; ++k) {
+ sum += m[k * 4 + row] * r.m[col * 4 + k];
+ }
+ res.m[col * 4 + row] = sum;
+ }
+ }
+ return res;
+ }
+
+ // Vector multiplication (Transform)
+ vec4 operator*(const vec4& v) const {
+ vec4 res;
+ for (int row = 0; row < 4; ++row) {
+ res.v[row] = m[row] * v.x + m[row + 4] * v.y + m[row + 8] * v.z +
+ m[row + 12] * v.w;
+ }
+ return res;
+ }
+
+ // Translation
+ static mat4 translate(vec3 t) {
+ mat4 r; // Identity
+ r.m[12] = t.x;
+ r.m[13] = t.y;
+ r.m[14] = t.z;
+ return r;
+ }
+
+ // Scaling
+ static mat4 scale(vec3 s) {
+ mat4 r; // Identity
+ r.m[0] = s.x;
+ r.m[5] = s.y;
+ r.m[10] = s.z;
+ return r;
+ }
+
+ // Rotation (Axis-Angle)
+ static mat4 rotate(vec3 axis, float angle) {
+ vec3 a = axis.normalize();
+ float s = std::sin(angle);
+ float c = std::cos(angle);
+ float oc = 1.0f - c;
+
+ mat4 r;
+ r.m[0] = oc * a.x * a.x + c;
+ r.m[1] = oc * a.x * a.y + a.z * s;
+ r.m[2] = oc * a.x * a.z - a.y * s;
+
+ r.m[4] = oc * a.x * a.y - a.z * s;
+ r.m[5] = oc * a.y * a.y + c;
+ r.m[6] = oc * a.y * a.z + a.x * s;
+
+ r.m[8] = oc * a.x * a.z + a.y * s;
+ r.m[9] = oc * a.y * a.z - a.x * s;
+ r.m[10] = oc * a.z * a.z + c;
+ return r;
+ }
+
static mat4 perspective(float fov, float asp, float n, float f) {
mat4 r = {};
float t = 1.0f / std::tan(fov * 0.5f);