| 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 */
58 } CullResultEnum;
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
69 } ClippingPlanesEnum;
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 /// \param matrices A return reference for the frustum.
86 GLS_EXPORT void ExtractFrustum( OpenGLMatrices& matrices );
87
88 /// \param v The center of the sphere.
89 /// \param radius The radius of the sphere.
90 /// \return Whether the sphere is inside, outside or intersects the view frustum.
91 GLS_EXPORT CullResultEnum SphereInFrustum( const Vector& v, const float radius ) const;
92
93 /// Slightly faster sphere culling because it doesn't test seperately
94 /// for intersection and it ignores the Enabled() state.
95 /// \param center The center of the sphere.
96 /// \param radius The radius of the sphere.
97 /// \return True if outside or intersecting, false if inside.
98 inline bool SphereOutsideFrustum( const Vector& center, const float radius ) const
99 {
100 if( ( _planes[ 0 ].a * center.x + _planes[ 0 ].b * center.y + _planes[ 0 ].c * center.z + _planes[ 0 ].d ) <= -radius )
101 return true;
102 if( ( _planes[ 1 ].a * center.x + _planes[ 1 ].b * center.y + _planes[ 1 ].c * center.z + _planes[ 1 ].d ) <= -radius )
103 return true;
104 if( ( _planes[ 2 ].a * center.x + _planes[ 2 ].b * center.y + _planes[ 2 ].c * center.z + _planes[ 2 ].d ) <= -radius )
105 return true;
106 if( ( _planes[ 3 ].a * center.x + _planes[ 3 ].b * center.y + _planes[ 3 ].c * center.z + _planes[ 3 ].d ) <= -radius )
107 return true;
108 if( ( _planes[ 4 ].a * center.x + _planes[ 4 ].b * center.y + _planes[ 4 ].c * center.z + _planes[ 4 ].d ) <= -radius )
109 return true;
110 if( ( _planes[ 5 ].a * center.x + _planes[ 5 ].b * center.y + _planes[ 5 ].c * center.z + _planes[ 5 ].d ) <= -radius )
111 return true;
112
113 return false; // inside (or intersecting)
114 }
115
116 /** Accessor method to get whether or not culling is enabled
117 * \return Whether or not culling is enabled
118 */
119 inline bool Enabled() const { return _enabled; }
120
121 /// Accessor method to set whether or not culling is enabled.
122 /// Be careful with this, because the Culler is usually passed by reference.
123 /// \param newVal The enabled value to set.
124 inline void Enabled( bool newVal ) { _enabled = newVal; }
125
126 /// Returns the clip plane values for the desired plane index.
127 /// \param whichPlane The index for the clipping plane to return.
128 /// \return A reference to the desired clipping plane for this view frustum.
129 GLS_EXPORT const PlaneClass& Plane( const ClippingPlanesEnum& whichPlane ) const;
130};
131
132} // namespace disti
133
134#endif
disti::Culler
Definition: cull.h:50
disti::Culler::_enabled
bool _enabled
Whether or not culling is enabled.
Definition: cull.h:72
disti::Culler::Enabled
void Enabled(bool newVal)
Definition: cull.h:124
disti::Culler::ClippingPlanesEnum
ClippingPlanesEnum
Definition: cull.h:62
disti::Culler::SphereOutsideFrustum
bool SphereOutsideFrustum(const Vector &center, const float radius) const
Definition: cull.h:98
disti::Culler::Enabled
bool Enabled() const
Definition: cull.h:119
disti::Culler::ExtractFrustum
void ExtractFrustum(OpenGLMatrices &matrices)
disti::Culler::Culler
Culler(bool enabled=true)
disti::Culler::_planes
PlaneClass _planes[6]
Array of six clipping planes to define the frustum.
Definition: cull.h:74
disti::Culler::Plane
const PlaneClass & Plane(const ClippingPlanesEnum &whichPlane) const
disti::Culler::CullResultEnum
CullResultEnum
Definition: cull.h:54
disti::Culler::INSIDE_FRUSTUM
@ INSIDE_FRUSTUM
Definition: cull.h:57
disti::Culler::INTERSECTS_FRUSTUM
@ INTERSECTS_FRUSTUM
Definition: cull.h:56
disti::Culler::SphereInFrustum
CullResultEnum SphereInFrustum(const Vector &v, const float radius) const
disti::OpenGLMatrices
Class to contain current OpenGL view, projection and draw matrices.
Definition: util.h:544
disti::PlaneClass
Definition: vertex.h:568
disti::PlaneClass::d
float d
A coefficient to describe a 3D plane: aX + bY + cZ + d = 0.
Definition: vertex.h:573
disti::PlaneClass::b
float b
A coefficient to describe a 3D plane: aX + bY + cZ + d = 0.
Definition: vertex.h:571
disti::PlaneClass::c
float c
A coefficient to describe a 3D plane: aX + bY + cZ + d = 0.
Definition: vertex.h:572
disti::VertexNoColor
Definition: vertex.h:85
disti::VertexNoColor::y
float y
The Y component.
Definition: vertex.h:88
disti::VertexNoColor::x
float x
The X component.
Definition: vertex.h:87
disti::VertexNoColor::z
float z
The Z component.
Definition: vertex.h:89
GLS_EXPORT
#define GLS_EXPORT
Macro denoting which functions should be visible from the runtime library.
Definition: gls_include.h:52
disti
Definition: bmpimage.h:47
util.h
Generally useful defines, macros, enumerations and function prototypes.
vertex.h
The disti::Vertex class. A class for manipulating 3D vertices.