1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #if defined _XBOX |
---|
16 | # define _USE_MATH_DEFINES /* for M_PI */ |
---|
17 | # include <xtl.h> |
---|
18 | # undef near /* Fuck Microsoft */ |
---|
19 | # undef far /* Fuck Microsoft again */ |
---|
20 | #elif defined _WIN32 |
---|
21 | # define _USE_MATH_DEFINES /* for M_PI */ |
---|
22 | # define WIN32_LEAN_AND_MEAN |
---|
23 | # include <windows.h> |
---|
24 | # undef near /* Fuck Microsoft */ |
---|
25 | # undef far /* Fuck Microsoft again */ |
---|
26 | #endif |
---|
27 | |
---|
28 | #include <cmath> /* for M_PI */ |
---|
29 | #include <cstdlib> /* free() */ |
---|
30 | #include <cstring> /* strdup() */ |
---|
31 | |
---|
32 | #include "core.h" |
---|
33 | |
---|
34 | using namespace std; |
---|
35 | |
---|
36 | namespace lol |
---|
37 | { |
---|
38 | |
---|
39 | static inline float det2(float a, float b, |
---|
40 | float c, float d) |
---|
41 | { |
---|
42 | return a * d - b * c; |
---|
43 | } |
---|
44 | |
---|
45 | static inline float det3(float a, float b, float c, |
---|
46 | float d, float e, float f, |
---|
47 | float g, float h, float i) |
---|
48 | { |
---|
49 | return a * (e * i - h * f) |
---|
50 | + b * (f * g - i * d) |
---|
51 | + c * (d * h - g * e); |
---|
52 | } |
---|
53 | |
---|
54 | static inline float cofact(mat2 const &mat, int i, int j) |
---|
55 | { |
---|
56 | return mat[(i + 1) & 1][(j + 1) & 1] * (((i + j) & 1) ? -1.0f : 1.0f); |
---|
57 | } |
---|
58 | |
---|
59 | static inline float cofact(mat3 const &mat, int i, int j) |
---|
60 | { |
---|
61 | return det2(mat[(i + 1) % 3][(j + 1) % 3], |
---|
62 | mat[(i + 2) % 3][(j + 1) % 3], |
---|
63 | mat[(i + 1) % 3][(j + 2) % 3], |
---|
64 | mat[(i + 2) % 3][(j + 2) % 3]); |
---|
65 | } |
---|
66 | |
---|
67 | static inline float cofact(mat4 const &mat, int i, int j) |
---|
68 | { |
---|
69 | return det3(mat[(i + 1) & 3][(j + 1) & 3], |
---|
70 | mat[(i + 2) & 3][(j + 1) & 3], |
---|
71 | mat[(i + 3) & 3][(j + 1) & 3], |
---|
72 | mat[(i + 1) & 3][(j + 2) & 3], |
---|
73 | mat[(i + 2) & 3][(j + 2) & 3], |
---|
74 | mat[(i + 3) & 3][(j + 2) & 3], |
---|
75 | mat[(i + 1) & 3][(j + 3) & 3], |
---|
76 | mat[(i + 2) & 3][(j + 3) & 3], |
---|
77 | mat[(i + 3) & 3][(j + 3) & 3]) * (((i + j) & 1) ? -1.0f : 1.0f); |
---|
78 | } |
---|
79 | |
---|
80 | template<> float determinant(mat2 const &mat) |
---|
81 | { |
---|
82 | return mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0]; |
---|
83 | } |
---|
84 | |
---|
85 | template<> mat2 transpose(mat2 const &mat) |
---|
86 | { |
---|
87 | mat2 ret; |
---|
88 | for (int j = 0; j < 2; j++) |
---|
89 | for (int i = 0; i < 2; i++) |
---|
90 | ret[j][i] = mat[i][j]; |
---|
91 | return ret; |
---|
92 | } |
---|
93 | |
---|
94 | template<> mat2 inverse(mat2 const &mat) |
---|
95 | { |
---|
96 | mat2 ret; |
---|
97 | float d = determinant(mat); |
---|
98 | if (d) |
---|
99 | { |
---|
100 | d = 1.0f / d; |
---|
101 | for (int j = 0; j < 2; j++) |
---|
102 | for (int i = 0; i < 2; i++) |
---|
103 | ret[j][i] = cofact(mat, i, j) * d; |
---|
104 | } |
---|
105 | return ret; |
---|
106 | } |
---|
107 | |
---|
108 | template<> float determinant(mat3 const &mat) |
---|
109 | { |
---|
110 | return det3(mat[0][0], mat[0][1], mat[0][2], |
---|
111 | mat[1][0], mat[1][1], mat[1][2], |
---|
112 | mat[2][0], mat[2][1], mat[2][2]); |
---|
113 | } |
---|
114 | |
---|
115 | template<> mat3 transpose(mat3 const &mat) |
---|
116 | { |
---|
117 | mat3 ret; |
---|
118 | for (int j = 0; j < 3; j++) |
---|
119 | for (int i = 0; i < 3; i++) |
---|
120 | ret[j][i] = mat[i][j]; |
---|
121 | return ret; |
---|
122 | } |
---|
123 | |
---|
124 | template<> mat3 inverse(mat3 const &mat) |
---|
125 | { |
---|
126 | mat3 ret; |
---|
127 | float d = determinant(mat); |
---|
128 | if (d) |
---|
129 | { |
---|
130 | d = 1.0f / d; |
---|
131 | for (int j = 0; j < 3; j++) |
---|
132 | for (int i = 0; i < 3; i++) |
---|
133 | ret[j][i] = cofact(mat, i, j) * d; |
---|
134 | } |
---|
135 | return ret; |
---|
136 | } |
---|
137 | |
---|
138 | template<> float determinant(mat4 const &mat) |
---|
139 | { |
---|
140 | float ret = 0; |
---|
141 | for (int n = 0; n < 4; n++) |
---|
142 | ret += mat[n][0] * cofact(mat, n, 0); |
---|
143 | return ret; |
---|
144 | } |
---|
145 | |
---|
146 | template<> mat4 transpose(mat4 const &mat) |
---|
147 | { |
---|
148 | mat4 ret; |
---|
149 | for (int j = 0; j < 4; j++) |
---|
150 | for (int i = 0; i < 4; i++) |
---|
151 | ret[j][i] = mat[i][j]; |
---|
152 | return ret; |
---|
153 | } |
---|
154 | |
---|
155 | template<> mat4 inverse(mat4 const &mat) |
---|
156 | { |
---|
157 | mat4 ret; |
---|
158 | float d = determinant(mat); |
---|
159 | if (d) |
---|
160 | { |
---|
161 | d = 1.0f / d; |
---|
162 | for (int j = 0; j < 4; j++) |
---|
163 | for (int i = 0; i < 4; i++) |
---|
164 | ret[j][i] = cofact(mat, i, j) * d; |
---|
165 | } |
---|
166 | return ret; |
---|
167 | } |
---|
168 | |
---|
169 | template<> void vec2::printf() const |
---|
170 | { |
---|
171 | Log::Debug("[ %6.6f %6.6f ]\n", x, y); |
---|
172 | } |
---|
173 | |
---|
174 | template<> void ivec2::printf() const |
---|
175 | { |
---|
176 | Log::Debug("[ %i %i ]\n", x, y); |
---|
177 | } |
---|
178 | |
---|
179 | template<> void cmplx::printf() const |
---|
180 | { |
---|
181 | Log::Debug("[ %6.6f %6.6f ]\n", x, y); |
---|
182 | } |
---|
183 | |
---|
184 | template<> void vec3::printf() const |
---|
185 | { |
---|
186 | Log::Debug("[ %6.6f %6.6f %6.6f ]\n", x, y, z); |
---|
187 | } |
---|
188 | |
---|
189 | template<> void ivec3::printf() const |
---|
190 | { |
---|
191 | Log::Debug("[ %i %i %i ]\n", x, y, z); |
---|
192 | } |
---|
193 | |
---|
194 | template<> void vec4::printf() const |
---|
195 | { |
---|
196 | Log::Debug("[ %6.6f %6.6f %6.6f %6.6f ]\n", x, y, z, w); |
---|
197 | } |
---|
198 | |
---|
199 | template<> void ivec4::printf() const |
---|
200 | { |
---|
201 | Log::Debug("[ %i %i %i %i ]\n", x, y, z, w); |
---|
202 | } |
---|
203 | |
---|
204 | template<> void quat::printf() const |
---|
205 | { |
---|
206 | Log::Debug("[ %6.6f %6.6f %6.6f %6.6f ]\n", w, x, y, z); |
---|
207 | } |
---|
208 | |
---|
209 | template<> void mat2::printf() const |
---|
210 | { |
---|
211 | mat2 const &p = *this; |
---|
212 | |
---|
213 | Log::Debug("[ %6.6f %6.6f\n", p[0][0], p[1][0]); |
---|
214 | Log::Debug(" %6.6f %6.6f ]\n", p[0][1], p[1][1]); |
---|
215 | } |
---|
216 | |
---|
217 | template<> void mat3::printf() const |
---|
218 | { |
---|
219 | mat3 const &p = *this; |
---|
220 | |
---|
221 | Log::Debug("[ %6.6f %6.6f %6.6f\n", p[0][0], p[1][0], p[2][0]); |
---|
222 | Log::Debug(" %6.6f %6.6f %6.6f\n", p[0][1], p[1][1], p[2][1]); |
---|
223 | Log::Debug(" %6.6f %6.6f %6.6f ]\n", p[0][2], p[1][2], p[2][2]); |
---|
224 | } |
---|
225 | |
---|
226 | template<> void mat4::printf() const |
---|
227 | { |
---|
228 | mat4 const &p = *this; |
---|
229 | |
---|
230 | Log::Debug("[ %6.6f %6.6f %6.6f %6.6f\n", |
---|
231 | p[0][0], p[1][0], p[2][0], p[3][0]); |
---|
232 | Log::Debug(" %6.6f %6.6f %6.6f %6.6f\n", |
---|
233 | p[0][1], p[1][1], p[2][1], p[3][1]); |
---|
234 | Log::Debug(" %6.6f %6.6f %6.6f %6.6f\n", |
---|
235 | p[0][2], p[1][2], p[2][2], p[3][2]); |
---|
236 | Log::Debug(" %6.6f %6.6f %6.6f %6.6f ]\n", |
---|
237 | p[0][3], p[1][3], p[2][3], p[3][3]); |
---|
238 | } |
---|
239 | |
---|
240 | #if !defined __ANDROID__ |
---|
241 | template<> std::ostream &operator<<(std::ostream &stream, ivec2 const &v) |
---|
242 | { |
---|
243 | return stream << "(" << v.x << ", " << v.y << ")"; |
---|
244 | } |
---|
245 | |
---|
246 | template<> std::ostream &operator<<(std::ostream &stream, icmplx const &v) |
---|
247 | { |
---|
248 | return stream << "(" << v.x << ", " << v.y << ")"; |
---|
249 | } |
---|
250 | |
---|
251 | template<> std::ostream &operator<<(std::ostream &stream, ivec3 const &v) |
---|
252 | { |
---|
253 | return stream << "(" << v.x << ", " << v.y << ", " << v.z << ")"; |
---|
254 | } |
---|
255 | |
---|
256 | template<> std::ostream &operator<<(std::ostream &stream, ivec4 const &v) |
---|
257 | { |
---|
258 | return stream << "(" << v.x << ", " << v.y << ", " |
---|
259 | << v.z << ", " << v.w << ")"; |
---|
260 | } |
---|
261 | |
---|
262 | template<> std::ostream &operator<<(std::ostream &stream, iquat const &v) |
---|
263 | { |
---|
264 | return stream << "(" << v.x << ", " << v.y << ", " |
---|
265 | << v.z << ", " << v.w << ")"; |
---|
266 | } |
---|
267 | |
---|
268 | template<> std::ostream &operator<<(std::ostream &stream, vec2 const &v) |
---|
269 | { |
---|
270 | return stream << "(" << v.x << ", " << v.y << ")"; |
---|
271 | } |
---|
272 | |
---|
273 | template<> std::ostream &operator<<(std::ostream &stream, cmplx const &v) |
---|
274 | { |
---|
275 | return stream << "(" << v.x << ", " << v.y << ")"; |
---|
276 | } |
---|
277 | |
---|
278 | template<> std::ostream &operator<<(std::ostream &stream, vec3 const &v) |
---|
279 | { |
---|
280 | return stream << "(" << v.x << ", " << v.y << ", " << v.z << ")"; |
---|
281 | } |
---|
282 | |
---|
283 | template<> std::ostream &operator<<(std::ostream &stream, vec4 const &v) |
---|
284 | { |
---|
285 | return stream << "(" << v.x << ", " << v.y << ", " |
---|
286 | << v.z << ", " << v.w << ")"; |
---|
287 | } |
---|
288 | |
---|
289 | template<> std::ostream &operator<<(std::ostream &stream, quat const &v) |
---|
290 | { |
---|
291 | return stream << "(" << v.x << ", " << v.y << ", " |
---|
292 | << v.z << ", " << v.w << ")"; |
---|
293 | } |
---|
294 | |
---|
295 | template<> std::ostream &operator<<(std::ostream &stream, mat4 const &m) |
---|
296 | { |
---|
297 | stream << "((" << m[0][0] << ", " << m[1][0] |
---|
298 | << ", " << m[2][0] << ", " << m[3][0] << "), "; |
---|
299 | stream << "(" << m[0][1] << ", " << m[1][1] |
---|
300 | << ", " << m[2][1] << ", " << m[3][1] << "), "; |
---|
301 | stream << "(" << m[0][2] << ", " << m[1][2] |
---|
302 | << ", " << m[2][2] << ", " << m[3][2] << "), "; |
---|
303 | stream << "(" << m[0][3] << ", " << m[1][3] |
---|
304 | << ", " << m[2][3] << ", " << m[3][3] << "))"; |
---|
305 | return stream; |
---|
306 | } |
---|
307 | #endif |
---|
308 | |
---|
309 | template<> mat3 mat3::scale(float x, float y, float z) |
---|
310 | { |
---|
311 | mat3 ret(1.0f); |
---|
312 | |
---|
313 | ret[0][0] = x; |
---|
314 | ret[1][1] = y; |
---|
315 | ret[2][2] = z; |
---|
316 | |
---|
317 | return ret; |
---|
318 | } |
---|
319 | |
---|
320 | template<> mat3 mat3::scale(vec3 v) |
---|
321 | { |
---|
322 | return scale(v.x, v.y, v.z); |
---|
323 | } |
---|
324 | |
---|
325 | template<> mat4 mat4::translate(float x, float y, float z) |
---|
326 | { |
---|
327 | mat4 ret(1.0f); |
---|
328 | ret[3][0] = x; |
---|
329 | ret[3][1] = y; |
---|
330 | ret[3][2] = z; |
---|
331 | return ret; |
---|
332 | } |
---|
333 | |
---|
334 | template<> mat4 mat4::translate(vec3 v) |
---|
335 | { |
---|
336 | return translate(v.x, v.y, v.z); |
---|
337 | } |
---|
338 | |
---|
339 | template<> mat2 mat2::rotate(float angle) |
---|
340 | { |
---|
341 | angle *= (M_PI / 180.0f); |
---|
342 | |
---|
343 | float st = std::sin(angle); |
---|
344 | float ct = std::cos(angle); |
---|
345 | |
---|
346 | mat2 ret; |
---|
347 | |
---|
348 | ret[0][0] = ct; |
---|
349 | ret[0][1] = st; |
---|
350 | |
---|
351 | ret[1][0] = -st; |
---|
352 | ret[1][1] = ct; |
---|
353 | |
---|
354 | return ret; |
---|
355 | } |
---|
356 | |
---|
357 | template<> mat3 mat3::rotate(float angle, float x, float y, float z) |
---|
358 | { |
---|
359 | angle *= (M_PI / 180.0f); |
---|
360 | |
---|
361 | float st = std::sin(angle); |
---|
362 | float ct = std::cos(angle); |
---|
363 | |
---|
364 | float len = sqrtf(x * x + y * y + z * z); |
---|
365 | float invlen = len ? 1.0f / len : 0.0f; |
---|
366 | x *= invlen; |
---|
367 | y *= invlen; |
---|
368 | z *= invlen; |
---|
369 | |
---|
370 | float mtx = (1.0f - ct) * x; |
---|
371 | float mty = (1.0f - ct) * y; |
---|
372 | float mtz = (1.0f - ct) * z; |
---|
373 | |
---|
374 | mat3 ret; |
---|
375 | |
---|
376 | ret[0][0] = x * mtx + ct; |
---|
377 | ret[0][1] = x * mty + st * z; |
---|
378 | ret[0][2] = x * mtz - st * y; |
---|
379 | |
---|
380 | ret[1][0] = y * mtx - st * z; |
---|
381 | ret[1][1] = y * mty + ct; |
---|
382 | ret[1][2] = y * mtz + st * x; |
---|
383 | |
---|
384 | ret[2][0] = z * mtx + st * y; |
---|
385 | ret[2][1] = z * mty - st * x; |
---|
386 | ret[2][2] = z * mtz + ct; |
---|
387 | |
---|
388 | return ret; |
---|
389 | } |
---|
390 | |
---|
391 | template<> mat3 mat3::rotate(float angle, vec3 v) |
---|
392 | { |
---|
393 | return rotate(angle, v.x, v.y, v.z); |
---|
394 | } |
---|
395 | |
---|
396 | template<> mat3 mat3::rotate(quat q) |
---|
397 | { |
---|
398 | float n = norm(q); |
---|
399 | |
---|
400 | if (!n) |
---|
401 | return mat3(1.0f); |
---|
402 | |
---|
403 | mat3 ret; |
---|
404 | float s = 2.0f / n; |
---|
405 | |
---|
406 | ret[0][0] = 1.0f - s * (q.y * q.y + q.z * q.z); |
---|
407 | ret[0][1] = s * (q.x * q.y - q.z * q.w); |
---|
408 | ret[0][2] = s * (q.x * q.z + q.y * q.w); |
---|
409 | |
---|
410 | ret[1][0] = s * (q.x * q.y + q.z * q.w); |
---|
411 | ret[1][1] = 1.0f - s * (q.z * q.z + q.x * q.x); |
---|
412 | ret[1][2] = s * (q.y * q.z - q.x * q.w); |
---|
413 | |
---|
414 | ret[2][0] = s * (q.x * q.z - q.y * q.w); |
---|
415 | ret[2][1] = s * (q.y * q.z + q.x * q.w); |
---|
416 | ret[2][2] = 1.0f - s * (q.x * q.x + q.y * q.y); |
---|
417 | |
---|
418 | return ret; |
---|
419 | } |
---|
420 | |
---|
421 | template<> quat::Quat(mat4 const &m) |
---|
422 | { |
---|
423 | /* See http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/christian.htm for a version with no branches */ |
---|
424 | float t = m[0][0] + m[1][1] + m[2][2]; |
---|
425 | if (t > 0) |
---|
426 | { |
---|
427 | w = 0.5f * sqrtf(1.0f + t); |
---|
428 | float s = 0.25f / w; |
---|
429 | x = s * (m[2][1] - m[1][2]); |
---|
430 | y = s * (m[0][2] - m[2][0]); |
---|
431 | z = s * (m[1][0] - m[0][1]); |
---|
432 | } |
---|
433 | else if (m[0][0] > m[1][1] && m[0][0] > m[2][2]) |
---|
434 | { |
---|
435 | x = 0.5f * sqrtf(1.0f + m[0][0] - m[1][1] - m[2][2]); |
---|
436 | float s = 0.25f / x; |
---|
437 | y = s * (m[1][0] + m[0][1]); |
---|
438 | z = s * (m[0][2] + m[2][0]); |
---|
439 | w = s * (m[2][1] - m[1][2]); |
---|
440 | } |
---|
441 | else if (m[1][1] > m[2][2]) |
---|
442 | { |
---|
443 | y = 0.5f * sqrtf(1.0f - m[0][0] + m[1][1] - m[2][2]); |
---|
444 | float s = 0.25f / y; |
---|
445 | x = s * (m[1][0] + m[0][1]); |
---|
446 | z = s * (m[2][1] + m[1][2]); |
---|
447 | w = s * (m[0][2] - m[2][0]); |
---|
448 | } |
---|
449 | else |
---|
450 | { |
---|
451 | z = 0.5f * sqrtf(1.0f - m[0][0] - m[1][1] + m[2][2]); |
---|
452 | float s = 0.25f / z; |
---|
453 | x = s * (m[0][2] + m[2][0]); |
---|
454 | y = s * (m[2][1] + m[1][2]); |
---|
455 | w = s * (m[1][0] - m[0][1]); |
---|
456 | } |
---|
457 | } |
---|
458 | |
---|
459 | template<> quat quat::rotate(float angle, vec3 const &v) |
---|
460 | { |
---|
461 | angle *= (M_PI / 360.0f); |
---|
462 | |
---|
463 | vec3 tmp = normalize(v) * std::sin(angle); |
---|
464 | |
---|
465 | return quat(tmp.x, tmp.y, tmp.z, std::cos(angle)); |
---|
466 | } |
---|
467 | |
---|
468 | template<> quat quat::rotate(float angle, float x, float y, float z) |
---|
469 | { |
---|
470 | return quat::rotate(angle, vec3(x, y, z)); |
---|
471 | } |
---|
472 | |
---|
473 | template<> quat quat::fromeuler(vec3 const &v) |
---|
474 | { |
---|
475 | using std::sin; |
---|
476 | using std::cos; |
---|
477 | |
---|
478 | vec3 half_angles = (M_PI / 360.0f) * v; |
---|
479 | float sx = sin(half_angles.x), cx = cos(half_angles.x); |
---|
480 | float sy = sin(half_angles.y), cy = cos(half_angles.y); |
---|
481 | float sz = sin(half_angles.z), cz = cos(half_angles.z); |
---|
482 | |
---|
483 | return quat(cx * cy * cz + sx * sy * sz, |
---|
484 | sx * cy * cz - cx * sy * sz, |
---|
485 | cx * sy * cz + sx * cy * sz, |
---|
486 | cx * cy * sz - sx * sy * cz); |
---|
487 | } |
---|
488 | |
---|
489 | template<> quat quat::fromeuler(float x, float y, float z) |
---|
490 | { |
---|
491 | return quat::fromeuler(vec3(x, y, z)); |
---|
492 | } |
---|
493 | |
---|
494 | template<> mat4 mat4::lookat(vec3 eye, vec3 center, vec3 up) |
---|
495 | { |
---|
496 | vec3 v3 = normalize(eye - center); |
---|
497 | vec3 v2 = normalize(up); |
---|
498 | vec3 v1 = normalize(cross(v2, v3)); |
---|
499 | v2 = cross(v3, v1); |
---|
500 | |
---|
501 | mat4 orient(1.0f); |
---|
502 | orient[0][0] = v1.x; |
---|
503 | orient[0][1] = v2.x; |
---|
504 | orient[0][2] = v3.x; |
---|
505 | orient[1][0] = v1.y; |
---|
506 | orient[1][1] = v2.y; |
---|
507 | orient[1][2] = v3.y; |
---|
508 | orient[2][0] = v1.z; |
---|
509 | orient[2][1] = v2.z; |
---|
510 | orient[2][2] = v3.z; |
---|
511 | |
---|
512 | return orient * mat4::translate(-eye); |
---|
513 | } |
---|
514 | |
---|
515 | template<> mat4 mat4::ortho(float left, float right, float bottom, |
---|
516 | float top, float near, float far) |
---|
517 | { |
---|
518 | float invrl = (right != left) ? 1.0f / (right - left) : 0.0f; |
---|
519 | float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f; |
---|
520 | float invfn = (far != near) ? 1.0f / (far - near) : 0.0f; |
---|
521 | |
---|
522 | mat4 ret(0.0f); |
---|
523 | ret[0][0] = 2.0f * invrl; |
---|
524 | ret[1][1] = 2.0f * invtb; |
---|
525 | ret[2][2] = -2.0f * invfn; |
---|
526 | ret[3][0] = - (right + left) * invrl; |
---|
527 | ret[3][1] = - (top + bottom) * invtb; |
---|
528 | ret[3][2] = - (far + near) * invfn; |
---|
529 | ret[3][3] = 1.0f; |
---|
530 | return ret; |
---|
531 | } |
---|
532 | |
---|
533 | template<> mat4 mat4::frustum(float left, float right, float bottom, |
---|
534 | float top, float near, float far) |
---|
535 | { |
---|
536 | float invrl = (right != left) ? 1.0f / (right - left) : 0.0f; |
---|
537 | float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f; |
---|
538 | float invfn = (far != near) ? 1.0f / (far - near) : 0.0f; |
---|
539 | |
---|
540 | mat4 ret(0.0f); |
---|
541 | ret[0][0] = 2.0f * near * invrl; |
---|
542 | ret[1][1] = 2.0f * near * invtb; |
---|
543 | ret[2][0] = (right + left) * invrl; |
---|
544 | ret[2][1] = (top + bottom) * invtb; |
---|
545 | ret[2][2] = - (far + near) * invfn; |
---|
546 | ret[2][3] = -1.0f; |
---|
547 | ret[3][2] = -2.0f * far * near * invfn; |
---|
548 | return ret; |
---|
549 | } |
---|
550 | |
---|
551 | template<> mat4 mat4::perspective(float fov_y, float width, |
---|
552 | float height, float near, float far) |
---|
553 | { |
---|
554 | fov_y *= (M_PI / 180.0f); |
---|
555 | |
---|
556 | float t2 = tanf(fov_y * 0.5f); |
---|
557 | float t1 = t2 * width / height; |
---|
558 | |
---|
559 | return frustum(-near * t1, near * t1, -near * t2, near * t2, near, far); |
---|
560 | } |
---|
561 | |
---|
562 | } /* namespace lol */ |
---|
563 | |
---|