source: trunk/src/matrix.cpp @ 686

Last change on this file since 686 was 686, checked in by sam, 10 years ago

Put everything in the "lol" namespace. Better late than never.

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