doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Frustum.h
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __BV_FRUSTUM_H__
30 #define __BV_FRUSTUM_H__
31 
32 /*
33 ===============================================================================
34 
35  Orthogonal Frustum
36 
37 ===============================================================================
38 */
39 
40 class idFrustum {
41 public:
42  idFrustum( void );
43 
44  void SetOrigin( const idVec3 &origin );
45  void SetAxis( const idMat3 &axis );
46  void SetSize( float dNear, float dFar, float dLeft, float dUp );
47  void SetPyramid( float dNear, float dFar );
48  void MoveNearDistance( float dNear );
49  void MoveFarDistance( float dFar );
50 
51  const idVec3 & GetOrigin( void ) const; // returns frustum origin
52  const idMat3 & GetAxis( void ) const; // returns frustum orientation
53  idVec3 GetCenter( void ) const; // returns center of frustum
54 
55  bool IsValid( void ) const; // returns true if the frustum is valid
56  float GetNearDistance( void ) const; // returns distance to near plane
57  float GetFarDistance( void ) const; // returns distance to far plane
58  float GetLeft( void ) const; // returns left vector length
59  float GetUp( void ) const; // returns up vector length
60 
61  idFrustum Expand( const float d ) const; // returns frustum expanded in all directions with the given value
62  idFrustum & ExpandSelf( const float d ); // expands frustum in all directions with the given value
63  idFrustum Translate( const idVec3 &translation ) const; // returns translated frustum
64  idFrustum & TranslateSelf( const idVec3 &translation ); // translates frustum
65  idFrustum Rotate( const idMat3 &rotation ) const; // returns rotated frustum
66  idFrustum & RotateSelf( const idMat3 &rotation ); // rotates frustum
67 
68  float PlaneDistance( const idPlane &plane ) const;
69  int PlaneSide( const idPlane &plane, const float epsilon = ON_EPSILON ) const;
70 
71  // fast culling but might not cull everything outside the frustum
72  bool CullPoint( const idVec3 &point ) const;
73  bool CullBounds( const idBounds &bounds ) const;
74  bool CullBox( const idBox &box ) const;
75  bool CullSphere( const idSphere &sphere ) const;
76  bool CullFrustum( const idFrustum &frustum ) const;
77  bool CullWinding( const class idWinding &winding ) const;
78 
79  // exact intersection tests
80  bool ContainsPoint( const idVec3 &point ) const;
81  bool IntersectsBounds( const idBounds &bounds ) const;
82  bool IntersectsBox( const idBox &box ) const;
83  bool IntersectsSphere( const idSphere &sphere ) const;
84  bool IntersectsFrustum( const idFrustum &frustum ) const;
85  bool IntersectsWinding( const idWinding &winding ) const;
86  bool LineIntersection( const idVec3 &start, const idVec3 &end ) const;
87  bool RayIntersection( const idVec3 &start, const idVec3 &dir, float &scale1, float &scale2 ) const;
88 
89  // returns true if the projection origin is far enough away from the bounding volume to create a valid frustum
90  bool FromProjection( const idBounds &bounds, const idVec3 &projectionOrigin, const float dFar );
91  bool FromProjection( const idBox &box, const idVec3 &projectionOrigin, const float dFar );
92  bool FromProjection( const idSphere &sphere, const idVec3 &projectionOrigin, const float dFar );
93 
94  // moves the far plane so it extends just beyond the bounding volume
95  bool ConstrainToBounds( const idBounds &bounds );
96  bool ConstrainToBox( const idBox &box );
97  bool ConstrainToSphere( const idSphere &sphere );
98  bool ConstrainToFrustum( const idFrustum &frustum );
99 
100  void ToPlanes( idPlane planes[6] ) const; // planes point outwards
101  void ToPoints( idVec3 points[8] ) const; // 8 corners of the frustum
102 
103  // calculates the projection of this frustum onto the given axis
104  void AxisProjection( const idVec3 &dir, float &min, float &max ) const;
105  void AxisProjection( const idMat3 &ax, idBounds &bounds ) const;
106 
107  // calculates the bounds for the projection in this frustum
108  bool ProjectionBounds( const idBounds &bounds, idBounds &projectionBounds ) const;
109  bool ProjectionBounds( const idBox &box, idBounds &projectionBounds ) const;
110  bool ProjectionBounds( const idSphere &sphere, idBounds &projectionBounds ) const;
111  bool ProjectionBounds( const idFrustum &frustum, idBounds &projectionBounds ) const;
112  bool ProjectionBounds( const idWinding &winding, idBounds &projectionBounds ) const;
113 
114  // calculates the bounds for the projection in this frustum of the given frustum clipped to the given box
115  bool ClippedProjectionBounds( const idFrustum &frustum, const idBox &clipBox, idBounds &projectionBounds ) const;
116 
117 private:
118  idVec3 origin; // frustum origin
119  idMat3 axis; // frustum orientation
120  float dNear; // distance of near plane, dNear >= 0.0f
121  float dFar; // distance of far plane, dFar > dNear
122  float dLeft; // half the width at the far plane
123  float dUp; // half the height at the far plane
124  float invFar; // 1.0f / dFar
125 
126 private:
127  bool CullLocalBox( const idVec3 &localOrigin, const idVec3 &extents, const idMat3 &localAxis ) const;
128  bool CullLocalFrustum( const idFrustum &localFrustum, const idVec3 indexPoints[8], const idVec3 cornerVecs[4] ) const;
129  bool CullLocalWinding( const idVec3 *points, const int numPoints, int *pointCull ) const;
130  bool BoundsCullLocalFrustum( const idBounds &bounds, const idFrustum &localFrustum, const idVec3 indexPoints[8], const idVec3 cornerVecs[4] ) const;
131  bool LocalLineIntersection( const idVec3 &start, const idVec3 &end ) const;
132  bool LocalRayIntersection( const idVec3 &start, const idVec3 &dir, float &scale1, float &scale2 ) const;
133  bool LocalFrustumIntersectsFrustum( const idVec3 points[8], const bool testFirstSide ) const;
134  bool LocalFrustumIntersectsBounds( const idVec3 points[8], const idBounds &bounds ) const;
135  void ToClippedPoints( const float fractions[4], idVec3 points[8] ) const;
136  void ToIndexPoints( idVec3 indexPoints[8] ) const;
137  void ToIndexPointsAndCornerVecs( idVec3 indexPoints[8], idVec3 cornerVecs[4] ) const;
138  void AxisProjection( const idVec3 indexPoints[8], const idVec3 cornerVecs[4], const idVec3 &dir, float &min, float &max ) const;
139  void AddLocalLineToProjectionBoundsSetCull( const idVec3 &start, const idVec3 &end, int &startCull, int &endCull, idBounds &bounds ) const;
140  void AddLocalLineToProjectionBoundsUseCull( const idVec3 &start, const idVec3 &end, int startCull, int endCull, idBounds &bounds ) const;
141  bool AddLocalCapsToProjectionBounds( const idVec3 endPoints[4], const int endPointCull[4], const idVec3 &point, int pointCull, int pointClip, idBounds &projectionBounds ) const;
142  bool BoundsRayIntersection( const idBounds &bounds, const idVec3 &start, const idVec3 &dir, float &scale1, float &scale2 ) const;
143  void ClipFrustumToBox( const idBox &box, float clipFractions[4], int clipPlanes[4] ) const;
144  bool ClipLine( const idVec3 localPoints[8], const idVec3 points[8], int startIndex, int endIndex, idVec3 &start, idVec3 &end, int &startClip, int &endClip ) const;
145 };
146 
147 
148 ID_INLINE idFrustum::idFrustum( void ) {
149  dNear = dFar = 0.0f;
150 }
151 
152 ID_INLINE void idFrustum::SetOrigin( const idVec3 &origin ) {
153  this->origin = origin;
154 }
155 
156 ID_INLINE void idFrustum::SetAxis( const idMat3 &axis ) {
157  this->axis = axis;
158 }
159 
160 ID_INLINE void idFrustum::SetSize( float dNear, float dFar, float dLeft, float dUp ) {
161  assert( dNear >= 0.0f && dFar > dNear && dLeft > 0.0f && dUp > 0.0f );
162  this->dNear = dNear;
163  this->dFar = dFar;
164  this->dLeft = dLeft;
165  this->dUp = dUp;
166  this->invFar = 1.0f / dFar;
167 }
168 
169 ID_INLINE void idFrustum::SetPyramid( float dNear, float dFar ) {
170  assert( dNear >= 0.0f && dFar > dNear );
171  this->dNear = dNear;
172  this->dFar = dFar;
173  this->dLeft = dFar;
174  this->dUp = dFar;
175  this->invFar = 1.0f / dFar;
176 }
177 
178 ID_INLINE void idFrustum::MoveNearDistance( float dNear ) {
179  assert( dNear >= 0.0f );
180  this->dNear = dNear;
181 }
182 
183 ID_INLINE void idFrustum::MoveFarDistance( float dFar ) {
184  assert( dFar > this->dNear );
185  float scale = dFar / this->dFar;
186  this->dFar = dFar;
187  this->dLeft *= scale;
188  this->dUp *= scale;
189  this->invFar = 1.0f / dFar;
190 }
191 
192 ID_INLINE const idVec3 &idFrustum::GetOrigin( void ) const {
193  return origin;
194 }
195 
196 ID_INLINE const idMat3 &idFrustum::GetAxis( void ) const {
197  return axis;
198 }
199 
200 ID_INLINE idVec3 idFrustum::GetCenter( void ) const {
201  return ( origin + axis[0] * ( ( dFar - dNear ) * 0.5f ) );
202 }
203 
204 ID_INLINE bool idFrustum::IsValid( void ) const {
205  return ( dFar > dNear );
206 }
207 
208 ID_INLINE float idFrustum::GetNearDistance( void ) const {
209  return dNear;
210 }
211 
212 ID_INLINE float idFrustum::GetFarDistance( void ) const {
213  return dFar;
214 }
215 
216 ID_INLINE float idFrustum::GetLeft( void ) const {
217  return dLeft;
218 }
219 
220 ID_INLINE float idFrustum::GetUp( void ) const {
221  return dUp;
222 }
223 
224 ID_INLINE idFrustum idFrustum::Expand( const float d ) const {
225  idFrustum f = *this;
226  f.origin -= d * f.axis[0];
227  f.dFar += 2.0f * d;
228  f.dLeft = f.dFar * dLeft * invFar;
229  f.dUp = f.dFar * dUp * invFar;
230  f.invFar = 1.0f / dFar;
231  return f;
232 }
233 
234 ID_INLINE idFrustum &idFrustum::ExpandSelf( const float d ) {
235  origin -= d * axis[0];
236  dFar += 2.0f * d;
237  dLeft = dFar * dLeft * invFar;
238  dUp = dFar * dUp * invFar;
239  invFar = 1.0f / dFar;
240  return *this;
241 }
242 
243 ID_INLINE idFrustum idFrustum::Translate( const idVec3 &translation ) const {
244  idFrustum f = *this;
245  f.origin += translation;
246  return f;
247 }
248 
249 ID_INLINE idFrustum &idFrustum::TranslateSelf( const idVec3 &translation ) {
250  origin += translation;
251  return *this;
252 }
253 
254 ID_INLINE idFrustum idFrustum::Rotate( const idMat3 &rotation ) const {
255  idFrustum f = *this;
256  f.axis *= rotation;
257  return f;
258 }
259 
260 ID_INLINE idFrustum &idFrustum::RotateSelf( const idMat3 &rotation ) {
261  axis *= rotation;
262  return *this;
263 }
264 
265 #endif /* !__BV_FRUSTUM_H__ */
void AxisProjection(const idVec3 &dir, float &min, float &max) const
Definition: Frustum.cpp:1684
float PlaneDistance(const idPlane &plane) const
Definition: Frustum.cpp:84
bool CullSphere(const idSphere &sphere) const
Definition: Frustum.cpp:263
float dNear
Definition: Frustum.h:120
bool ConstrainToBox(const idBox &box)
Definition: Frustum.cpp:1434
GLsizei const GLfloat * points
Definition: glext.h:3884
idFrustum & RotateSelf(const idMat3 &rotation)
Definition: Frustum.h:260
void SetOrigin(const idVec3 &origin)
Definition: Frustum.h:152
#define min(a, b)
const idVec3 & GetOrigin(void) const
Definition: Frustum.h:192
assert(prefInfo.fullscreenBtn)
void ToIndexPointsAndCornerVecs(idVec3 indexPoints[8], idVec3 cornerVecs[4]) const
Definition: Frustum.cpp:1623
bool CullFrustum(const idFrustum &frustum) const
Definition: Frustum.cpp:395
void ToPoints(idVec3 points[8]) const
Definition: Frustum.cpp:1523
bool LocalRayIntersection(const idVec3 &start, const idVec3 &dir, float &scale1, float &scale2) const
Definition: Frustum.cpp:674
bool LocalLineIntersection(const idVec3 &start, const idVec3 &end) const
Definition: Frustum.cpp:547
void ClipFrustumToBox(const idBox &box, float clipFractions[4], int clipPlanes[4]) const
Definition: Frustum.cpp:2405
float invFar
Definition: Frustum.h:124
void AddLocalLineToProjectionBoundsUseCull(const idVec3 &start, const idVec3 &end, int startCull, int endCull, idBounds &bounds) const
Definition: Frustum.cpp:1851
void MoveFarDistance(float dFar)
Definition: Frustum.h:183
GLenum GLenum GLenum GLenum GLenum scale
Definition: glext.h:4804
idFrustum Expand(const float d) const
Definition: Frustum.h:224
bool IntersectsBounds(const idBounds &bounds) const
Definition: Frustum.cpp:864
bool ConstrainToBounds(const idBounds &bounds)
Definition: Frustum.cpp:1414
void SetSize(float dNear, float dFar, float dLeft, float dUp)
Definition: Frustum.h:160
void ToPlanes(idPlane planes[6]) const
Definition: Frustum.cpp:1494
bool LineIntersection(const idVec3 &start, const idVec3 &end) const
Definition: Frustum.cpp:1174
void MoveNearDistance(float dNear)
Definition: Frustum.h:178
Definition: Vector.h:316
idFrustum & ExpandSelf(const float d)
Definition: Frustum.h:234
bool CullLocalBox(const idVec3 &localOrigin, const idVec3 &extents, const idMat3 &localAxis) const
Definition: Frustum.cpp:150
void AddLocalLineToProjectionBoundsSetCull(const idVec3 &start, const idVec3 &end, int &startCull, int &endCull, idBounds &bounds) const
Definition: Frustum.cpp:1712
bool AddLocalCapsToProjectionBounds(const idVec3 endPoints[4], const int endPointCull[4], const idVec3 &point, int pointCull, int pointClip, idBounds &projectionBounds) const
Definition: Frustum.cpp:2606
idVec3 GetCenter(void) const
Definition: Frustum.h:200
bool ProjectionBounds(const idBounds &bounds, idBounds &projectionBounds) const
Definition: Frustum.cpp:2043
float GetNearDistance(void) const
Definition: Frustum.h:208
const idMat3 & GetAxis(void) const
Definition: Frustum.h:196
bool CullLocalWinding(const idVec3 *points, const int numPoints, int *pointCull) const
Definition: Frustum.cpp:414
bool CullBox(const idBox &box) const
Definition: Frustum.cpp:242
bool ContainsPoint(const idVec3 &point) const
Definition: Frustum.cpp:795
bool ClipLine(const idVec3 localPoints[8], const idVec3 points[8], int startIndex, int endIndex, idVec3 &start, idVec3 &end, int &startClip, int &endClip) const
Definition: Frustum.cpp:2468
bool IntersectsBox(const idBox &box) const
Definition: Frustum.cpp:907
bool LocalFrustumIntersectsFrustum(const idVec3 points[8], const bool testFirstSide) const
Definition: Frustum.cpp:804
void SetPyramid(float dNear, float dFar)
Definition: Frustum.h:169
GLuint GLuint end
Definition: glext.h:2845
int PlaneSide(const idPlane &plane, const float epsilon=ON_EPSILON) const
Definition: Frustum.cpp:102
bool CullWinding(const class idWinding &winding) const
Definition: Frustum.cpp:449
bool IntersectsWinding(const idWinding &winding) const
Definition: Frustum.cpp:1102
idFrustum & TranslateSelf(const idVec3 &translation)
Definition: Frustum.h:249
float dLeft
Definition: Frustum.h:122
idFrustum Translate(const idVec3 &translation) const
Definition: Frustum.h:243
Definition: Plane.h:71
idFrustum Rotate(const idMat3 &rotation) const
Definition: Frustum.h:254
float GetLeft(void) const
Definition: Frustum.h:216
bool LocalFrustumIntersectsBounds(const idVec3 points[8], const idBounds &bounds) const
Definition: Frustum.cpp:834
void SetAxis(const idMat3 &axis)
Definition: Frustum.h:156
void ToClippedPoints(const float fractions[4], idVec3 points[8]) const
Definition: Frustum.cpp:1554
idFrustum(void)
Definition: Frustum.h:148
float dUp
Definition: Frustum.h:123
void ToIndexPoints(idVec3 indexPoints[8]) const
Definition: Frustum.cpp:1590
bool IsValid(void) const
Definition: Frustum.h:204
bool ClippedProjectionBounds(const idFrustum &frustum, const idBox &clipBox, idBounds &projectionBounds) const
Definition: Frustum.cpp:2623
Definition: Matrix.h:333
bool ConstrainToSphere(const idSphere &sphere)
Definition: Frustum.cpp:1454
tuple f
Definition: idal.py:89
#define ON_EPSILON
Definition: Plane.h:44
bool FromProjection(const idBounds &bounds, const idVec3 &projectionOrigin, const float dFar)
Definition: Frustum.cpp:1204
float dFar
Definition: Frustum.h:121
bool IntersectsFrustum(const idFrustum &frustum) const
Definition: Frustum.cpp:1055
bool CullBounds(const idBounds &bounds) const
Definition: Frustum.cpp:218
idVec3 origin
Definition: Frustum.h:118
bool CullLocalFrustum(const idFrustum &localFrustum, const idVec3 indexPoints[8], const idVec3 cornerVecs[4]) const
Definition: Frustum.cpp:308
bool BoundsRayIntersection(const idBounds &bounds, const idVec3 &start, const idVec3 &dir, float &scale1, float &scale2) const
Definition: Frustum.cpp:1978
bool RayIntersection(const idVec3 &start, const idVec3 &dir, float &scale1, float &scale2) const
Definition: Frustum.cpp:1187
bool ConstrainToFrustum(const idFrustum &frustum)
Definition: Frustum.cpp:1474
#define max(x, y)
Definition: os.h:70
float GetFarDistance(void) const
Definition: Frustum.h:212
Definition: Box.h:40
bool CullPoint(const idVec3 &point) const
Definition: Frustum.cpp:120
GLuint start
Definition: glext.h:2845
float GetUp(void) const
Definition: Frustum.h:220
bool IntersectsSphere(const idSphere &sphere) const
Definition: Frustum.cpp:955
idMat3 axis
Definition: Frustum.h:119
bool BoundsCullLocalFrustum(const idBounds &bounds, const idFrustum &localFrustum, const idVec3 indexPoints[8], const idVec3 cornerVecs[4]) const
Definition: Frustum.cpp:472