Pyrogenesis  trunk
Frustum.h
Go to the documentation of this file.
1 /* Copyright (C) 2021 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * CFrustum is a collection of planes which define a viewing space.
20  */
21 
22 /*
23 Usually associated with the camera, there are 6 planes which define the
24 view pyramid. But we allow more planes per frustum which may be used for
25 portal rendering, where a portal may have 3 or more edges.
26 */
27 
28 #ifndef INCLUDED_FRUSTUM
29 #define INCLUDED_FRUSTUM
30 
31 #include "maths/Plane.h"
32 
34 class CMatrix3D;
35 
36 class CFrustum
37 {
38 public:
39  CFrustum();
40  ~CFrustum();
41 
42  // Set the number of planes to use for calculations. This is clamped to
43  // [0, MAX_NUM_FRUSTUM_PLANES].
44  void SetNumPlanes(size_t num);
45 
46  size_t GetNumPlanes() const { return m_NumPlanes; }
47 
48  void AddPlane(const CPlane& plane);
49 
50  void Transform(const CMatrix3D& m);
51 
52  // The following methods return true if the shape is
53  // partially or completely in front of the frustum planes.
54  bool IsPointVisible(const CVector3D& point) const;
55  bool DoesSegmentIntersect(const CVector3D& start, const CVector3D& end) const;
56  bool IsSphereVisible(const CVector3D& center, float radius) const;
57  bool IsBoxVisible(const CVector3D& position, const CBoundingBoxAligned& bounds) const;
58  bool IsBoxVisible(const CBoundingBoxAligned& bounds) const;
59 
60  CPlane& operator[](size_t idx) { return m_Planes[idx]; }
61  const CPlane& operator[](size_t idx) const { return m_Planes[idx]; }
62 
63 private:
64  static const size_t MAX_NUM_FRUSTUM_PLANES = 10;
65 
67  size_t m_NumPlanes;
68 };
69 
70 #endif // INCLUDED_FRUSTUM
size_t GetNumPlanes() const
Definition: Frustum.h:46
bool IsBoxVisible(const CVector3D &position, const CBoundingBoxAligned &bounds) const
Definition: Frustum.cpp:118
CFrustum()
Definition: Frustum.cpp:32
Definition: Vector3D.h:30
Definition: Frustum.h:36
static const size_t MAX_NUM_FRUSTUM_PLANES
Definition: Frustum.h:64
Definition: Matrix3D.h:33
CPlane & operator[](size_t idx)
Definition: Frustum.h:60
size_t m_NumPlanes
Definition: Frustum.h:67
bool DoesSegmentIntersect(const CVector3D &start, const CVector3D &end) const
Definition: Frustum.cpp:85
void AddPlane(const CPlane &plane)
Definition: Frustum.cpp:52
~CFrustum()
Definition: Frustum.cpp:37
bool IsSphereVisible(const CVector3D &center, float radius) const
Definition: Frustum.cpp:105
const CPlane & operator[](size_t idx) const
Definition: Frustum.h:61
bool IsPointVisible(const CVector3D &point) const
Definition: Frustum.cpp:74
Definition: BoundingBoxAligned.h:33
CPlane m_Planes[MAX_NUM_FRUSTUM_PLANES]
Definition: Frustum.h:66
void SetNumPlanes(size_t num)
Definition: Frustum.cpp:41
void Transform(const CMatrix3D &m)
Definition: Frustum.cpp:63
Definition: Plane.h:38