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