source: abuse/trunk/src/lol/matrix.cpp @ 682

Last change on this file since 682 was 682, checked in by Sam Hocevar, 8 years ago

core: rename vec2i to ivec2 and update matrix.h from Lol Engine.

  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
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
20using namespace std;
21
22namespace lol
23{
24
25static 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
34static 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
47template<> 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
55template<> 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
69template<> 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#if !defined __ANDROID__
86template<> std::ostream &operator<<(std::ostream &stream, ivec2 const &v)
87{
88    return stream << "(" << v.x << ", " << v.y << ")";
89}
90
91template<> std::ostream &operator<<(std::ostream &stream, ivec3 const &v)
92{
93    return stream << "(" << v.x << ", " << v.y << ", " << v.z << ")";
94}
95
96template<> std::ostream &operator<<(std::ostream &stream, ivec4 const &v)
97{
98    return stream << "(" << v.x << ", " << v.y << ", "
99                         << v.z << ", " << v.w << ")";
100}
101
102template<> std::ostream &operator<<(std::ostream &stream, vec2 const &v)
103{
104    return stream << "(" << v.x << ", " << v.y << ")";
105}
106
107template<> std::ostream &operator<<(std::ostream &stream, vec3 const &v)
108{
109    return stream << "(" << v.x << ", " << v.y << ", " << v.z << ")";
110}
111
112template<> std::ostream &operator<<(std::ostream &stream, vec4 const &v)
113{
114    return stream << "(" << v.x << ", " << v.y << ", "
115                         << v.z << ", " << v.w << ")";
116}
117
118template<> std::ostream &operator<<(std::ostream &stream, mat4 const &m)
119{
120    stream << "((" << m[0][0] << ", " << m[1][0]
121            << ", " << m[2][0] << ", " << m[3][0] << "), ";
122    stream << "(" << m[0][1] << ", " << m[1][1]
123           << ", " << m[2][1] << ", " << m[3][1] << "), ";
124    stream << "(" << m[0][2] << ", " << m[1][2]
125           << ", " << m[2][2] << ", " << m[3][2] << "), ";
126    stream << "(" << m[0][3] << ", " << m[1][3]
127           << ", " << m[2][3] << ", " << m[3][3] << "))";
128    return stream;
129}
130#endif
131
132template<> mat4 mat4::ortho(float left, float right, float bottom,
133                            float top, float near, float far)
134{
135    float invrl = (right != left) ? 1.0f / (right - left) : 0.0f;
136    float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f;
137    float invfn = (far != near) ? 1.0f / (far - near) : 0.0f;
138
139    mat4 ret(0.0f);
140    ret[0][0] = 2.0f * invrl;
141    ret[1][1] = 2.0f * invtb;
142    ret[2][2] = -2.0f * invfn;
143    ret[3][0] = - (right + left) * invrl;
144    ret[3][1] = - (top + bottom) * invtb;
145    ret[3][2] = - (far + near) * invfn;
146    ret[3][3] = 1.0f;
147    return ret;
148}
149
150template<> mat4 mat4::frustum(float left, float right, float bottom,
151                              float top, float near, float far)
152{
153    float invrl = (right != left) ? 1.0f / (right - left) : 0.0f;
154    float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f;
155    float invfn = (far != near) ? 1.0f / (far - near) : 0.0f;
156
157    mat4 ret(0.0f);
158    ret[0][0] = 2.0f * near * invrl;
159    ret[1][1] = 2.0f * near * invtb;
160    ret[2][0] = (right + left) * invrl;
161    ret[2][1] = (top + bottom) * invtb;
162    ret[2][2] = - (far + near) * invfn;
163    ret[2][3] = -1.0f;
164    ret[3][2] = -2.0f * far * near * invfn;
165    return ret;
166}
167
168template<> mat4 mat4::perspective(float theta, float width,
169                                  float height, float near, float far)
170{
171    float t1 = tanf(theta / 2.0f);
172    float t2 = t1 * height / width;
173
174    return frustum(-near * t1, near * t1, -near * t2, near * t2, near, far);
175}
176
177template<> mat4 mat4::translate(float x, float y, float z)
178{
179    mat4 ret(1.0f);
180    ret[3][0] = x;
181    ret[3][1] = y;
182    ret[3][2] = z;
183    return ret;
184}
185
186template<> mat4 mat4::rotate(float theta, float x, float y, float z)
187{
188    float st = sinf(theta);
189    float ct = cosf(theta);
190
191    float len = sqrtf(x * x + y * y + z * z);
192    float invlen = len ? 1.0f / len : 0.0f;
193    x *= invlen;
194    y *= invlen;
195    z *= invlen;
196
197    float mtx = (1.0f - ct) * x;
198    float mty = (1.0f - ct) * y;
199    float mtz = (1.0f - ct) * z;
200
201    mat4 ret(1.0f);
202
203    ret[0][0] = x * mtx + ct;
204    ret[0][1] = x * mty + st * z;
205    ret[0][2] = x * mtz - st * y;
206
207    ret[1][0] = y * mtx - st * z;
208    ret[1][1] = y * mty + ct;
209    ret[1][2] = y * mtz + st * x;
210
211    ret[2][0] = z * mtx + st * y;
212    ret[2][1] = z * mty - st * x;
213    ret[2][2] = z * mtz + ct;
214
215    return ret;
216}
217
218} /* namespace lol */
219
Note: See TracBrowser for help on using the repository browser.