doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Frustum_gcc.cpp
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 #include "../precompiled.h"
29 
30 void BoxToPoints( const idVec3 &center, const idVec3 &extents, const idMat3 &axis, idVec3 points[8] );
31 
32 /*
33 ============
34 idFrustum::ProjectionBounds
35 ============
36 */
37 bool idFrustum::ProjectionBounds( const idBox &box, idBounds &projectionBounds ) const {
38  int i, p1, p2, pointCull[8], culled, outside;
39  float scale1, scale2;
40  idFrustum localFrustum;
41  idVec3 points[8], localOrigin;
42  idMat3 localAxis, localScaled;
43  idBounds bounds( -box.GetExtents(), box.GetExtents() );
44 
45  // if the frustum origin is inside the bounds
46  if ( bounds.ContainsPoint( ( origin - box.GetCenter() ) * box.GetAxis().Transpose() ) ) {
47  // bounds that cover the whole frustum
48  float boxMin, boxMax, base;
49 
50  base = origin * axis[0];
51  box.AxisProjection( axis[0], boxMin, boxMax );
52 
53  projectionBounds[0].x = boxMin - base;
54  projectionBounds[1].x = boxMax - base;
55  projectionBounds[0].y = projectionBounds[0].z = -1.0f;
56  projectionBounds[1].y = projectionBounds[1].z = 1.0f;
57 
58  return true;
59  }
60 
61  projectionBounds.Clear();
62 
63  // transform the bounds into the space of this frustum
64  localOrigin = ( box.GetCenter() - origin ) * axis.Transpose();
65  localAxis = box.GetAxis() * axis.Transpose();
66  BoxToPoints( localOrigin, box.GetExtents(), localAxis, points );
67 
68  // test outer four edges of the bounds
69  culled = -1;
70  outside = 0;
71  for ( i = 0; i < 4; i++ ) {
72  p1 = i;
73  p2 = 4 + i;
74  AddLocalLineToProjectionBoundsSetCull( points[p1], points[p2], pointCull[p1], pointCull[p2], projectionBounds );
75  culled &= pointCull[p1] & pointCull[p2];
76  outside |= pointCull[p1] | pointCull[p2];
77  }
78 
79  // if the bounds are completely outside this frustum
80  if ( culled ) {
81  return false;
82  }
83 
84  // if the bounds are completely inside this frustum
85  if ( !outside ) {
86  return true;
87  }
88 
89  // test the remaining edges of the bounds
90  for ( i = 0; i < 4; i++ ) {
91  p1 = i;
92  p2 = (i+1)&3;
93  AddLocalLineToProjectionBoundsUseCull( points[p1], points[p2], pointCull[p1], pointCull[p2], projectionBounds );
94  }
95 
96  for ( i = 0; i < 4; i++ ) {
97  p1 = 4 + i;
98  p2 = 4 + ((i+1)&3);
99  AddLocalLineToProjectionBoundsUseCull( points[p1], points[p2], pointCull[p1], pointCull[p2], projectionBounds );
100  }
101 
102  // if the bounds extend beyond two or more boundaries of this frustum
103  if ( outside != 1 && outside != 2 && outside != 4 && outside != 8 ) {
104 
105  localOrigin = ( origin - box.GetCenter() ) * box.GetAxis().Transpose();
106  localScaled = axis * box.GetAxis().Transpose();
107  localScaled[0] *= dFar;
108  localScaled[1] *= dLeft;
109  localScaled[2] *= dUp;
110 
111  // test the outer edges of this frustum for intersection with the bounds
112  if ( (outside & 2) && (outside & 8) ) {
113  BoundsRayIntersection( bounds, localOrigin, localScaled[0] - localScaled[1] - localScaled[2], scale1, scale2 );
114  if ( scale1 <= scale2 && scale1 >= 0.0f ) {
115  projectionBounds.AddPoint( idVec3( scale1 * dFar, -1.0f, -1.0f ) );
116  projectionBounds.AddPoint( idVec3( scale2 * dFar, -1.0f, -1.0f ) );
117  }
118  }
119  if ( (outside & 2) && (outside & 4) ) {
120  BoundsRayIntersection( bounds, localOrigin, localScaled[0] - localScaled[1] + localScaled[2], scale1, scale2 );
121  if ( scale1 <= scale2 && scale1 >= 0.0f ) {
122  projectionBounds.AddPoint( idVec3( scale1 * dFar, -1.0f, 1.0f ) );
123  projectionBounds.AddPoint( idVec3( scale2 * dFar, -1.0f, 1.0f ) );
124  }
125  }
126  if ( (outside & 1) && (outside & 8) ) {
127  BoundsRayIntersection( bounds, localOrigin, localScaled[0] + localScaled[1] - localScaled[2], scale1, scale2 );
128  if ( scale1 <= scale2 && scale1 >= 0.0f ) {
129  projectionBounds.AddPoint( idVec3( scale1 * dFar, 1.0f, -1.0f ) );
130  projectionBounds.AddPoint( idVec3( scale2 * dFar, 1.0f, -1.0f ) );
131  }
132  }
133  if ( (outside & 1) && (outside & 2) ) {
134  BoundsRayIntersection( bounds, localOrigin, localScaled[0] + localScaled[1] + localScaled[2], scale1, scale2 );
135  if ( scale1 <= scale2 && scale1 >= 0.0f ) {
136  projectionBounds.AddPoint( idVec3( scale1 * dFar, 1.0f, 1.0f ) );
137  projectionBounds.AddPoint( idVec3( scale2 * dFar, 1.0f, 1.0f ) );
138  }
139  }
140  }
141 
142  return true;
143 }
GLsizei const GLfloat * points
Definition: glext.h:3884
const idVec3 & GetExtents(void) const
Definition: Box.h:217
void AddLocalLineToProjectionBoundsUseCull(const idVec3 &start, const idVec3 &end, int startCull, int endCull, idBounds &bounds) const
Definition: Frustum.cpp:1851
idMat3 Transpose(void) const
Definition: Matrix.h:677
Definition: Vector.h:316
void Clear(void)
Definition: Bounds.h:201
int i
Definition: process.py:33
void AddLocalLineToProjectionBoundsSetCull(const idVec3 &start, const idVec3 &end, int &startCull, int &endCull, idBounds &bounds) const
Definition: Frustum.cpp:1712
bool ProjectionBounds(const idBounds &bounds, idBounds &projectionBounds) const
Definition: Frustum.cpp:2043
void BoxToPoints(const idVec3 &center, const idVec3 &extents, const idMat3 &axis, idVec3 points[8])
Definition: Frustum.cpp:58
bool AddPoint(const idVec3 &v)
Definition: Bounds.h:226
float dLeft
Definition: Frustum.h:122
const idMat3 & GetAxis(void) const
Definition: Box.h:221
float dUp
Definition: Frustum.h:123
const idVec3 & GetCenter(void) const
Definition: Box.h:213
Definition: Matrix.h:333
tuple f
Definition: idal.py:89
float dFar
Definition: Frustum.h:121
idVec3 origin
Definition: Frustum.h:118
bool BoundsRayIntersection(const idBounds &bounds, const idVec3 &start, const idVec3 &dir, float &scale1, float &scale2) const
Definition: Frustum.cpp:1978
Definition: Box.h:40
void AxisProjection(const idVec3 &dir, float &min, float &max) const
Definition: Box.h:277
idMat3 axis
Definition: Frustum.h:119