source: trunk/src/matrix.cpp @ 735

Last change on this file since 735 was 735, checked in by sam, 11 years ago

core: get rid of now useless <cstdio> includes.

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