source: trunk/src/matrix.cpp @ 939

Last change on this file since 939 was 939, checked in by sam, 9 years ago

build: stop defining ANDROID_NDK and check for ANDROID instead.

According to David Turner, “it should be the only thing that NDK users
should be testing again.”

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