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

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

Implement float4x4::ortho() and float4x4::translate().

• Property svn:keywords set to `Id`
File size: 3.4 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 <cstdio>
16#include <cstdlib> /* free() */
17#include <cstring> /* strdup() */
18
19#include "core.h"
20
21static inline float det3(float a, float b, float c,
22                         float d, float e, float f,
23                         float g, float h, float i)
24{
25    return a * (e * i - h * f)
26         + b * (f * g - i * d)
27         + c * (d * h - g * e);
28}
29
30static inline float cofact3(float4x4 const &mat, int i, int j)
31{
32    return det3(mat[(i + 1) & 3][(j + 1) & 3],
33                mat[(i + 2) & 3][(j + 1) & 3],
34                mat[(i + 3) & 3][(j + 1) & 3],
35                mat[(i + 1) & 3][(j + 2) & 3],
36                mat[(i + 2) & 3][(j + 2) & 3],
37                mat[(i + 3) & 3][(j + 2) & 3],
38                mat[(i + 1) & 3][(j + 3) & 3],
39                mat[(i + 2) & 3][(j + 3) & 3],
40                mat[(i + 3) & 3][(j + 3) & 3]) * (((i + j) & 1) ? -1.0f : 1.0f);
41}
42
43template<> float float4x4::det() const
44{
45    float ret = 0;
46    for (int n = 0; n < 4; n++)
47        ret += (*this)[n][0] * cofact3(*this, n, 0);
48    return ret;
49}
50
51template<> float4x4 float4x4::invert() const
52{
53    float4x4 ret;
54    float d = det();
55    if (d)
56    {
57        d = 1.0f / d;
58        for (int j = 0; j < 4; j++)
59            for (int i = 0; i < 4; i++)
60                ret[j][i] = cofact3(*this, i, j) * d;
61    }
62    return ret;
63}
64
65template<> float4x4 float4x4::ortho(float left, float right, float bottom,
66                                    float top, float near, float far)
67{
68    float invrl = (right != left) ? 1.0f / (right - left) : 0.0f;
69    float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f;
70    float invfn = (far != near) ? 1.0f / (far - near) : 0.0f;
71
72    float4x4 ret(0.0f);
73    ret[0][0] = 2.0f * invrl;
74    ret[1][1] = 2.0f * invtb;
75    ret[2][2] = -2.0f * invfn;
76    ret[3][0] = - (right + left) * invrl;
77    ret[3][1] = - (top + bottom) * invtb;
78    ret[3][2] = - (far + near) * invfn;
79    ret[3][3] = 1.0f;
80    return ret;
81}
82
83template<> float4x4 float4x4::frustum(float left, float right, float bottom,
84                                      float top, float near, float far)
85{
86    float invrl = (right != left) ? 1.0f / (right - left) : 0.0f;
87    float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f;
88    float invfn = (far != near) ? 1.0f / (far - near) : 0.0f;
89
90    float4x4 ret(0.0f);
91    ret[0][0] = 2.0f * near * invrl;
92    ret[1][1] = 2.0f * near * invtb;
93    ret[2][0] = (right + left) * invrl;
94    ret[2][1] = (top + bottom) * invtb;
95    ret[2][2] = - (far + near) * invfn;
96    ret[2][3] = -1.0f;
97    ret[3][2] = -2.0f * far * near * invfn;
98    return ret;
99}
100
101template<> float4x4 float4x4::perspective(float theta, float width,
102                                          float height, float near, float far)
103{
104    float t1 = tanf(theta / 2.0f);
105    float t2 = t1 * height / width;
106
107    return frustum(-near * t1, near * t1, -near * t2, near * t2, near, far);
108}
109
110template<> float4x4 float4x4::translate(float x, float y, float z)
111{
112    float4x4 ret(1.0f);
113    ret[3][0] = x;
114    ret[3][1] = y;
115    ret[3][2] = z;
116    return ret;
117}
118
Note: See TracBrowser for help on using the repository browser.