TDME2  1.9.200
Frustum.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 
5 #include <tdme/tdme.h>
6 #include <tdme/engine/fwd-tdme.h>
12 #include <tdme/math/fwd-tdme.h>
13 #include <tdme/math/Matrix4x4.h>
14 
15 using std::array;
16 
23 
24 /**
25  * Frustum class
26  * @author Mark Morley, Andreas Drewke
27  */
29 {
30 private:
31  Renderer* renderer { nullptr };
32 
36 
37  array<Plane, 6> planes;
38 
39 public:
40  // right, left, bottom, top, far, near
41  static constexpr int PLANE_RIGHT { 0 };
42  static constexpr int PLANE_LEFT { 1 };
43  static constexpr int PLANE_BOTTOM { 2 };
44  static constexpr int PLANE_TOP { 3 };
45  static constexpr int PLANE_FAR { 4 };
46  static constexpr int PLANE_NEAR { 5 };
47 
48  // forbid class copy
50 
51  /**
52  * Public constructor
53  */
55 
56  /**
57  * @return planes
58  */
59  inline const array<Plane, 6>& getPlanes() {
60  return planes;
61  }
62 
63  /**
64  * Setups frustum, should be called if frustum did change
65  */
66  void update();
67 
68  /**
69  * Checks if given vertex is in frustum
70  * @param vector vector
71  * @return visibility
72  */
73  inline bool isVisible(const Vector3& vertex) {
74  for (const auto& p: planes) {
75  if (Vector3::computeDotProduct(p.getNormal(), vertex) + p.getDistance() < 0.0f) {
76  return false;
77  }
78  }
79  return true;
80  }
81 
82  /**
83  * Checks if sphere is in frustum
84  * @param s s
85  * @return visibility
86  */
87  inline bool isVisible(Sphere* s) {
88  const auto& center = s->getCenter();
89  auto radius = s->getRadius();
90  for (const auto& p: planes) {
91  if (Vector3::computeDotProduct(p.getNormal(), center) + p.getDistance() < -radius) {
92  return false;
93  }
94  }
95  return true;
96  }
97 
98  /**
99  * Checks if bounding box is in frustum
100  * @param b s
101  * @return visibility
102  */
103  inline bool isVisible(BoundingBox* b) {
104  auto minX = b->getMin()[0];
105  auto minY = b->getMin()[1];
106  auto minZ = b->getMin()[2];
107  auto maxX = b->getMax()[0];
108  auto maxY = b->getMax()[1];
109  auto maxZ = b->getMax()[2];
110  Vector3 point;
111  for (const auto& p :planes) {
112  const auto& normal = p.getNormal();
113  auto distance = p.getDistance();
114  if (Vector3::computeDotProduct(normal, point.set(minX, minY, minZ)) + distance > 0.0f) continue;
115  if (Vector3::computeDotProduct(normal, point.set(maxX, minY, minZ)) + distance > 0.0f) continue;
116  if (Vector3::computeDotProduct(normal, point.set(minX, maxY, minZ)) + distance > 0.0f) continue;
117  if (Vector3::computeDotProduct(normal, point.set(maxX, maxY, minZ)) + distance > 0.0f) continue;
118  if (Vector3::computeDotProduct(normal, point.set(minX, minY, maxZ)) + distance > 0.0f) continue;
119  if (Vector3::computeDotProduct(normal, point.set(maxX, minY, maxZ)) + distance > 0.0f) continue;
120  if (Vector3::computeDotProduct(normal, point.set(minX, maxY, maxZ)) + distance > 0.0f) continue;
121  if (Vector3::computeDotProduct(normal, point.set(maxX, maxY, maxZ)) + distance > 0.0f) continue;
122  return false;
123  }
124  return true;
125  }
126 
127 };
Frustum class.
Definition: Frustum.h:29
static constexpr int PLANE_RIGHT
Definition: Frustum.h:41
static constexpr int PLANE_NEAR
Definition: Frustum.h:46
static constexpr int PLANE_LEFT
Definition: Frustum.h:42
array< Plane, 6 > planes
Definition: Frustum.h:37
static constexpr int PLANE_FAR
Definition: Frustum.h:45
static constexpr int PLANE_BOTTOM
Definition: Frustum.h:43
bool isVisible(BoundingBox *b)
Checks if bounding box is in frustum.
Definition: Frustum.h:103
Matrix4x4 modelViewMatrixTransposed
Definition: Frustum.h:34
bool isVisible(const Vector3 &vertex)
Checks if given vertex is in frustum.
Definition: Frustum.h:73
static constexpr int PLANE_TOP
Definition: Frustum.h:44
const array< Plane, 6 > & getPlanes()
Definition: Frustum.h:59
Matrix4x4 frustumMatrix
Definition: Frustum.h:35
bool isVisible(Sphere *s)
Checks if sphere is in frustum.
Definition: Frustum.h:87
void update()
Setups frustum, should be called if frustum did change.
Definition: Frustum.cpp:30
Matrix4x4 projectionMatrixTransposed
Definition: Frustum.h:33
Frustum(Renderer *renderer)
Public constructor.
Definition: Frustum.cpp:25
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
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
Vector3 & set(float x, float y, float z)
Sets this vector3 by its components.
Definition: Vector3.h:70
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6