OpenBF1942-Plus
Refractor 1, rebuilt from scratch
Loading...
Searching...
No Matches
mat.h
Go to the documentation of this file.
1// Minimal 4x4 matrix math. Column-major, column vectors (OpenGL layout).
2// World data stays in the game's left-handed space; only the view mirrors z.
3#pragma once
4#include <cmath>
5
6namespace obf {
7
8struct Mat4 {
9 float m[16] = {};
10 static Mat4 identity() {
11 Mat4 r;
12 r.m[0] = r.m[5] = r.m[10] = r.m[15] = 1;
13 return r;
14 }
15 Mat4 operator*(const Mat4& b) const {
16 Mat4 r;
17 for (int c = 0; c < 4; ++c)
18 for (int i = 0; i < 4; ++i)
19 for (int k = 0; k < 4; ++k) r.m[c * 4 + i] += m[k * 4 + i] * b.m[c * 4 + k];
20 return r;
21 }
22};
23
24inline Mat4 translate(float x, float y, float z) {
25 Mat4 r = Mat4::identity();
26 r.m[12] = x, r.m[13] = y, r.m[14] = z;
27 return r;
28}
29
30inline Mat4 scale(float x, float y, float z) {
31 Mat4 r = Mat4::identity();
32 r.m[0] = x, r.m[5] = y, r.m[10] = z;
33 return r;
34}
35
36// Refractor "yaw/pitch/roll" in degrees, Direct3D convention: roll (z), then pitch (x), then yaw (y).
37inline Mat4 rotation_ypr(float yaw_deg, float pitch_deg, float roll_deg) {
38 const float k = 3.14159265f / 180.0f;
39 float cy = std::cos(yaw_deg * k), sy = std::sin(yaw_deg * k);
40 float cp = std::cos(pitch_deg * k), sp = std::sin(pitch_deg * k);
41 float cr = std::cos(roll_deg * k), sr = std::sin(roll_deg * k);
42 Mat4 ry = Mat4::identity(), rx = Mat4::identity(), rz = Mat4::identity();
43 // Left-handed rotations (D3DXMatrixRotation*), transposed to column vectors.
44 ry.m[0] = cy, ry.m[8] = sy, ry.m[2] = -sy, ry.m[10] = cy;
45 rx.m[5] = cp, rx.m[9] = -sp, rx.m[6] = sp, rx.m[10] = cp;
46 rz.m[0] = cr, rz.m[4] = -sr, rz.m[1] = sr, rz.m[5] = cr;
47 return ry * rx * rz;
48}
49
50inline Mat4 perspective(float fovy, float aspect, float zn, float zf) {
51 Mat4 r;
52 float f = 1.0f / std::tan(fovy / 2);
53 r.m[0] = f / aspect;
54 r.m[5] = f;
55 r.m[10] = (zf + zn) / (zn - zf);
56 r.m[11] = -1;
57 r.m[14] = 2 * zf * zn / (zn - zf);
58 return r;
59}
60
61// Camera in game coordinates; yaw 0 looks along +z, pitch > 0 looks up.
62inline Mat4 view(float x, float y, float z, float yaw, float pitch) {
63 float fx = std::sin(yaw) * std::cos(pitch), fy = std::sin(pitch), fz = std::cos(yaw) * std::cos(pitch);
64 // Game data is left-handed (Direct3D); mirror z into OpenGL's right-handed space.
65 fz = -fz;
66 z = -z;
67 float rx = -fz, rz = fx; // right = forward x up
68 float rl = std::sqrt(rx * rx + rz * rz);
69 rx /= rl;
70 rz /= rl;
71 float ux = -rz * fy, uy = rz * fx - rx * fz, uz = rx * fy; // up = right x forward
72 Mat4 v = Mat4::identity();
73 v.m[0] = rx, v.m[4] = 0, v.m[8] = rz;
74 v.m[1] = ux, v.m[5] = uy, v.m[9] = uz;
75 v.m[2] = -fx, v.m[6] = -fy, v.m[10] = -fz;
76 v.m[12] = -(rx * x + rz * z);
77 v.m[13] = -(ux * x + uy * y + uz * z);
78 v.m[14] = fx * x + fy * y + fz * z;
79 return v * scale(1, 1, -1);
80}
81
82} // namespace obf
Definition anim.cpp:7
Mat4 translate(float x, float y, float z)
Definition mat.h:24
Mat4 view(float x, float y, float z, float yaw, float pitch)
Definition mat.h:62
Mat4 perspective(float fovy, float aspect, float zn, float zf)
Definition mat.h:50
Mat4 rotation_ypr(float yaw_deg, float pitch_deg, float roll_deg)
Definition mat.h:37
Mat4 scale(float x, float y, float z)
Definition mat.h:30
Definition mat.h:8
static Mat4 identity()
Definition mat.h:10
Mat4 operator*(const Mat4 &b) const
Definition mat.h:15
float m[16]
Definition mat.h:9