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
|
#pragma once
mat3 translate(float Tx, float Ty)
{
mat3* res = new mat3(1.f);
(*res)[0][2] = Tx;
(*res)[1][2] = Ty;
return *res;
}
mat4 translate(float Tx, float Ty, float Tz)
{
mat4* res = new mat4(1.f);
(*res)[0][3] = Tx;
(*res)[1][3] = Ty;
(*res)[2][3] = Tz;
return *res;
}
mat3 scale(float Sx, float Sy)
{
mat3* res = new mat3(1.f);
(*res)[0][0] = Sx;
(*res)[1][1] = Sy;
return *res;
}
mat4 scale(float Sx, float Sy, float Sz)
{
mat4* res = new mat4(1.f);
(*res)[0][0] = Sx;
(*res)[1][1] = Sy;
(*res)[2][2] = Sz;
return *res;
}
mat3 scale(float S)
{
return scale(S, S);
}
mat3 rotate(float theta)
{
mat3* res = new mat3(1.f);
(*res)[0][0] = (*res)[1][1] = (float)cos(theta);
(*res)[0][1] = (float)sin(theta);
(*res)[1][0] = -(*res)[0][1];
return *res;
}
mat4 rotate(float theta, vec3 n)
{
n = norm(n);
mat3 nCross = crossM(n);
mat3 id = mat3(1.f);
mat3 m1 = id + nCross * sin(theta);
mat3 m2 = nCross * (nCross * (1.f - cos(theta)));
mat3 m = m1 + m2;
return mat4(vec4(m[0], 0.f),
vec4(m[1], 0.f),
vec4(m[2], 0.f),
vec4(0.f, 0.f, 0.f, 0.f));
}
mat4 rotateP(float theta, vec3 n, vec3 P) {
return translate(P.x, P.y, P.z) *
(rotate(theta, n) * translate(-P.x, -P.y, -P.z));
}
mat4 lookAt(vec3 S, vec3 P, vec3 u) {
vec3 e3 = S - P;
e3 *= 1.f / length(e3);
vec3 e1 = cross(u, e3);
e1 *= 1.f / length(e1);
vec3 e2 = cross(e3, e1);
e2 *= 1.f / length(e2);
mat4 T = translate(-S.x, -S.y, -S.z);
return mat4(vec4(e1, 0.f),
vec4(e2, 0.f),
vec4(e3, 0.f),
vec4(0.f, 0.f, 0.f, 1.f)) * T;
}
mat4 ortho(float l, float r, float b, float t, float zn, float zf) {
return mat4(
vec4(2.f / (r - l), 0.f, 0.f, -(r + l) / (r - l)),
vec4(0.f, 2.f / (t - b), 0.f, -(t + b) / (t - b)),
vec4(0.f, 0.f, -2 / (zf - zn), -(zf + zn) / (zf - zn)),
vec4(0.f, 0.f, 0.f, 1.f));
}
mat4 frustum(float l, float r, float b, float t, float n, float f) {
return mat4(
vec4(2.f * n / (r - l), 0.f, (r + l) / (r - l), 0.f),
vec4(0.f, 2.f * n / (t - b), (t + b) / (t - b), 0.f),
vec4(0.f, 0.f, -(f + n) / (f - n), -(2.f * f * n) / (f - n)),
vec4(0.f, 0.f, -1.f, 0.f));
}
mat4 perspective(float fovy, float aspect, float n, float f) {
return mat4(
vec4(1.f / aspect / tanf(fovy / 2.f), 0.f, 0.f, 0.f),
vec4(0.f, 1.f / tanf(fovy / 2.f), 0.f, 0.f),
vec4(0.f, 0.f, -(f + n) / (f - n), -2.f * f * n / (f - n)),
vec4(0.f, 0.f, -1.f, 0.f));
}
mat3 cadrRL(vec2 Vc, vec2 V, vec2 Wc, vec2 W) {
return translate(Wc.x, Wc.y) *
(scale(W.x / V.x, -W.y / V.y) * translate(-Vc.x, -Vc.y));
}
mat3 mirrorX()
{
mat3* res = new mat3(1.f);
(*res)[1][1] = -1;
return *res;
}
mat3 mirrorY()
{
mat3* res = new mat3(1.f);
(*res)[0][0] = -1;
return *res;
}
|