# source:trunk/src/matrix.cpp@929Tweet

Last change on this file since 929 was 929, checked in by sam, 12 years ago

core: add std::ostream operators for vector and matrix classes.

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