-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cpp
166 lines (152 loc) · 4.06 KB
/
Matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "Matrix.h"
#include <math.h>
mat4_t mat4_identity(void)
{
// | 1 0 0 0 |
// | 0 1 0 0 |
// | 0 0 1 0 |
// | 0 0 0 1 |
mat4_t m = {{
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 }
}};
return m;
}
mat4_t mat4_make_scale(float sx, float sy, float sz)
{
// | sx 0 0 0 |
// | 0 sy 0 0 |
// | 0 0 sz 0 |
// | 0 0 0 1 |
mat4_t m = mat4_identity();
m.m[0][0] = sx;
m.m[1][1] = sy;
m.m[2][2] = sz;
return m;
}
mat4_t mat4_make_translation(float tx, float ty, float tz)
{
// | 1 0 0 tx |
// | 0 1 0 ty |
// | 0 0 1 tz |
// | 0 0 0 1 |
mat4_t m = mat4_identity();
m.m[0][3] = tx;
m.m[1][3] = ty;
m.m[2][3] = tz;
return m;
}
vec4_t mat4_mul_vec4(mat4_t m, vec4_t v)
{
vec4_t result;
result.x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3] * v.w;
result.y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3] * v.w;
result.z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3] * v.w;
result.w = m.m[3][0] * v.x + m.m[3][1] * v.y + m.m[3][2] * v.z + m.m[3][3] * v.w;
return result;
}
mat4_t mat4_make_rotation_x(float angle)
{
float c = cos(angle);
float s = sin(angle);
// | 1 0 0 0 |
// | 0 c -s 0 |
// | 0 s c 0 |
// | 0 0 0 1 |
mat4_t m = mat4_identity();
m.m[1][1] = c;
m.m[1][2] = -s;
m.m[2][1] = s;
m.m[2][2] = c;
return m;
}
mat4_t mat4_make_rotation_y(float angle)
{
float c = cos(angle);
float s = sin(angle);
// | c 0 s 0 |
// | 0 1 0 0 |
// | -s 0 c 0 |
// | 0 0 0 1 |
mat4_t m = mat4_identity();
m.m[0][0] = c;
m.m[0][2] = s;
m.m[2][0] = -s;
m.m[2][2] = c;
return m;
}
mat4_t mat4_make_rotation_z(float angle)
{
float c = cos(angle);
float s = sin(angle);
// | c -s 0 0 |
// | s c 0 0 |
// | 0 0 1 0 |
// | 0 0 0 1 |
mat4_t m = mat4_identity();
m.m[0][0] = c;
m.m[0][1] = -s;
m.m[1][0] = s;
m.m[1][1] = c;
return m;
}
mat4_t mat4_mul_mat4(mat4_t a, mat4_t b) {
mat4_t m;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
m.m[i][j] = a.m[i][0] * b.m[0][j] + a.m[i][1] * b.m[1][j] + a.m[i][2] * b.m[2][j] + a.m[i][3] * b.m[3][j];
}
}
return m;
}
mat4_t mat4_make_perspective(float fov, float aspect, float znear, float zfar)
{
// | (h/w)*1/tan(fov/2) 0 0 0 |
// | 0 1/tan(fov/2) 0 0 |
// | 0 0 zf/(zf-zn) (-zf*zn)/(zf-zn) |
// | 0 0 1 0 |
mat4_t m = {{{ 0 }}};
m.m[0][0] = aspect * (1 / tan(fov / 2));
m.m[1][1] = 1 / tan(fov / 2);
m.m[2][2] = zfar / (zfar - znear);
m.m[2][3] = (-zfar * znear) / (zfar - znear);
m.m[3][2] = 1.0;
return m;
}
vec4_t mat4_mul_vec4_project(mat4_t mat_proj, vec4_t v)
{
// multiply the projection matrix by our original vector
vec4_t result = mat4_mul_vec4(mat_proj, v);
// perform perspective divide with original z-value that is now stored in w
if (result.w != 0.0) {
result.x /= result.w;
result.y /= result.w;
result.z /= result.w;
}
return result;
}
mat4_t mat4_look_at(vec3_t eye, vec3_t target, vec3_t up)
{
// Compute the forward (z), right (x), and up (y) vectors
vec3_t z = vec3_sub(target, eye);
vec3_normalize(&z);
vec3_t x = vec3_cross(up, z);
vec3_normalize(&x);
vec3_t y = vec3_cross(z, x);
// | x.x x.y x.z -dot(x,eye) |
// | y.x y.y y.z -dot(y,eye) |
// | z.x z.y z.z -dot(z,eye) |
// | 0 0 0 1 |
mat4_t view_matrix =
{
{
{ x.x, x.y, x.z, -vec3_dot(x, eye) },
{ y.x, y.y, y.z, -vec3_dot(y, eye) },
{ z.x, z.y, z.z, -vec3_dot(z, eye) },
{ 0, 0, 0, 1 }
}
};
return view_matrix;
}