TDME2
1.9.121
src
tdme
engine
Frustum.cpp
Go to the documentation of this file.
1
#include <
tdme/engine/Frustum.h
>
2
3
#include <array>
4
5
#include <
tdme/tdme.h
>
6
#include <
tdme/engine/primitives/BoundingBox.h
>
7
#include <
tdme/engine/primitives/Plane.h
>
8
#include <
tdme/engine/primitives/Sphere.h
>
9
#include <
tdme/engine/subsystems/renderer/Renderer.h
>
10
#include <
tdme/math/Math.h
>
11
#include <
tdme/math/Matrix4x4.h
>
12
#include <
tdme/math/Vector3.h
>
13
14
using
std::array;
15
16
using
tdme::engine::primitives::BoundingBox
;
17
using
tdme::engine::primitives::Plane
;
18
using
tdme::engine::primitives::Sphere
;
19
using
tdme::engine::subsystems::renderer::Renderer
;
20
using
tdme::engine::Frustum
;
21
using
tdme::math::Math
;
22
using
tdme::math::Matrix4x4
;
23
using
tdme::math::Vector3
;
24
25
Frustum::Frustum(
Renderer
* renderer)
26
{
27
this->renderer =
renderer
;
28
}
29
30
void
Frustum::update
()
31
{
32
// see: http://www.crownandcutlass.com/features/technicaldetails/frustum.html
33
projectionMatrixTransposed
.
set
(
renderer
->
getProjectionMatrix
()).
transpose
();
34
modelViewMatrixTransposed
.
set
(
renderer
->
getModelViewMatrix
()).
transpose
();
35
frustumMatrix
.
set
(
projectionMatrixTransposed
).
multiply
(
modelViewMatrixTransposed
);
36
float
x, y, z, d, t;
37
38
// right plane
39
x =
frustumMatrix
[12] -
frustumMatrix
[0];
40
y =
frustumMatrix
[13] -
frustumMatrix
[1];
41
z =
frustumMatrix
[14] -
frustumMatrix
[2];
42
d =
frustumMatrix
[15] -
frustumMatrix
[3];
43
44
// normalize
45
t = Math::sqrt((x * x) + (y * y) + (z * z));
46
x /= t;
47
y /= t;
48
z /= t;
49
d /= t;
50
51
// setup plane
52
planes
[0].setNormal(
Vector3
(x, y, z));
53
planes
[0].setDistance(d);
54
55
// left plane
56
x =
frustumMatrix
[12] +
frustumMatrix
[0];
57
y =
frustumMatrix
[13] +
frustumMatrix
[1];
58
z =
frustumMatrix
[14] +
frustumMatrix
[2];
59
d =
frustumMatrix
[15] +
frustumMatrix
[3];
60
61
// normalize
62
t = Math::sqrt((x * x) + (y * y) + (z * z));
63
x /= t;
64
y /= t;
65
z /= t;
66
d /= t;
67
68
// setup plane
69
planes
[1].setNormal(
Vector3
(x, y, z));
70
planes
[1].setDistance(d);
71
72
// bottom plane
73
x =
frustumMatrix
[12] +
frustumMatrix
[4];
74
y =
frustumMatrix
[13] +
frustumMatrix
[5];
75
z =
frustumMatrix
[14] +
frustumMatrix
[6];
76
d =
frustumMatrix
[15] +
frustumMatrix
[7];
77
78
// normalize
79
t = Math::sqrt((x * x) + (y * y) + (z * z));
80
x /= t;
81
y /= t;
82
z /= t;
83
d /= t;
84
85
// setup plane
86
planes
[2].setNormal(
Vector3
(x, y, z));
87
planes
[2].setDistance(d);
88
89
// top plane
90
x =
frustumMatrix
[12] -
frustumMatrix
[4];
91
y =
frustumMatrix
[13] -
frustumMatrix
[5];
92
z =
frustumMatrix
[14] -
frustumMatrix
[6];
93
d =
frustumMatrix
[15] -
frustumMatrix
[7];
94
95
// normalize
96
t = Math::sqrt((x * x) + (y * y) + (z * z));
97
x /= t;
98
y /= t;
99
z /= t;
100
d /= t;
101
102
// setup plane
103
planes
[3].setNormal(
Vector3
(x, y, z));
104
planes
[3].setDistance(d);
105
106
// far plane
107
x =
frustumMatrix
[12] -
frustumMatrix
[8];
108
y =
frustumMatrix
[13] -
frustumMatrix
[9];
109
z =
frustumMatrix
[14] -
frustumMatrix
[10];
110
d =
frustumMatrix
[15] -
frustumMatrix
[11];
111
112
// normalize
113
t = Math::sqrt((x * x) + (y * y) + (z * z));
114
x /= t;
115
y /= t;
116
z /= t;
117
d /= t;
118
119
// setup plane
120
planes
[4].setNormal(
Vector3
(x, y, z));
121
planes
[4].setDistance(d);
122
123
// near plane
124
x =
frustumMatrix
[12] +
frustumMatrix
[8];
125
y =
frustumMatrix
[13] +
frustumMatrix
[9];
126
z =
frustumMatrix
[14] +
frustumMatrix
[10];
127
d =
frustumMatrix
[15] +
frustumMatrix
[11];
128
129
// normalize
130
t = Math::sqrt((x * x) + (y * y) + (z * z));
131
x /= t;
132
y /= t;
133
z /= t;
134
d /= t;
135
136
// setup plane
137
planes
[5].setNormal(
Vector3
(x, y, z));
138
planes
[5].setDistance(d);
139
}
140
BoundingBox.h
Frustum.h
Math.h
Matrix4x4.h
Plane.h
Renderer.h
Sphere.h
Vector3.h
tdme::engine::Frustum
Frustum class.
Definition:
Frustum.h:30
tdme::engine::Frustum::planes
array< Plane, 6 > planes
Definition:
Frustum.h:38
tdme::engine::Frustum::modelViewMatrixTransposed
Matrix4x4 modelViewMatrixTransposed
Definition:
Frustum.h:35
tdme::engine::Frustum::frustumMatrix
Matrix4x4 frustumMatrix
Definition:
Frustum.h:36
tdme::engine::Frustum::update
void update()
Setups frustum, should be called if frustum did change.
Definition:
Frustum.cpp:30
tdme::engine::Frustum::projectionMatrixTransposed
Matrix4x4 projectionMatrixTransposed
Definition:
Frustum.h:34
tdme::engine::Frustum::renderer
Renderer * renderer
Definition:
Frustum.h:32
tdme::engine::primitives::BoundingBox
Axis aligned bounding box used for frustum, this is not directly connectable with physics engine.
Definition:
BoundingBox.h:25
tdme::engine::primitives::Plane
Plane entity, this is not directly connectable with physics engine.
Definition:
Plane.h:16
tdme::engine::primitives::Sphere
Sphere physics primitive.
Definition:
Sphere.h:18
tdme::engine::subsystems::renderer::Renderer
Renderer interface.
Definition:
Renderer.h:45
tdme::engine::subsystems::renderer::Renderer::getProjectionMatrix
Matrix4x4 & getProjectionMatrix()
Definition:
Renderer.h:530
tdme::engine::subsystems::renderer::Renderer::getModelViewMatrix
Matrix4x4 & getModelViewMatrix()
Definition:
Renderer.h:556
tdme::math::Math
Standard math functions.
Definition:
Math.h:21
tdme::math::Matrix4x4
4x4 3D Matrix class
Definition:
Matrix4x4.h:24
tdme::math::Matrix4x4::set
Matrix4x4 & set(float r0c0, float r1c0, float r2c0, float r3c0, float r0c1, float r1c1, float r2c1, float r3c1, float r0c2, float r1c2, float r2c2, float r3c2, float r0c3, float r1c3, float r2c3, float r3c3)
Set up matrix by values.
Definition:
Matrix4x4.h:95
tdme::math::Matrix4x4::transpose
Matrix4x4 & transpose()
Transposes this matrix.
Definition:
Matrix4x4.h:510
tdme::math::Matrix4x4::multiply
Vector3 multiply(const Vector3 &v) const
Multiplies a vector3 with this matrix into destination vector.
Definition:
Matrix4x4.h:351
tdme::math::Vector3
3D vector 3 class
Definition:
Vector3.h:22
tdme.h
Generated by
1.9.3