mElite  1.0
An Elite clone based on TextElite by Jan-Philipp Kappmeier and Melanie Schmidt.
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Frustum.cpp
Go to the documentation of this file.
1 
6 #include "Frustum.h"
7 
9  this->frustum = frustum;
10 }
11 
13 }
14 
16 
22  this->frustum = frustum;
23 }
24 
26 
31 void Frustum::update( CameraObject *camera ) {
32 
33  // calculate positions of far plane edges
34  Vector3 farCenter = camera->getPos() + camera->getView() * frustum.farDist;
35  farTopLeft= farCenter + (camera->getUp() * frustum.farHeight*0.5) - (camera->getRight() * frustum.farWidth*0.5);
36  farTopRight= farCenter + (camera->getUp() * frustum.farHeight*0.5) + (camera->getRight() * frustum.farWidth*0.5);
37  farBottomLeft= farCenter - (camera->getUp() * frustum.farHeight*0.5) - (camera->getRight() * frustum.farWidth*0.5);
38  farBottomRight = farCenter - (camera->getUp() * frustum.farHeight*0.5) + (camera->getRight() * frustum.farWidth*0.5);
39 
40  // calculate positions of near plane edges
41  Vector3 nearCenter = camera->getPos() + camera->getView() * frustum.nearDist;
42  nearTopLeft= nearCenter + (camera->getUp() * frustum.nearHeight*0.5) - (camera->getRight() * frustum.nearWidth*0.5);
43  nearTopRight= nearCenter + (camera->getUp() * frustum.nearHeight*0.5) + (camera->getRight() * frustum.nearWidth*0.5);
44  nearBottomLeft= nearCenter - (camera->getUp() * frustum.nearHeight*0.5) - (camera->getRight() * frustum.nearWidth*0.5);
45  nearBottomRight = nearCenter - (camera->getUp() * frustum.nearHeight*0.5) + (camera->getRight() * frustum.nearWidth*0.5);
46 
47  // now compute the six planes bounding the frustum. the points are given in counter clockwise
48  // order so that all normals point inside the frustum. that will us easyly allow to check if a point is
49  // inside the frustum
50  planes[topPlane].setPlane( nearTopRight, nearTopLeft, farTopLeft );
51  planes[bottomPlane].setPlane( nearBottomLeft, nearBottomRight, farBottomRight );
52  planes[leftPlane].setPlane( nearTopLeft, nearBottomRight, farBottomLeft );
53  planes[rightPlane].setPlane( nearBottomRight, nearTopRight, farBottomRight );
54  planes[nearPlane].setPlane( nearTopLeft, nearTopRight, nearBottomRight );
55  planes[farPlane].setPlane( farTopRight, farTopLeft, farBottomLeft );
56 }
57 
59 
68  for(int i=0; i < 6; i++) {
69  if (planes[i].distance(p) < 0)
70  return outside;
71  }
72  return inside;
73 }
74 
76 
83  float distance;
84  CullingLocation location = inside;
85 
86  for( int i=0; i < 6; i++ ) {
87  distance = planes[i].distance( p );
88  if( distance < -radius )
89  return outside;
90  else if( distance < radius )
91  location = intersect; // could be outside of another plane!
92  }
93  return location;
94 }
95 
97 
103 bool Frustum::isInUpperHalf( const Vector3 &p ) {
104  float dist1 = planes[topPlane].distance( p );
105  float dist2 = planes[bottomPlane].distance( p );
106  if( dist1 < 0 )
107  return false;
108  if( dist1 < dist2 )
109  return true;
110  else
111  return false;
112 }
113 
115 
121 bool Frustum::isInRightHalf( const Vector3 &p ) {
122  float dist1 = planes[rightPlane].distance( p );
123  float dist2 = planes[leftPlane].distance( p );
124  if( dist1 < 0 )
125  return false;
126  if( dist1 < dist2 )
127  return true;
128  else
129  return false;
130 }
131 
133 
140 float Frustum::getFrustumWidth( float dist ) {
141  if( dist < frustum.nearDist || dist > frustum.farDist )
142  return 0;
143 
144  float val = getFrustumHeight( dist ) * frustum.aspect;
145  return val;
146 }
147 
148 
150 
157 float Frustum::getFrustumHeight( float dist ) {
158  if( dist < frustum.nearDist || dist > frustum.farDist )
159  return 0;
160 
161  float val = 2 * tan( frustum.fov * ANGLE2DEG * 0.5 ) * dist;
162  return val;
163 }