1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2011 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 | #include <cstdio> |
---|
16 | #include <cstdlib> /* free() */ |
---|
17 | #include <cstring> /* strdup() */ |
---|
18 | |
---|
19 | #include "core.h" |
---|
20 | |
---|
21 | static inline float det3(float a, float b, float c, |
---|
22 | float d, float e, float f, |
---|
23 | float g, float h, float i) |
---|
24 | { |
---|
25 | return a * (e * i - h * f) |
---|
26 | + b * (f * g - i * d) |
---|
27 | + c * (d * h - g * e); |
---|
28 | } |
---|
29 | |
---|
30 | static inline float cofact3(mat4 const &mat, int i, int j) |
---|
31 | { |
---|
32 | return det3(mat[(i + 1) & 3][(j + 1) & 3], |
---|
33 | mat[(i + 2) & 3][(j + 1) & 3], |
---|
34 | mat[(i + 3) & 3][(j + 1) & 3], |
---|
35 | mat[(i + 1) & 3][(j + 2) & 3], |
---|
36 | mat[(i + 2) & 3][(j + 2) & 3], |
---|
37 | mat[(i + 3) & 3][(j + 2) & 3], |
---|
38 | mat[(i + 1) & 3][(j + 3) & 3], |
---|
39 | mat[(i + 2) & 3][(j + 3) & 3], |
---|
40 | mat[(i + 3) & 3][(j + 3) & 3]) * (((i + j) & 1) ? -1.0f : 1.0f); |
---|
41 | } |
---|
42 | |
---|
43 | template<> float mat4::det() const |
---|
44 | { |
---|
45 | float ret = 0; |
---|
46 | for (int n = 0; n < 4; n++) |
---|
47 | ret += (*this)[n][0] * cofact3(*this, n, 0); |
---|
48 | return ret; |
---|
49 | } |
---|
50 | |
---|
51 | template<> mat4 mat4::invert() const |
---|
52 | { |
---|
53 | mat4 ret; |
---|
54 | float d = det(); |
---|
55 | if (d) |
---|
56 | { |
---|
57 | d = 1.0f / d; |
---|
58 | for (int j = 0; j < 4; j++) |
---|
59 | for (int i = 0; i < 4; i++) |
---|
60 | ret[j][i] = cofact3(*this, i, j) * d; |
---|
61 | } |
---|
62 | return ret; |
---|
63 | } |
---|
64 | |
---|
65 | template<> mat4 mat4::ortho(float left, float right, float bottom, |
---|
66 | float top, float near, float far) |
---|
67 | { |
---|
68 | float invrl = (right != left) ? 1.0f / (right - left) : 0.0f; |
---|
69 | float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f; |
---|
70 | float invfn = (far != near) ? 1.0f / (far - near) : 0.0f; |
---|
71 | |
---|
72 | mat4 ret(0.0f); |
---|
73 | ret[0][0] = 2.0f * invrl; |
---|
74 | ret[1][1] = 2.0f * invtb; |
---|
75 | ret[2][2] = -2.0f * invfn; |
---|
76 | ret[3][0] = - (right + left) * invrl; |
---|
77 | ret[3][1] = - (top + bottom) * invtb; |
---|
78 | ret[3][2] = - (far + near) * invfn; |
---|
79 | ret[3][3] = 1.0f; |
---|
80 | return ret; |
---|
81 | } |
---|
82 | |
---|
83 | template<> mat4 mat4::frustum(float left, float right, float bottom, |
---|
84 | float top, float near, float far) |
---|
85 | { |
---|
86 | float invrl = (right != left) ? 1.0f / (right - left) : 0.0f; |
---|
87 | float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f; |
---|
88 | float invfn = (far != near) ? 1.0f / (far - near) : 0.0f; |
---|
89 | |
---|
90 | mat4 ret(0.0f); |
---|
91 | ret[0][0] = 2.0f * near * invrl; |
---|
92 | ret[1][1] = 2.0f * near * invtb; |
---|
93 | ret[2][0] = (right + left) * invrl; |
---|
94 | ret[2][1] = (top + bottom) * invtb; |
---|
95 | ret[2][2] = - (far + near) * invfn; |
---|
96 | ret[2][3] = -1.0f; |
---|
97 | ret[3][2] = -2.0f * far * near * invfn; |
---|
98 | return ret; |
---|
99 | } |
---|
100 | |
---|
101 | template<> mat4 mat4::perspective(float theta, float width, |
---|
102 | float height, float near, float far) |
---|
103 | { |
---|
104 | float t1 = tanf(theta / 2.0f); |
---|
105 | float t2 = t1 * height / width; |
---|
106 | |
---|
107 | return frustum(-near * t1, near * t1, -near * t2, near * t2, near, far); |
---|
108 | } |
---|
109 | |
---|
110 | template<> mat4 mat4::translate(float x, float y, float z) |
---|
111 | { |
---|
112 | mat4 ret(1.0f); |
---|
113 | ret[3][0] = x; |
---|
114 | ret[3][1] = y; |
---|
115 | ret[3][2] = z; |
---|
116 | return ret; |
---|
117 | } |
---|
118 | |
---|
119 | template<> mat4 mat4::rotate(float theta, float x, float y, float z) |
---|
120 | { |
---|
121 | float st = sinf(theta); |
---|
122 | float ct = cosf(theta); |
---|
123 | |
---|
124 | float len = sqrtf(x * x + y * y + z * z); |
---|
125 | float invlen = len ? 1.0f / len : 0.0f; |
---|
126 | x *= invlen; |
---|
127 | y *= invlen; |
---|
128 | z *= invlen; |
---|
129 | |
---|
130 | float mtx = (1.0f - ct) * x; |
---|
131 | float mty = (1.0f - ct) * y; |
---|
132 | float mtz = (1.0f - ct) * z; |
---|
133 | |
---|
134 | mat4 ret(1.0f); |
---|
135 | |
---|
136 | ret[0][0] = x * mtx + ct; |
---|
137 | ret[0][1] = x * mty + st * z; |
---|
138 | ret[0][2] = x * mtz - st * y; |
---|
139 | |
---|
140 | ret[1][0] = y * mtx - st * z; |
---|
141 | ret[1][1] = y * mty + ct; |
---|
142 | ret[1][2] = y * mtz + st * x; |
---|
143 | |
---|
144 | ret[2][0] = z * mtx + st * y; |
---|
145 | ret[2][1] = z * mty - st * x; |
---|
146 | ret[2][2] = z * mtz + ct; |
---|
147 | |
---|
148 | return ret; |
---|
149 | } |
---|
150 | |
---|