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

Last change on this file since 652 was 652, checked in by sam, 9 years ago

Implement float4x4::frustum() and float4x4::perspective() to build
projection matrices.

• Property svn:keywords set to `Id`
File size: 2.6 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::frustum(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 * near * invrl;
74    ret[1][1] = 2.0f * near * invtb;
75    ret[2][0] = (right + left) * invrl;
76    ret[2][1] = (top + bottom) * invtb;
77    ret[2][2] = - (far + near) * invfn;
78    ret[2][3] = -1.0f;
79    ret[3][2] = -2.0f * far * near * invfn;
80    return ret;
81}
82
83template<> float4x4 float4x4::perspective(float theta, float width,
84                                          float height, float near, float far)
85{
86    float t1 = tanf(theta / 2.0f);
87    float t2 = t1 * height / width;
88
89    return frustum(-near * t1, near * t1, -near * t2, near * t2, near, far);
90}
91
Note: See TracBrowser for help on using the repository browser.