GL Studio 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) 2015 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 "vertex.h"
44 #include "util.h"
45 
46 namespace disti
47 {
48 
49 /** The Culler class. Implements view frustum culling. */
50 class Culler
51 {
52 
53 public:
54 
55  /** Return type for culling function */
56  typedef enum
57  {
58  OUTSIDE_FRUSTUM, /** The object was entirely outside of the viewing frustum */
59  INTERSECTS_FRUSTUM, /** The object intersects the viewing frustum */
60  INSIDE_FRUSTUM /** The object was entirely inside the viewing frustum */
62 
63  /** Definitions for the six frustum clipping planes */
64  typedef enum
65  {
66  RIGHT_PLANE,
67  LEFT_PLANE,
68  BOTTOM_PLANE,
69  TOP_PLANE,
70  FAR_PLANE,
71  NEAR_PLANE
73 
74 
75 
76 protected:
77  bool _enabled; /** Whether or not culling is enabled */
78 
79  PlaneClass _planes[6]; /** Array of six clipping planes to define the frustum */
80 
81 public:
82  /** Culler constructor
83  * \param enabled Whether or not to actually to the culling test
84  */
85  GLS_EXPORT Culler(bool enabled=true);
86 
87  /** Reads the current OpenGL projection matrices and determines the six frustum planes
88  * Note: This assumes a perspective projection, but it will work with orthographic,
89  * though it could be optimized better if it was known that Ortho is being used.
90  */
91  GLS_EXPORT void ExtractFrustum(OpenGLMatrices &matrices);
92 
93  /** \return Whether the sphere is inside, outside or intersects the view frustum.
94  */
95  GLS_EXPORT CullResultEnum SphereInFrustum(const Vector &v,const float radius) const;
96 
97  /** Slightly faster sphere culling because it doesn't test seperately
98  * for intersection and it ignores the Enabled() state.
99  * \returns true if outside or intersecting, false if inside
100  */
101  inline bool SphereOutsideFrustum(const Vector &center,const float radius) const
102  {
103  if( (_planes[0].a * center.x + _planes[0].b * center.y + _planes[0].c * center.z + _planes[0].d) <= -radius )
104  return true;
105  if( (_planes[1].a * center.x + _planes[1].b * center.y + _planes[1].c * center.z + _planes[1].d) <= -radius )
106  return true;
107  if( (_planes[2].a * center.x + _planes[2].b * center.y + _planes[2].c * center.z + _planes[2].d) <= -radius )
108  return true;
109  if( (_planes[3].a * center.x + _planes[3].b * center.y + _planes[3].c * center.z + _planes[3].d) <= -radius )
110  return true;
111  if( (_planes[4].a * center.x + _planes[4].b * center.y + _planes[4].c * center.z + _planes[4].d) <= -radius )
112  return true;
113  if( (_planes[5].a * center.x + _planes[5].b * center.y + _planes[5].c * center.z + _planes[5].d) <= -radius )
114  return true;
115 
116  return false; // inside (or intersecting)
117  }
118 
119  /** Accessor method to get whether or not culling is enabled
120  * \return Whether or not culling is enabled
121  */
122  inline bool Enabled(void) const { return _enabled; }
123 
124  /** Accessor method to set whether or not culling is enabled
125  * Be carefull with this, because the Culler is usually passed
126  * by reference.
127  */
128  inline void Enabled(bool newVal) { _enabled = newVal; }
129 
130  /** \return A reference to the desired clipping plane for this view frustum */
131  GLS_EXPORT const PlaneClass &Plane(const ClippingPlanesEnum &whichPlane) const;
132 };
133 
134 } // namespace disti
135 
136 #endif
Definition: cull.h:50
Definition: vertex.h:494
Class to contain current OpenGL view, projection and draw matrices.
Definition: util.h:289
Culler(bool enabled=true)
Definition: cull.h:60
bool SphereOutsideFrustum(const Vector &center, const float radius) const
Definition: cull.h:101
void Enabled(bool newVal)
Definition: cull.h:128
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:56
PlaneClass _planes[6]
Definition: cull.h:79
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:122
Definition: bmpimage.h:46
ClippingPlanesEnum
Definition: cull.h:64