sceneZoneCullingState.h
Engine/source/scene/culling/sceneZoneCullingState.h
Classes:
class
Culling state for a zone.
class
Iterator over the culling volumes assigned to a zone.
class
A culling volume linked to a zone.
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifndef _SCENEZONECULLINGSTATE_H_ 25#define _SCENEZONECULLINGSTATE_H_ 26 27#ifndef _SCENECULLINGVOLUME_H_ 28#include "scene/culling/sceneCullingVolume.h" 29#endif 30 31 32/// Culling state for a zone. 33/// 34/// Zone states keep track of the culling volumes that are generated during traversal 35/// for a particular zone in a scene. 36/// 37/// @note This class has no meaningful constructor; the memory for all zone states is 38/// cleared en bloc. 39class SceneZoneCullingState 40{ 41 public: 42 43 friend class SceneCullingState; // mCullingVolumes 44 45 /// Result of a culling test in a zone. 46 enum CullingTestResult 47 { 48 /// An includer tested positive on the bounding volume. 49 CullingTestPositiveByInclusion, 50 51 /// An occluder tested positive on the bounding volume. 52 CullingTestPositiveByOcclusion, 53 54 /// None of the culling volumes included or excluded the bounding volume. 55 CullingTestNegative 56 }; 57 58 /// A culling volume linked to a zone. 59 /// 60 /// @note Memory for CullingVolumeLink instances is maintained by SceneCullingState. 61 struct CullingVolumeLink 62 { 63 /// Culling volume. 64 SceneCullingVolume mVolume; 65 66 /// Next culling volume linked to the zone. 67 CullingVolumeLink* mNext; 68 69 CullingVolumeLink( const SceneCullingVolume& volume ) 70 : mVolume( volume ) {mNext=<a href="/coding/file/types_8lint_8h/#types_8lint_8h_1a070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>;} 71 }; 72 73 /// Iterator over the culling volumes assigned to a zone. 74 struct CullingVolumeIterator 75 { 76 CullingVolumeIterator( const SceneZoneCullingState& state ) 77 : mCurrent( state.getCullingVolumes() ) {} 78 79 bool isValid() const { return mCurrent != NULL; } 80 const SceneCullingVolume& operator*() const 81 { 82 AssertFatal( isValid(), "SceneCullingState::ZoneState::CullingVolumeIterator::operator* - Invalid iterator" ); 83 return mCurrent->mVolume; 84 } 85 const SceneCullingVolume* operator->() const 86 { 87 AssertFatal( isValid(), "SceneCullingState::ZoneState::CullingVolumeIterator::operator-> - Invalid iterator" ); 88 return &mCurrent->mVolume; 89 } 90 CullingVolumeIterator& operator++() 91 { 92 AssertFatal( isValid(), "SceneCullingState::ZoneState::CullingVolumeIterator::operator++ - Invalid iterator" ); 93 mCurrent = mCurrent->mNext; 94 return *this; 95 } 96 97 private: 98 CullingVolumeLink* mCurrent; 99 }; 100 101 protected: 102 103 /// Whether tests can be short-circuited, i.e. the first culler that rejects or accepts 104 /// will cause the test to terminate. This is the case if there are no includers inside 105 /// occluders, i.e. if occluders can be trusted to fully exclude any space they cover. 106 bool mCanShortcuit;//RDTODO: implement this 107 108 /// Link of culling volumes defining the visibility state of the zone. Since there may be 109 /// multiple portals leading into a zone or multiple occluders inside a zone, we may have multiple 110 /// culling volumes. 111 mutable CullingVolumeLink* mCullingVolumes; 112 113 /// Whether culling volumes for this zone state have already been sorted. 114 mutable bool mHaveSortedVolumes; 115 116 /// Whether there are inclusion volumes on this state. 117 bool mHaveIncluders; 118 119 /// Whether there are occlusion volumes on this state. 120 bool mHaveOccluders; 121 122 /// Culling volume test abstracted over bounding volume type. 123 template< typename T > CullingTestResult _testVolumes( T bounds, bool occludersOnly ) const; 124 125 /// Sort the culling volumes such that the volumes with the highest probability 126 /// of rejecting objects come first in the list. Also, make sure that all 127 /// occluders come before all includers so that occlusion is handled correctly. 128 void _sortVolumes() const; 129 130 /// Insert the volume in @a link at the proper position in the list represented 131 /// by @a head and @a tail. 132 static void _insertSorted( CullingVolumeLink*& head, CullingVolumeLink*& tail, CullingVolumeLink* link ); 133 134 public: 135 136 /// Zone states are constructed by SceneCullingState. This constructor should not 137 /// be used otherwise. It is public due to the use through Vector in SceneCullingState. 138 SceneZoneCullingState():mCanShortcuit(false), mCullingVolumes(NULL), mHaveSortedVolumes(false), mHaveIncluders(false), mHaveOccluders(false){} 139 140 /// Return true if the zone is visible. This is the case if any 141 /// includers have been added to the zone's rendering state. 142 bool isZoneVisible() const { return mHaveIncluders; } 143 144 /// Return the list of culling volumes attached to the zone. 145 CullingVolumeLink* getCullingVolumes() const { _sortVolumes(); return mCullingVolumes; } 146 147 /// Test whether the culling volumes added to the zone test positive on the 148 /// given AABB, i.e. whether they include or exclude the given AABB. 149 CullingTestResult testVolumes( const Box3F& aabb, bool occludersOnly = false ) const; 150 151 /// Test whether the culling volumes added to the zone test positive on the 152 /// given OBB, i.e. whether they include or exclude the given OBB. 153 /// 154 /// @param obb An OBB described by 8 points. 155 /// @param invertedOnly If true, only inverted cullers are tested. 156 CullingTestResult testVolumes( const OrientedBox3F& obb, bool occludersOnly = false ) const; 157 158 /// Test whether the culling volumes added to the zone test positive on the 159 /// given sphere, i.e. whether they include or exclude the given sphere. 160 CullingTestResult testVolumes( const SphereF& sphere, bool occludersOnly = false ) const; 161 162 /// Return true if the zone has more than one culling volume assigned to it. 163 bool hasMultipleVolumes() const { return ( mCullingVolumes && mCullingVolumes->mNext ); } 164 165 /// Return true if the zone has inclusion volumes assigned to it. 166 bool hasIncluders() const { return mHaveIncluders; } 167 168 /// Return true if the zone has occlusion volumes assigned to it. 169 bool hasOccluders() const { return mHaveOccluders; } 170}; 171 172#endif // !_SCENEZONECULLINGSTATE_H_ 173