GL Studio C++ Runtime API
cull.h
Go to the documentation of this file.
1 /*! \file
2  \brief The disti::Culler class. Implements view frustum culling.
3 
4  \par Copyright Information
5 
6  Copyright (c) 2017 by The DiSTI Corporation.<br>
7  11301 Corporate Blvd; Suite 100<br>
8  Orlando, Florida 32817<br>
9  USA<br>
10  <br>
11  All rights reserved.<br>
12 
13  This Software contains proprietary trade secrets of DiSTI and may not be
14 reproduced, in whole or part, in any form, or by any means of electronic,
15 mechanical, or otherwise, without the written permission of DiSTI. Said
16 permission may be derived through the purchase of applicable DiSTI product
17 licenses which detail the distribution rights of this content and any
18 Derivative Works based on this or other copyrighted DiSTI Software.
19 
20  NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21 AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22 PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23 AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24 IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25 PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26 
27  LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28 IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29 INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30 DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31 INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32 INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBILITY
33 OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34 EXCEED FIVE DOLLARS (US$5.00).
35 
36  The aforementioned terms and restrictions are governed by the laws of the
37 State of Florida and the United States of America.
38 
39 */
40 #ifndef _CULL_H
41 #define _CULL_H
42 
43 #include "util.h"
44 #include "vertex.h"
45 
46 namespace disti
47 {
48 /** The Culler class. Implements view frustum culling. */
49 class Culler
50 {
51 public:
52  /** Return type for culling function */
53  typedef enum
54  {
55  OUTSIDE_FRUSTUM, /** The object was entirely outside of the viewing frustum */
56  INTERSECTS_FRUSTUM, /** The object intersects the viewing frustum */
57  INSIDE_FRUSTUM /** The object was entirely inside the viewing frustum */
59 
60  /** Definitions for the six frustum clipping planes */
61  typedef enum
62  {
63  RIGHT_PLANE,
64  LEFT_PLANE,
65  BOTTOM_PLANE,
66  TOP_PLANE,
67  FAR_PLANE,
68  NEAR_PLANE
70 
71 protected:
72  bool _enabled; /** Whether or not culling is enabled */
73 
74  PlaneClass _planes[ 6 ]; /** Array of six clipping planes to define the frustum */
75 
76 public:
77  /** Culler constructor
78  * \param enabled Whether or not to actually to the culling test
79  */
80  GLS_EXPORT Culler( bool enabled = true );
81 
82  /** Reads the current OpenGL projection matrices and determines the six frustum planes
83  * Note: This assumes a perspective projection, but it will work with orthographic,
84  * though it could be optimized better if it was known that Ortho is being used.
85  */
86  GLS_EXPORT void ExtractFrustum( OpenGLMatrices& matrices );
87 
88  /** \return Whether the sphere is inside, outside or intersects the view frustum.
89  */
90  GLS_EXPORT CullResultEnum SphereInFrustum( const Vector& v, const float radius ) const;
91 
92  /** Slightly faster sphere culling because it doesn't test seperately
93  * for intersection and it ignores the Enabled() state.
94  * \returns true if outside or intersecting, false if inside
95  */
96  inline bool SphereOutsideFrustum( const Vector& center, const float radius ) const
97  {
98  if( ( _planes[ 0 ].a * center.x + _planes[ 0 ].b * center.y + _planes[ 0 ].c * center.z + _planes[ 0 ].d ) <= -radius )
99  return true;
100  if( ( _planes[ 1 ].a * center.x + _planes[ 1 ].b * center.y + _planes[ 1 ].c * center.z + _planes[ 1 ].d ) <= -radius )
101  return true;
102  if( ( _planes[ 2 ].a * center.x + _planes[ 2 ].b * center.y + _planes[ 2 ].c * center.z + _planes[ 2 ].d ) <= -radius )
103  return true;
104  if( ( _planes[ 3 ].a * center.x + _planes[ 3 ].b * center.y + _planes[ 3 ].c * center.z + _planes[ 3 ].d ) <= -radius )
105  return true;
106  if( ( _planes[ 4 ].a * center.x + _planes[ 4 ].b * center.y + _planes[ 4 ].c * center.z + _planes[ 4 ].d ) <= -radius )
107  return true;
108  if( ( _planes[ 5 ].a * center.x + _planes[ 5 ].b * center.y + _planes[ 5 ].c * center.z + _planes[ 5 ].d ) <= -radius )
109  return true;
110 
111  return false; // inside (or intersecting)
112  }
113 
114  /** Accessor method to get whether or not culling is enabled
115  * \return Whether or not culling is enabled
116  */
117  inline bool Enabled( void ) const { return _enabled; }
118 
119  /** Accessor method to set whether or not culling is enabled
120  * Be carefull with this, because the Culler is usually passed
121  * by reference.
122  */
123  inline void Enabled( bool newVal ) { _enabled = newVal; }
124 
125  /** \return A reference to the desired clipping plane for this view frustum */
126  GLS_EXPORT const PlaneClass& Plane( const ClippingPlanesEnum& whichPlane ) const;
127 };
128 
129 } // namespace disti
130 
131 #endif
Definition: cull.h:49
Definition: vertex.h:551
Class to contain current OpenGL view, projection and draw matrices.
Definition: util.h:471
Culler(bool enabled=true)
Definition: cull.h:57
bool SphereOutsideFrustum(const Vector &center, const float radius) const
Definition: cull.h:96
void Enabled(bool newVal)
Definition: cull.h:123
void ExtractFrustum(OpenGLMatrices &matrices)
The disti::Vertex class. A class for manipulating 3D vertices.
Generally useful defines, macros, enumerations and function prototypes.
CullResultEnum
Definition: cull.h:53
PlaneClass _planes[6]
Definition: cull.h:74
CullResultEnum SphereInFrustum(const Vector &v, const float radius) const
const PlaneClass & Plane(const ClippingPlanesEnum &whichPlane) const
Definition: vertex.h:84
bool Enabled(void) const
Definition: cull.h:117
Definition: bmpimage.h:46
ClippingPlanesEnum
Definition: cull.h:61