TDME2  1.9.200
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>
10 #include <tdme/math/Math.h>
11 #include <tdme/math/Matrix4x4.h>
12 #include <tdme/math/Vector3.h>
13 
14 using std::array;
15 
21 using tdme::math::Math;
24 
25 Frustum::Frustum(Renderer* renderer)
26 {
27  this->renderer = renderer;
28 }
29 
31 {
32  // see: http://www.crownandcutlass.com/features/technicaldetails/frustum.html
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 
Frustum class.
Definition: Frustum.h:29
array< Plane, 6 > planes
Definition: Frustum.h:37
Matrix4x4 modelViewMatrixTransposed
Definition: Frustum.h:34
Matrix4x4 frustumMatrix
Definition: Frustum.h:35
void update()
Setups frustum, should be called if frustum did change.
Definition: Frustum.cpp:30
Matrix4x4 projectionMatrixTransposed
Definition: Frustum.h:33
Renderer * renderer
Definition: Frustum.h:31
Axis aligned bounding box used for frustum, this is not directly connectable with physics engine.
Definition: BoundingBox.h:26
Plane entity, this is not directly connectable with physics engine.
Definition: Plane.h:15
Sphere physics primitive.
Definition: Sphere.h:19
Standard math functions.
Definition: Math.h:19
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
Vector3 multiply(const Vector3 &vector3) const
Multiplies this matrix with vector3.
Definition: Matrix4x4.h:225
Matrix4x4 & set(float r0c0, float r0c1, float r0c2, float r0c3, float r1c0, float r1c1, float r1c2, float r1c3, float r2c0, float r2c1, float r2c2, float r2c3, float r3c0, float r3c1, float r3c2, float r3c3)
Sets this matrix by its components.
Definition: Matrix4x4.h:108
Matrix4x4 & transpose()
Transposes this matrix.
Definition: Matrix4x4.h:453
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20