| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSG_CULLSETTINGS |
|---|
| 15 | #define OSG_CULLSETTINGS 1 |
|---|
| 16 | |
|---|
| 17 | #include <iosfwd> |
|---|
| 18 | #include <osg/Matrix> |
|---|
| 19 | #include <osg/ClearNode> |
|---|
| 20 | |
|---|
| 21 | namespace osg { |
|---|
| 22 | |
|---|
| 23 | // forward declare |
|---|
| 24 | class ArgumentParser; |
|---|
| 25 | class ApplicationUsage; |
|---|
| 26 | |
|---|
| 27 | class OSG_EXPORT CullSettings |
|---|
| 28 | { |
|---|
| 29 | public: |
|---|
| 30 | |
|---|
| 31 | CullSettings() |
|---|
| 32 | { |
|---|
| 33 | setDefaults(); |
|---|
| 34 | readEnvironmentalVariables(); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | CullSettings(ArgumentParser& arguments) |
|---|
| 38 | { |
|---|
| 39 | setDefaults(); |
|---|
| 40 | readEnvironmentalVariables(); |
|---|
| 41 | readCommandLine(arguments); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | CullSettings(const CullSettings& cs); |
|---|
| 45 | |
|---|
| 46 | virtual ~CullSettings() {} |
|---|
| 47 | |
|---|
| 48 | CullSettings& operator = (const CullSettings& settings) |
|---|
| 49 | { |
|---|
| 50 | if (this==&settings) return *this; |
|---|
| 51 | setCullSettings(settings); |
|---|
| 52 | return *this; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | virtual void setDefaults(); |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | enum VariablesMask |
|---|
| 60 | { |
|---|
| 61 | COMPUTE_NEAR_FAR_MODE = (0x1 << 0), |
|---|
| 62 | CULLING_MODE = (0x1 << 1), |
|---|
| 63 | LOD_SCALE = (0x1 << 2), |
|---|
| 64 | SMALL_FEATURE_CULLING_PIXEL_SIZE = (0x1 << 3), |
|---|
| 65 | CLAMP_PROJECTION_MATRIX_CALLBACK = (0x1 << 4), |
|---|
| 66 | NEAR_FAR_RATIO = (0x1 << 5), |
|---|
| 67 | IMPOSTOR_ACTIVE = (0x1 << 6), |
|---|
| 68 | DEPTH_SORT_IMPOSTOR_SPRITES = (0x1 << 7), |
|---|
| 69 | IMPOSTOR_PIXEL_ERROR_THRESHOLD = (0x1 << 8), |
|---|
| 70 | NUM_FRAMES_TO_KEEP_IMPOSTORS_SPRITES = (0x1 << 9), |
|---|
| 71 | CULL_MASK = (0x1 << 10), |
|---|
| 72 | CULL_MASK_LEFT = (0x1 << 11), |
|---|
| 73 | CULL_MASK_RIGHT = (0x1 << 12), |
|---|
| 74 | CLEAR_COLOR = (0x1 << 13), |
|---|
| 75 | CLEAR_MASK = (0x1 << 14), |
|---|
| 76 | LIGHTING_MODE = (0x1 << 15), |
|---|
| 77 | LIGHT = (0x1 << 16), |
|---|
| 78 | DRAW_BUFFER = (0x1 << 17), |
|---|
| 79 | READ_BUFFER = (0x1 << 18), |
|---|
| 80 | |
|---|
| 81 | NO_VARIABLES = 0x00000000, |
|---|
| 82 | ALL_VARIABLES = 0x7FFFFFFF |
|---|
| 83 | }; |
|---|
| 84 | |
|---|
| 85 | typedef int InheritanceMask; |
|---|
| 86 | |
|---|
| 87 | /** Set the inheritance mask used in inheritCullSettings to control which variables get overwritten by the passed in CullSettings object.*/ |
|---|
| 88 | void setInheritanceMask(InheritanceMask mask) { _inheritanceMask = mask; } |
|---|
| 89 | |
|---|
| 90 | /** Get the inheritance mask used in inheritCullSettings to control which variables get overwritten by the passed in CullSettings object.*/ |
|---|
| 91 | InheritanceMask getInheritanceMask() const { return _inheritanceMask; } |
|---|
| 92 | |
|---|
| 93 | /** Set the local cull settings values from specified CullSettings object.*/ |
|---|
| 94 | void setCullSettings(const CullSettings& settings); |
|---|
| 95 | |
|---|
| 96 | /** Inherit the local cull settings variable from specified CullSettings object, according to the inheritance mask.*/ |
|---|
| 97 | virtual void inheritCullSettings(const CullSettings& settings) { inheritCullSettings(settings, _inheritanceMask); } |
|---|
| 98 | |
|---|
| 99 | /** Inherit the local cull settings variable from specified CullSettings object, according to the inheritance mask.*/ |
|---|
| 100 | virtual void inheritCullSettings(const CullSettings& settings, unsigned int inheritanceMask); |
|---|
| 101 | |
|---|
| 102 | /** read the environmental variables.*/ |
|---|
| 103 | void readEnvironmentalVariables(); |
|---|
| 104 | |
|---|
| 105 | /** read the commandline arguments.*/ |
|---|
| 106 | void readCommandLine(ArgumentParser& arguments); |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | enum InheritanceMaskActionOnAttributeSetting |
|---|
| 110 | { |
|---|
| 111 | DISABLE_ASSOCIATED_INHERITANCE_MASK_BIT, |
|---|
| 112 | DO_NOT_MODIFY_INHERITANCE_MASK |
|---|
| 113 | }; |
|---|
| 114 | |
|---|
| 115 | void setInheritanceMaskActionOnAttributeSetting(InheritanceMaskActionOnAttributeSetting action) { _inheritanceMaskActionOnAttributeSetting = action; } |
|---|
| 116 | InheritanceMaskActionOnAttributeSetting getInheritanceMaskActionOnAttributeSetting() const { return _inheritanceMaskActionOnAttributeSetting; } |
|---|
| 117 | |
|---|
| 118 | /** Apply the action, specified by the InheritanceMaskActionOnAttributeSetting, to apply to the inheritance bit mask. |
|---|
| 119 | * This method is called by CullSettings::set*() parameter methods to ensure that CullSettings inheritance mechanisms doesn't overwrite the local parameter settings.*/ |
|---|
| 120 | inline void applyMaskAction(unsigned int maskBit) |
|---|
| 121 | { |
|---|
| 122 | if (_inheritanceMaskActionOnAttributeSetting==DISABLE_ASSOCIATED_INHERITANCE_MASK_BIT) |
|---|
| 123 | { |
|---|
| 124 | _inheritanceMask = _inheritanceMask & (~maskBit); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | /** Switch the creation of Impostors on or off. |
|---|
| 130 | * Setting active to false forces the CullVisitor to use the Impostor |
|---|
| 131 | * LOD children for rendering. Setting active to true forces the |
|---|
| 132 | * CullVisitor to create the appropriate pre-rendering stages which |
|---|
| 133 | * render to the ImpostorSprite's texture.*/ |
|---|
| 134 | void setImpostorsActive(bool active) { _impostorActive = active; applyMaskAction(IMPOSTOR_ACTIVE); } |
|---|
| 135 | |
|---|
| 136 | /** Get whether impostors are active or not. */ |
|---|
| 137 | bool getImpostorsActive() const { return _impostorActive; } |
|---|
| 138 | |
|---|
| 139 | /** Set the impostor error threshold. |
|---|
| 140 | * Used in calculation of whether impostors remain valid.*/ |
|---|
| 141 | void setImpostorPixelErrorThreshold(float numPixels) { _impostorPixelErrorThreshold=numPixels; applyMaskAction(IMPOSTOR_PIXEL_ERROR_THRESHOLD); } |
|---|
| 142 | |
|---|
| 143 | /** Get the impostor error threshold.*/ |
|---|
| 144 | float getImpostorPixelErrorThreshold() const { return _impostorPixelErrorThreshold; } |
|---|
| 145 | |
|---|
| 146 | /** Set whether ImpostorSprite's should be placed in a depth sorted bin for rendering.*/ |
|---|
| 147 | void setDepthSortImpostorSprites(bool doDepthSort) { _depthSortImpostorSprites = doDepthSort; applyMaskAction(DEPTH_SORT_IMPOSTOR_SPRITES); } |
|---|
| 148 | |
|---|
| 149 | /** Get whether ImpostorSprite's are depth sorted bin for rendering.*/ |
|---|
| 150 | bool getDepthSortImpostorSprites() const { return _depthSortImpostorSprites; } |
|---|
| 151 | |
|---|
| 152 | /** Set the number of frames that an ImpostorSprite is kept whilst not being beyond, |
|---|
| 153 | * before being recycled.*/ |
|---|
| 154 | void setNumberOfFrameToKeepImpostorSprites(int numFrames) { _numFramesToKeepImpostorSprites = numFrames; applyMaskAction(NUM_FRAMES_TO_KEEP_IMPOSTORS_SPRITES); } |
|---|
| 155 | |
|---|
| 156 | /** Get the number of frames that an ImpostorSprite is kept whilst not being beyond, |
|---|
| 157 | * before being recycled.*/ |
|---|
| 158 | int getNumberOfFrameToKeepImpostorSprites() const { return _numFramesToKeepImpostorSprites; } |
|---|
| 159 | |
|---|
| 160 | enum ComputeNearFarMode |
|---|
| 161 | { |
|---|
| 162 | DO_NOT_COMPUTE_NEAR_FAR = 0, |
|---|
| 163 | COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES, |
|---|
| 164 | COMPUTE_NEAR_FAR_USING_PRIMITIVES |
|---|
| 165 | }; |
|---|
| 166 | |
|---|
| 167 | void setComputeNearFarMode(ComputeNearFarMode cnfm) { _computeNearFar=cnfm; applyMaskAction(COMPUTE_NEAR_FAR_MODE); } |
|---|
| 168 | ComputeNearFarMode getComputeNearFarMode() const { return _computeNearFar;} |
|---|
| 169 | |
|---|
| 170 | void setNearFarRatio(double ratio) { _nearFarRatio = ratio; applyMaskAction(NEAR_FAR_RATIO); } |
|---|
| 171 | double getNearFarRatio() const { return _nearFarRatio; } |
|---|
| 172 | |
|---|
| 173 | enum CullingModeValues |
|---|
| 174 | { |
|---|
| 175 | NO_CULLING = 0x0, |
|---|
| 176 | VIEW_FRUSTUM_SIDES_CULLING = 0x1, |
|---|
| 177 | NEAR_PLANE_CULLING = 0x2, |
|---|
| 178 | FAR_PLANE_CULLING = 0x4, |
|---|
| 179 | VIEW_FRUSTUM_CULLING = VIEW_FRUSTUM_SIDES_CULLING| |
|---|
| 180 | NEAR_PLANE_CULLING| |
|---|
| 181 | FAR_PLANE_CULLING, |
|---|
| 182 | SMALL_FEATURE_CULLING = 0x8, |
|---|
| 183 | SHADOW_OCCLUSION_CULLING = 0x10, |
|---|
| 184 | CLUSTER_CULLING = 0x20, |
|---|
| 185 | DEFAULT_CULLING = VIEW_FRUSTUM_SIDES_CULLING| |
|---|
| 186 | SMALL_FEATURE_CULLING| |
|---|
| 187 | SHADOW_OCCLUSION_CULLING| |
|---|
| 188 | CLUSTER_CULLING, |
|---|
| 189 | ENABLE_ALL_CULLING = VIEW_FRUSTUM_CULLING| |
|---|
| 190 | SMALL_FEATURE_CULLING| |
|---|
| 191 | SHADOW_OCCLUSION_CULLING| |
|---|
| 192 | CLUSTER_CULLING |
|---|
| 193 | }; |
|---|
| 194 | |
|---|
| 195 | typedef int CullingMode; |
|---|
| 196 | |
|---|
| 197 | /** Set the culling mode for the CullVisitor to use.*/ |
|---|
| 198 | void setCullingMode(CullingMode mode) { _cullingMode = mode; applyMaskAction(CULLING_MODE); } |
|---|
| 199 | |
|---|
| 200 | /** Returns the current CullingMode.*/ |
|---|
| 201 | CullingMode getCullingMode() const { return _cullingMode; } |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | void setCullMask(osg::Node::NodeMask nm) { _cullMask = nm; applyMaskAction(CULL_MASK); } |
|---|
| 205 | osg::Node::NodeMask getCullMask() const { return _cullMask; } |
|---|
| 206 | |
|---|
| 207 | void setCullMaskLeft(osg::Node::NodeMask nm) { _cullMaskLeft = nm; applyMaskAction(CULL_MASK_LEFT); } |
|---|
| 208 | osg::Node::NodeMask getCullMaskLeft() const { return _cullMaskLeft; } |
|---|
| 209 | |
|---|
| 210 | void setCullMaskRight(osg::Node::NodeMask nm) { _cullMaskRight = nm; applyMaskAction(CULL_MASK_RIGHT); } |
|---|
| 211 | osg::Node::NodeMask getCullMaskRight() const { return _cullMaskRight; } |
|---|
| 212 | |
|---|
| 213 | /** Set the LOD bias for the CullVisitor to use.*/ |
|---|
| 214 | void setLODScale(float scale) { _LODScale = scale; applyMaskAction(LOD_SCALE); } |
|---|
| 215 | |
|---|
| 216 | /** Get the LOD bias.*/ |
|---|
| 217 | float getLODScale() const { return _LODScale; } |
|---|
| 218 | |
|---|
| 219 | /** Set the Small Feature Culling Pixel Size.*/ |
|---|
| 220 | void setSmallFeatureCullingPixelSize(float value) { _smallFeatureCullingPixelSize=value; applyMaskAction(SMALL_FEATURE_CULLING_PIXEL_SIZE); } |
|---|
| 221 | |
|---|
| 222 | /** Get the Small Feature Culling Pixel Size.*/ |
|---|
| 223 | float getSmallFeatureCullingPixelSize() const { return _smallFeatureCullingPixelSize; } |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | /** Callback for overriding the CullVisitor's default clamping of the projection matrix to computed near and far values. |
|---|
| 228 | * Note, both Matrixf and Matrixd versions of clampProjectionMatrixImplementation must be implemented as the CullVisitor |
|---|
| 229 | * can target either Matrix data type, configured at compile time.*/ |
|---|
| 230 | struct ClampProjectionMatrixCallback : public osg::Referenced |
|---|
| 231 | { |
|---|
| 232 | virtual bool clampProjectionMatrixImplementation(osg::Matrixf& projection, double& znear, double& zfar) const = 0; |
|---|
| 233 | virtual bool clampProjectionMatrixImplementation(osg::Matrixd& projection, double& znear, double& zfar) const = 0; |
|---|
| 234 | }; |
|---|
| 235 | |
|---|
| 236 | /** set the ClampProjectionMatrixCallback.*/ |
|---|
| 237 | void setClampProjectionMatrixCallback(ClampProjectionMatrixCallback* cpmc) { _clampProjectionMatrixCallback = cpmc; applyMaskAction(CLAMP_PROJECTION_MATRIX_CALLBACK); } |
|---|
| 238 | /** get the non const ClampProjectionMatrixCallback.*/ |
|---|
| 239 | ClampProjectionMatrixCallback* getClampProjectionMatrixCallback() { return _clampProjectionMatrixCallback.get(); } |
|---|
| 240 | /** get the const ClampProjectionMatrixCallback.*/ |
|---|
| 241 | const ClampProjectionMatrixCallback* getClampProjectionMatrixCallback() const { return _clampProjectionMatrixCallback.get(); } |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | /** Write out internal settings of CullSettings. */ |
|---|
| 245 | void write(std::ostream& out); |
|---|
| 246 | |
|---|
| 247 | protected: |
|---|
| 248 | |
|---|
| 249 | InheritanceMask _inheritanceMask; |
|---|
| 250 | InheritanceMaskActionOnAttributeSetting _inheritanceMaskActionOnAttributeSetting; |
|---|
| 251 | |
|---|
| 252 | ComputeNearFarMode _computeNearFar; |
|---|
| 253 | CullingMode _cullingMode; |
|---|
| 254 | float _LODScale; |
|---|
| 255 | float _smallFeatureCullingPixelSize; |
|---|
| 256 | |
|---|
| 257 | ref_ptr<ClampProjectionMatrixCallback> _clampProjectionMatrixCallback; |
|---|
| 258 | double _nearFarRatio; |
|---|
| 259 | bool _impostorActive; |
|---|
| 260 | bool _depthSortImpostorSprites; |
|---|
| 261 | float _impostorPixelErrorThreshold; |
|---|
| 262 | int _numFramesToKeepImpostorSprites; |
|---|
| 263 | |
|---|
| 264 | Node::NodeMask _cullMask; |
|---|
| 265 | Node::NodeMask _cullMaskLeft; |
|---|
| 266 | Node::NodeMask _cullMaskRight; |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | }; |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | #endif |
|---|