GL Studio C++ Runtime API
cull.h
Go to the documentation of this file.
1/// \file cull.h
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/// 12249 Science Drive; Suite 300<br>
8/// Orlando, Florida 32826<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#ifndef INCLUDED_DISTI_CULL_H
40#define INCLUDED_DISTI_CULL_H
41
42#include "util.h"
43#include "vertex.h"
44
45namespace disti
46{
47/** The Culler class. Implements view frustum culling. */
48class Culler
49{
50public:
51 /** Return type for culling function */
52 typedef enum
53 {
54 OUTSIDE_FRUSTUM, /** The object was entirely outside of the viewing frustum */
55 INTERSECTS_FRUSTUM, /** The object intersects the viewing frustum */
56 INSIDE_FRUSTUM /** The object was entirely inside the viewing frustum */
58
59 /** Definitions for the six frustum clipping planes */
60 typedef enum
61 {
62 RIGHT_PLANE,
63 LEFT_PLANE,
64 BOTTOM_PLANE,
65 TOP_PLANE,
66 FAR_PLANE,
67 NEAR_PLANE
69
70protected:
71 bool _enabled; ///< Whether or not culling is enabled.
72
73 PlaneClass _planes[ 6 ]; ///< Array of six clipping planes to define the frustum.
74
75public:
76 /** Culler constructor
77 * \param enabled Whether or not to actually to the culling test
78 */
79 GLS_EXPORT Culler( bool enabled = true );
80
81 /// Reads the current OpenGL projection matrices and determines the six frustum planes
82 /// Note: This assumes a perspective projection, but it will work with orthographic,
83 /// though it could be optimized better if it was known that Ortho is being used.
84 /// \param matrices A return reference for the frustum.
86
87 /// \param v The center of the sphere.
88 /// \param radius The radius of the sphere.
89 /// \return Whether the sphere is inside, outside or intersects the view frustum.
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 /// \param center The center of the sphere.
95 /// \param radius The radius of the sphere.
96 /// \return True if outside or intersecting, false if inside.
97 bool SphereOutsideFrustum( const Vector& center, const float radius ) const
98 {
99 if( ( _planes[ 0 ].a * center.x + _planes[ 0 ].b * center.y + _planes[ 0 ].c * center.z + _planes[ 0 ].d ) <= -radius )
100 return true;
101 if( ( _planes[ 1 ].a * center.x + _planes[ 1 ].b * center.y + _planes[ 1 ].c * center.z + _planes[ 1 ].d ) <= -radius )
102 return true;
103 if( ( _planes[ 2 ].a * center.x + _planes[ 2 ].b * center.y + _planes[ 2 ].c * center.z + _planes[ 2 ].d ) <= -radius )
104 return true;
105 if( ( _planes[ 3 ].a * center.x + _planes[ 3 ].b * center.y + _planes[ 3 ].c * center.z + _planes[ 3 ].d ) <= -radius )
106 return true;
107 if( ( _planes[ 4 ].a * center.x + _planes[ 4 ].b * center.y + _planes[ 4 ].c * center.z + _planes[ 4 ].d ) <= -radius )
108 return true;
109 if( ( _planes[ 5 ].a * center.x + _planes[ 5 ].b * center.y + _planes[ 5 ].c * center.z + _planes[ 5 ].d ) <= -radius )
110 return true;
111
112 return false; // inside (or intersecting)
113 }
114
115 /** Accessor method to get whether or not culling is enabled
116 * \return Whether or not culling is enabled
117 */
118 bool Enabled() const { return _enabled; }
119
120 /// Accessor method to set whether or not culling is enabled.
121 /// Be careful with this, because the Culler is usually passed by reference.
122 /// \param newVal The enabled value to set.
123 void Enabled( bool newVal ) { _enabled = newVal; }
124
125 /// Returns the clip plane values for the desired plane index.
126 /// \param whichPlane The index for the clipping plane to return.
127 /// \return A reference to the desired clipping plane for this view frustum.
128 GLS_EXPORT const PlaneClass& Plane( const ClippingPlanesEnum& whichPlane ) const;
129};
130
131} // namespace disti
132
133#endif
Definition: cull.h:49
bool _enabled
Whether or not culling is enabled.
Definition: cull.h:71
void Enabled(bool newVal)
Definition: cull.h:123
ClippingPlanesEnum
Definition: cull.h:61
bool SphereOutsideFrustum(const Vector &center, const float radius) const
Definition: cull.h:97
bool Enabled() const
Definition: cull.h:118
void ExtractFrustum(OpenGLMatrices &matrices)
Culler(bool enabled=true)
PlaneClass _planes[6]
Array of six clipping planes to define the frustum.
Definition: cull.h:73
const PlaneClass & Plane(const ClippingPlanesEnum &whichPlane) const
CullResultEnum
Definition: cull.h:53
@ INSIDE_FRUSTUM
Definition: cull.h:56
@ INTERSECTS_FRUSTUM
Definition: cull.h:55
CullResultEnum SphereInFrustum(const Vector &v, const float radius) const
Class to contain current OpenGL view, projection and draw matrices.
Definition: util.h:540
Definition: vertex.h:567
float d
A coefficient to describe a 3D plane: aX + bY + cZ + d = 0.
Definition: vertex.h:572
float b
A coefficient to describe a 3D plane: aX + bY + cZ + d = 0.
Definition: vertex.h:570
float c
A coefficient to describe a 3D plane: aX + bY + cZ + d = 0.
Definition: vertex.h:571
Definition: vertex.h:84
float y
The Y component.
Definition: vertex.h:87
float x
The X component.
Definition: vertex.h:86
float z
The Z component.
Definition: vertex.h:88
#define GLS_EXPORT
Macro denoting which functions should be visible from the runtime library.
Definition: gls_include.h:50
Definition: bmpimage.h:46
Generally useful defines, macros, enumerations and function prototypes.
The disti::Vertex class. A class for manipulating 3D vertices.