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