-
Notifications
You must be signed in to change notification settings - Fork 0
/
vecmath.h
221 lines (188 loc) · 5.82 KB
/
vecmath.h
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#ifndef VECMATH_H
#define VECMATH_H
#include <cfloat>
#include <cmath>
struct vec4
{
float x, y, z, w;
inline static vec4 cross(const vec4& u, const vec4& v)
{
return vec4(u.y*v.z-v.y*u.z, u.z*v.x-v.z*u.x, u.x*v.y-v.x*u.y, 0.0f);
}
inline static float length(const vec4& u)
{
return sqrt(u.x*u.x + u.y*u.y + u.z*u.z + u.w*u.w);
}
inline static vec4 normalize(const vec4& u)
{
float invmag = 1.0f/(length(u) + FLT_EPSILON);
return vec4(u.x * invmag, u.y * invmag, u.z * invmag, u.w * invmag);
}
inline vec4()
{
}
inline vec4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w)
{
}
friend vec4 operator +(const vec4& u, const vec4& v);
friend vec4 operator -(const vec4& u, const vec4& v);
friend vec4 operator *(const vec4& u, const vec4& v);
friend vec4 operator /(const vec4& u, const vec4& v);
};
inline vec4 operator +(const vec4& u, const vec4& v)
{
return vec4(u.x+v.x, u.y+v.y, u.z+v.z, u.w+v.w);
}
inline vec4 operator -(const vec4& u, const vec4& v)
{
return vec4(u.x-v.x, u.y-v.y, u.z-v.z, u.w-v.w);
}
inline vec4 operator *(const vec4& u, const vec4& v)
{
return vec4(u.x*v.x, u.y*v.y, u.z*v.z, u.w*v.w);
}
inline vec4 operator /(const vec4& u, const vec4& v)
{
return vec4(u.x/v.x, u.y/v.y, u.z/v.z, u.w/v.w);
}
struct mat4
{
vec4 x;
vec4 y;
vec4 z;
vec4 w;
inline static mat4 identity()
{
return mat4(1.0f,0.0f,0.0f,0.0f, 0.0f,1.0f,0.0f,0.0f, 0.0f,0.0f,1.0f,0.0f, 0.0f,0.0f,0.0f,1.0f);
}
inline static mat4 lookAt(const vec4& eye, const vec4& target, const vec4& up)
{
vec4 f = vec4::normalize(target - eye);
vec4 s = vec4::normalize(vec4::cross(f, up));
vec4 u = vec4::normalize(vec4::cross(s, f));
mat4 m(
s.x,
u.x,
-f.x,
0.0f,
s.y,
u.y,
-f.y,
0.0f,
s.z,
u.z,
-f.z,
0.0f,
0.0f,
0.0f,
0.0f,
1.0f);
vec4 t = m * vec4(-eye.x, -eye.y, -eye.z, 1.0f);
m.w.x = t.x;
m.w.y = t.y;
m.w.z = t.z;
return m;
}
inline static mat4 perspective(float fovY, float aspect, float zNear, float zFar)
{
float rad = fovY/180.0f * 3.141593f * 0.5f;
float cotan = cos(rad) / sin(rad);
float deltaZ = zNear - zFar;
return mat4(
cotan/aspect,
0.0f,
0.0f,
0.0f,
0.0f,
cotan,
0.0f,
0.0f,
0.0f,
0.0f,
(zFar + zNear) / deltaZ,
-1.0f,
0.0f,
0.0f,
2.0f * zNear * zFar / deltaZ,
0.0f);
}
inline static mat4 rotate(float deg, const vec4& axis)
{
float rad = deg/180.0f * 3.141593f;
float c = cos(rad);
float s = sin(rad);
return mat4(
axis.x*axis.x*(1.0f-c) + c,
axis.y*axis.x*(1.0f-c) + axis.z*s,
axis.x*axis.z*(1.0f-c) - axis.y*s,
0.0f,
axis.x*axis.y*(1.0f-c) - axis.z*s,
axis.y*axis.y*(1.0f-c) + c,
axis.y*axis.z*(1.0f-c) + axis.x*s,
0.0f,
axis.x*axis.z*(1.0f-c) + axis.y*s,
axis.y*axis.z*(1.0f-c) - axis.x*s,
axis.z*axis.z*(1.0f-c) + c,
0.0f,
0.0f,
0.0f,
0.0f,
1.0f);
}
inline mat4(float cxrx,
float cxry,
float cxrz,
float cxrw,
float cyrx,
float cyry,
float cyrz,
float cyrw,
float czrx,
float czry,
float czrz,
float czrw,
float cwrx,
float cwry,
float cwrz,
float cwrw) :
x(cxrx,cxry,cxrz,cxrw),
y(cyrx,cyry,cyrz,cyrw),
z(czrx,czry,czrz,czrw),
w(cwrx,cwry,cwrz,cwrw)
{
}
inline mat4()
{
}
friend vec4 operator *(const mat4& m, const vec4& u);
friend mat4 operator *(const mat4& m, const mat4& n);
};
inline vec4 operator *(const mat4& m, const vec4& u)
{
return vec4(
m.x.x*u.x + m.y.x*u.y + m.z.x*u.z + m.w.x*u.w,
m.x.y*u.x + m.y.y*u.y + m.z.y*u.z + m.w.y*u.w,
m.x.z*u.x + m.y.z*u.y + m.z.z*u.z + m.w.z*u.w,
m.x.w*u.x + m.y.w*u.y + m.z.w*u.z + m.w.w*u.w);
}
inline mat4 operator *(const mat4& m, const mat4& n)
{
return mat4(
m.x.x*n.x.x + m.y.x*n.x.y + m.z.x*n.x.z + m.w.x*n.x.w,
m.x.y*n.x.x + m.y.y*n.x.y + m.z.y*n.x.z + m.w.y*n.x.w,
m.x.z*n.x.x + m.y.z*n.x.y + m.z.z*n.x.z + m.w.z*n.x.w,
m.x.w*n.x.x + m.y.w*n.x.y + m.z.w*n.x.z + m.w.w*n.x.w,
m.x.x*n.y.x + m.y.x*n.y.y + m.z.x*n.y.z + m.w.x*n.y.w,
m.x.y*n.y.x + m.y.y*n.y.y + m.z.y*n.y.z + m.w.y*n.y.w,
m.x.z*n.y.x + m.y.z*n.y.y + m.z.z*n.y.z + m.w.z*n.y.w,
m.x.w*n.y.x + m.y.w*n.y.y + m.z.w*n.y.z + m.w.w*n.y.w,
m.x.x*n.z.x + m.y.x*n.z.y + m.z.x*n.z.z + m.w.x*n.z.w,
m.x.y*n.z.x + m.y.y*n.z.y + m.z.y*n.z.z + m.w.y*n.z.w,
m.x.z*n.z.x + m.y.z*n.z.y + m.z.z*n.z.z + m.w.z*n.z.w,
m.x.w*n.z.x + m.y.w*n.z.y + m.z.w*n.z.z + m.w.w*n.z.w,
m.x.x*n.w.x + m.y.x*n.w.y + m.z.x*n.w.z + m.w.x*n.w.w,
m.x.y*n.w.x + m.y.y*n.w.y + m.z.y*n.w.z + m.w.y*n.w.w,
m.x.z*n.w.x + m.y.z*n.w.y + m.z.z*n.w.z + m.w.z*n.w.w,
m.x.w*n.w.x + m.y.w*n.w.y + m.z.w*n.w.z + m.w.w*n.w.w);
}
#endif // VECMATH_H