source: abuse/trunk/src/lol/matrix.h @ 555

Last change on this file since 555 was 555, checked in by Sam Hocevar, 11 years ago

ps3: make everything compile on the PS3. Of course, nothing links yet
because so much support is missing.

  • Property svn:keywords set to Id
File size: 7.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//
12// The Matrix classes
13// ------------------
14//
15
16#if !defined __LOL_MATRIX_H__
17#define __LOL_MATRIX_H__
18
19#include <cmath>
20
21namespace lol
22{
23
24#define VECTOR_OP(elems, op) \
25    template<typename U> \
26    inline Vec##elems<T> operator op(Vec##elems<U> const &val) const \
27    { \
28        Vec##elems<T> ret; \
29        for (int n = 0; n < elems; n++) \
30            ret[n] = (*this)[n] op val[n]; \
31        return ret; \
32    } \
33    \
34    template<typename U> \
35    inline Vec##elems<T> operator op##=(Vec##elems<U> const &val) \
36    { \
37        return *this = (*this) op val; \
38    }
39
40#define BOOL_OP(elems, op, op2, ret) \
41    inline bool operator op(Vec##elems<T> const &val) const \
42    { \
43        for (int n = 0; n < elems; n++) \
44            if (!((*this)[n] op2 val[n])) \
45                return !ret; \
46        return ret; \
47    }
48
49#define SCALAR_OP(elems, op) \
50    inline Vec##elems<T> operator op(T const &val) const \
51    { \
52        Vec##elems<T> ret; \
53        for (int n = 0; n < elems; n++) \
54            ret[n] = (*this)[n] op val; \
55        return ret; \
56    } \
57    \
58    inline Vec##elems<T> operator op##=(T const &val) \
59    { \
60        return *this = (*this) op val; \
61    }
62
63#define CAST_OP(elems, dest) \
64    inline operator Vec##dest<T>() const \
65    { \
66        Vec##dest<T> ret; \
67        for (int n = 0; n < elems && n < dest; n++) \
68            ret[n] = (*this)[n]; \
69        return ret; \
70    }
71
72#define OPERATORS(elems) \
73    inline T& operator[](int n) { return *(&x + n); } \
74    inline T const& operator[](int n) const { return *(&x + n); } \
75    \
76    VECTOR_OP(elems, -) \
77    VECTOR_OP(elems, +) \
78    VECTOR_OP(elems, *) \
79    VECTOR_OP(elems, /) \
80    \
81    BOOL_OP(elems, ==, ==, true) \
82    BOOL_OP(elems, !=, ==, false) \
83    BOOL_OP(elems, <=, <=, true) \
84    BOOL_OP(elems, >=, >=, true) \
85    BOOL_OP(elems, <, <, true) \
86    BOOL_OP(elems, >, >, true) \
87    \
88    SCALAR_OP(elems, -) \
89    SCALAR_OP(elems, +) \
90    SCALAR_OP(elems, *) \
91    SCALAR_OP(elems, /) \
92    \
93    CAST_OP(elems, 2) \
94    CAST_OP(elems, 3) \
95    CAST_OP(elems, 4) \
96    \
97    template<typename U> \
98    inline operator Vec##elems<U>() const \
99    { \
100        Vec##elems<U> ret; \
101        for (int n = 0; n < elems; n++) \
102            ret[n] = static_cast<U>((*this)[n]); \
103        return ret; \
104    } \
105    \
106    inline T sqlen() const \
107    { \
108        T acc = 0; \
109        for (int n = 0; n < elems; n++) \
110            acc += (*this)[n] * (*this)[n]; \
111        return acc; \
112    } \
113    \
114    inline float len() const \
115    { \
116        using namespace std; \
117        return sqrtf((float)sqlen()); \
118    }
119
120template <typename T> struct Vec2;
121template <typename T> struct Vec3;
122template <typename T> struct Vec4;
123
124template <typename T> struct Vec2
125{
126    inline Vec2() { }
127    inline Vec2(T val) { x = y = val; }
128    inline Vec2(T _x, T _y) { x = _x; y = _y; }
129
130    OPERATORS(2)
131
132    union { T x; T a; T i; };
133    union { T y; T b; T j; };
134};
135
136typedef Vec2<float> vec2;
137typedef Vec2<int> vec2i;
138
139template <typename T> struct Vec3
140{
141    inline Vec3() { }
142    inline Vec3(T val) { x = y = z = val; }
143    inline Vec3(T _x, T _y, T _z) { x = _x; y = _y; z = _z; }
144
145    OPERATORS(3)
146
147    union { T x; T a; T i; };
148    union { T y; T b; T j; };
149    union { T z; T c; T k; };
150};
151
152typedef Vec3<float> vec3;
153typedef Vec3<int> vec3i;
154
155template <typename T> struct Vec4
156{
157    inline Vec4() { }
158    inline Vec4(T val) { x = y = z = w = val; }
159    inline Vec4(T _x, T _y, T _z, T _w) { x = _x; y = _y; z = _z; w = _w; }
160
161    OPERATORS(4)
162
163    union { T x; T a; T i; };
164    union { T y; T b; T j; };
165    union { T z; T c; T k; };
166    union { T w; T d; T l; };
167};
168
169typedef Vec4<float> vec4;
170typedef Vec4<int> vec4i;
171
172#define SCALAR_GLOBAL(elems, op, U) \
173    template<typename T> \
174    static inline Vec##elems<U> operator op(U const &val, \
175                                            Vec##elems<T> const &that) \
176    { \
177        Vec##elems<U> ret; \
178        for (int n = 0; n < elems; n++) \
179            ret[n] = val op that[n]; \
180        return ret; \
181    }
182
183#define SCALAR_GLOBAL2(elems, op) \
184    SCALAR_GLOBAL(elems, op, int) \
185    SCALAR_GLOBAL(elems, op, float)
186
187#define GLOBALS(elems) \
188    SCALAR_GLOBAL2(elems, -) \
189    SCALAR_GLOBAL2(elems, +) \
190    SCALAR_GLOBAL2(elems, *) \
191    SCALAR_GLOBAL2(elems, /)
192
193GLOBALS(2)
194GLOBALS(3)
195GLOBALS(4)
196
197template <typename T> struct Mat4
198{
199    inline Mat4() { }
200    inline Mat4(T val)
201    {
202        for (int j = 0; j < 4; j++)
203            for (int i = 0; i < 4; i++)
204                v[i][j] = (i == j) ? val : 0;
205    }
206    inline Mat4(Vec4<T> v0, Vec4<T> v1, Vec4<T> v2, Vec4<T> v3)
207    {
208        v[0] = v0; v[1] = v1; v[2] = v2; v[3] = v3;
209    }
210
211    inline Vec4<T>& operator[](int n) { return v[n]; }
212    inline Vec4<T> const& operator[](int n) const { return v[n]; }
213
214    T det() const;
215    Mat4<T> invert() const;
216
217    static Mat4<T> ortho(T left, T right, T bottom, T top, T near, T far);
218    static Mat4<T> frustum(T left, T right, T bottom, T top, T near, T far);
219    static Mat4<T> perspective(T theta, T width, T height, T near, T far);
220    static Mat4<T> translate(T x, T y, T z);
221    static Mat4<T> rotate(T theta, T x, T y, T z);
222
223    void printf() const;
224
225    inline Mat4<T> operator +(Mat4<T> const val) const
226    {
227        Mat4<T> ret;
228        for (int j = 0; j < 4; j++)
229            for (int i = 0; i < 4; i++)
230                ret[i][j] = v[i][j] + val[i][j];
231        return ret;
232    }
233
234    inline Mat4<T> operator +=(Mat4<T> const val)
235    {
236        return *this = *this + val;
237    }
238
239    inline Mat4<T> operator -(Mat4<T> const val) const
240    {
241        Mat4<T> ret;
242        for (int j = 0; j < 4; j++)
243            for (int i = 0; i < 4; i++)
244                ret[i][j] = v[i][j] - val[i][j];
245        return ret;
246    }
247
248    inline Mat4<T> operator -=(Mat4<T> const val)
249    {
250        return *this = *this - val;
251    }
252
253    inline Mat4<T> operator *(Mat4<T> const val) const
254    {
255        Mat4<T> ret;
256        for (int j = 0; j < 4; j++)
257            for (int i = 0; i < 4; i++)
258            {
259                T tmp = 0;
260                for (int k = 0; k < 4; k++)
261                    tmp += v[k][j] * val[i][k];
262                ret[i][j] = tmp;
263            }
264        return ret;
265    }
266
267    inline Mat4<T> operator *=(Mat4<T> const val)
268    {
269        return *this = *this * val;
270    }
271
272    inline Vec4<T> operator *(Vec4<T> const val) const
273    {
274        Vec4<T> ret;
275        for (int j = 0; j < 4; j++)
276        {
277            T tmp = 0;
278            for (int i = 0; i < 4; i++)
279                tmp += v[i][j] * val[i];
280            ret[j] = tmp;
281        }
282        return ret;
283    }
284
285    Vec4<T> v[4];
286};
287
288typedef Mat4<float> mat4;
289typedef Mat4<int> mat4i;
290
291} /* namespace lol */
292
293#endif // __LOL_MATRIX_H__
294
Note: See TracBrowser for help on using the repository browser.