OpenSceneGraph 3.6.5
CullSettings
Go to the documentation of this file.
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
21namespace osg {
22
23// forward declare
24class ArgumentParser;
25class ApplicationUsage;
26
28{
29 public:
30
36
38 {
41 readCommandLine(arguments);
42 }
43
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
60 {
62 CULLING_MODE = (0x1 << 1),
63 LOD_SCALE = (0x1 << 2),
66 NEAR_FAR_RATIO = (0x1 << 5),
67 IMPOSTOR_ACTIVE = (0x1 << 6),
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
89
92
94 void setCullSettings(const CullSettings& settings);
95
97 virtual void inheritCullSettings(const CullSettings& settings) { inheritCullSettings(settings, _inheritanceMask); }
98
100 virtual void inheritCullSettings(const CullSettings& settings, unsigned int inheritanceMask);
101
104
107
108
114
117
120 inline void applyMaskAction(unsigned int maskBit)
121 {
123 {
124 _inheritanceMask = _inheritanceMask & (~maskBit);
125 }
126 }
127
128
135
137 bool getImpostorsActive() const { return _impostorActive; }
138
142
145
148
151
155
159
167
170
172 double getNearFarRatio() const { return _nearFarRatio; }
173
195
196 typedef int CullingMode;
197
200
203
204
207
210
213
215 void setLODScale(float scale) { _LODScale = scale; applyMaskAction(LOD_SCALE); }
216
218 float getLODScale() const { return _LODScale; }
219
223
226
227
228
233 {
234 virtual bool clampProjectionMatrixImplementation(osg::Matrixf& projection, double& znear, double& zfar) const = 0;
235 virtual bool clampProjectionMatrixImplementation(osg::Matrixd& projection, double& znear, double& zfar) const = 0;
236 };
237
244
245
247 void write(std::ostream& out);
248
249 protected:
250
253
258
265
269
270
271};
272
273template<class matrix_type, class value_type>
274bool clampProjectionMatrix(matrix_type& projection, double& znear, double& zfar, value_type nearFarRatio)
275{
276 double epsilon = 1e-6;
277 if (zfar<znear-epsilon)
278 {
279 if (zfar != -FLT_MAX || znear != FLT_MAX)
280 {
281 OSG_INFO<<"_clampProjectionMatrix not applied, invalid depth range, znear = "<<znear<<" zfar = "<<zfar<<std::endl;
282 }
283 return false;
284 }
285
286 if (zfar<znear+epsilon)
287 {
288 // znear and zfar are too close together and could cause divide by zero problems
289 // late on in the clamping code, so move the znear and zfar apart.
290 double average = (znear+zfar)*0.5;
291 znear = average-epsilon;
292 zfar = average+epsilon;
293 // OSG_INFO << "_clampProjectionMatrix widening znear and zfar to "<<znear<<" "<<zfar<<std::endl;
294 }
295
296 if (fabs(projection(0,3))<epsilon && fabs(projection(1,3))<epsilon && fabs(projection(2,3))<epsilon )
297 {
298 // OSG_INFO << "Orthographic matrix before clamping"<<projection<<std::endl;
299
300 value_type delta_span = (zfar-znear)*0.02;
301 if (delta_span<1.0) delta_span = 1.0;
302 value_type desired_znear = znear - delta_span;
303 value_type desired_zfar = zfar + delta_span;
304
305 // assign the clamped values back to the computed values.
306 znear = desired_znear;
307 zfar = desired_zfar;
308
309 projection(2,2)=-2.0f/(desired_zfar-desired_znear);
310 projection(3,2)=-(desired_zfar+desired_znear)/(desired_zfar-desired_znear);
311
312 // OSG_INFO << "Orthographic matrix after clamping "<<projection<<std::endl;
313 }
314 else
315 {
316
317 // OSG_INFO << "Persepective matrix before clamping"<<projection<<std::endl;
318
319 //std::cout << "_computed_znear"<<_computed_znear<<std::endl;
320 //std::cout << "_computed_zfar"<<_computed_zfar<<std::endl;
321
322 value_type zfarPushRatio = 1.02;
323 value_type znearPullRatio = 0.98;
324
325 //znearPullRatio = 0.99;
326
327 value_type desired_znear = znear * znearPullRatio;
328 value_type desired_zfar = zfar * zfarPushRatio;
329
330 // near plane clamping.
331 double min_near_plane = zfar*nearFarRatio;
332 if (desired_znear<min_near_plane) desired_znear=min_near_plane;
333
334 // assign the clamped values back to the computed values.
335 znear = desired_znear;
336 zfar = desired_zfar;
337
338 value_type trans_near_plane = (-desired_znear*projection(2,2)+projection(3,2))/(-desired_znear*projection(2,3)+projection(3,3));
339 value_type trans_far_plane = (-desired_zfar*projection(2,2)+projection(3,2))/(-desired_zfar*projection(2,3)+projection(3,3));
340
341 value_type ratio = fabs(2.0/(trans_near_plane-trans_far_plane));
342 value_type center = -(trans_near_plane+trans_far_plane)/2.0;
343
344 projection.postMult(osg::Matrix(1.0f,0.0f,0.0f,0.0f,
345 0.0f,1.0f,0.0f,0.0f,
346 0.0f,0.0f,ratio,0.0f,
347 0.0f,0.0f,center*ratio,1.0f));
348
349 // OSG_INFO << "Persepective matrix after clamping"<<projection<<std::endl;
350 }
351 return true;
352}
353
354
355
356}
357
358#endif
#define OSG_INFO
Definition Notify:87
The core osg library provides the basic scene graph classes such as Nodes, State and Drawables,...
Definition AlphaFunc:19
bool clampProjectionMatrix(matrix_type &projection, double &znear, double &zfar, value_type nearFarRatio)
Definition CullSettings:274
Matrixd Matrix
Definition Matrix:27
Definition ArgumentParser:28
InheritanceMask getInheritanceMask() const
Get the inheritance mask used in inheritCullSettings to control which variables get overwritten by th...
Definition CullSettings:91
float _LODScale
Definition CullSettings:256
void setSmallFeatureCullingPixelSize(float value)
Threshold at which small features are culled.
Definition CullSettings:222
void setLODScale(float scale)
Set the LOD bias for the CullVisitor to use.
Definition CullSettings:215
bool getImpostorsActive() const
Get whether impostors are active or not.
Definition CullSettings:137
float getSmallFeatureCullingPixelSize() const
Get the Small Feature Culling Pixel Size.
Definition CullSettings:225
Node::NodeMask _cullMaskLeft
Definition CullSettings:267
ComputeNearFarMode
Definition CullSettings:161
@ COMPUTE_NEAR_FAR_USING_PRIMITIVES
Definition CullSettings:164
@ DO_NOT_COMPUTE_NEAR_FAR
Definition CullSettings:162
@ COMPUTE_NEAR_USING_PRIMITIVES
Definition CullSettings:165
@ COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES
Definition CullSettings:163
virtual void inheritCullSettings(const CullSettings &settings)
Inherit the local cull settings variable from specified CullSettings object, according to the inherit...
Definition CullSettings:97
CullSettings(ArgumentParser &arguments)
Definition CullSettings:37
void setCullMaskRight(osg::Node::NodeMask nm)
Definition CullSettings:211
ClampProjectionMatrixCallback * getClampProjectionMatrixCallback()
get the non const ClampProjectionMatrixCallback.
Definition CullSettings:241
void setClampProjectionMatrixCallback(ClampProjectionMatrixCallback *cpmc)
set the ClampProjectionMatrixCallback.
Definition CullSettings:239
void readCommandLine(ArgumentParser &arguments)
read the commandline arguments.
Node::NodeMask _cullMask
Definition CullSettings:266
void applyMaskAction(unsigned int maskBit)
Apply the action, specified by the InheritanceMaskActionOnAttributeSetting, to apply to the inheritan...
Definition CullSettings:120
void setInheritanceMask(InheritanceMask mask)
Set the inheritance mask used in inheritCullSettings to control which variables get overwritten by th...
Definition CullSettings:88
void setInheritanceMaskActionOnAttributeSetting(InheritanceMaskActionOnAttributeSetting action)
Definition CullSettings:115
void readEnvironmentalVariables()
read the environmental variables.
double _nearFarRatio
Definition CullSettings:260
void setNearFarRatio(double ratio)
Definition CullSettings:171
VariablesMask
Definition CullSettings:60
@ LOD_SCALE
Definition CullSettings:63
@ CULL_MASK_RIGHT
Definition CullSettings:73
@ DRAW_BUFFER
Definition CullSettings:78
@ LIGHT
Definition CullSettings:77
@ ALL_VARIABLES
Definition CullSettings:82
@ NO_VARIABLES
Definition CullSettings:81
@ LIGHTING_MODE
Definition CullSettings:76
@ NEAR_FAR_RATIO
Definition CullSettings:66
@ IMPOSTOR_ACTIVE
Definition CullSettings:67
@ DEPTH_SORT_IMPOSTOR_SPRITES
Definition CullSettings:68
@ COMPUTE_NEAR_FAR_MODE
Definition CullSettings:61
@ IMPOSTOR_PIXEL_ERROR_THRESHOLD
Definition CullSettings:69
@ CULLING_MODE
Definition CullSettings:62
@ CLEAR_COLOR
Definition CullSettings:74
@ NUM_FRAMES_TO_KEEP_IMPOSTORS_SPRITES
Definition CullSettings:70
@ SMALL_FEATURE_CULLING_PIXEL_SIZE
Definition CullSettings:64
@ CULL_MASK
Definition CullSettings:71
@ CULL_MASK_LEFT
Definition CullSettings:72
@ CLEAR_MASK
Definition CullSettings:75
@ CLAMP_PROJECTION_MATRIX_CALLBACK
Definition CullSettings:65
@ READ_BUFFER
Definition CullSettings:79
double getNearFarRatio() const
Definition CullSettings:172
virtual ~CullSettings()
Definition CullSettings:46
bool getDepthSortImpostorSprites() const
Get whether ImpostorSprite's are depth sorted bin for rendering.
Definition CullSettings:150
void write(std::ostream &out)
Write out internal settings of CullSettings.
ComputeNearFarMode _computeNearFar
Definition CullSettings:254
int _numFramesToKeepImpostorSprites
Definition CullSettings:264
ComputeNearFarMode getComputeNearFarMode() const
Definition CullSettings:169
osg::Node::NodeMask getCullMaskLeft() const
Definition CullSettings:209
int getNumberOfFrameToKeepImpostorSprites() const
Get the number of frames that an ImpostorSprite is kept whilst not being beyond, before being recycle...
Definition CullSettings:158
float _smallFeatureCullingPixelSize
Definition CullSettings:257
Node::NodeMask _cullMaskRight
Definition CullSettings:268
virtual void inheritCullSettings(const CullSettings &settings, unsigned int inheritanceMask)
Inherit the local cull settings variable from specified CullSettings object, according to the inherit...
float getLODScale() const
Get the LOD bias.
Definition CullSettings:218
InheritanceMaskActionOnAttributeSetting
Definition CullSettings:110
@ DISABLE_ASSOCIATED_INHERITANCE_MASK_BIT
Definition CullSettings:111
@ DO_NOT_MODIFY_INHERITANCE_MASK
Definition CullSettings:112
CullSettings(const CullSettings &cs)
const ClampProjectionMatrixCallback * getClampProjectionMatrixCallback() const
get the const ClampProjectionMatrixCallback.
Definition CullSettings:243
void setImpostorsActive(bool active)
Switch the creation of Impostors on or off.
Definition CullSettings:134
void setCullSettings(const CullSettings &settings)
Set the local cull settings values from specified CullSettings object.
void setCullMask(osg::Node::NodeMask nm)
Definition CullSettings:205
bool _depthSortImpostorSprites
Definition CullSettings:262
void setNumberOfFrameToKeepImpostorSprites(int numFrames)
Set the number of frames that an ImpostorSprite is kept whilst not being beyond, before being recycle...
Definition CullSettings:154
InheritanceMaskActionOnAttributeSetting getInheritanceMaskActionOnAttributeSetting() const
Definition CullSettings:116
InheritanceMask _inheritanceMask
Definition CullSettings:251
float _impostorPixelErrorThreshold
Definition CullSettings:263
CullingMode _cullingMode
Definition CullSettings:255
CullingModeValues
Definition CullSettings:175
@ SMALL_FEATURE_CULLING
Definition CullSettings:183
@ CLUSTER_CULLING
Definition CullSettings:185
@ DEFAULT_CULLING
Definition CullSettings:186
@ FAR_PLANE_CULLING
Definition CullSettings:179
@ ENABLE_ALL_CULLING
Definition CullSettings:190
@ NEAR_PLANE_CULLING
Definition CullSettings:178
@ SHADOW_OCCLUSION_CULLING
Definition CullSettings:184
@ NO_CULLING
Definition CullSettings:176
@ VIEW_FRUSTUM_SIDES_CULLING
Definition CullSettings:177
@ VIEW_FRUSTUM_CULLING
Definition CullSettings:180
void setImpostorPixelErrorThreshold(float numPixels)
Set the impostor error threshold.
Definition CullSettings:141
osg::Node::NodeMask getCullMaskRight() const
Definition CullSettings:212
void setDepthSortImpostorSprites(bool doDepthSort)
Set whether ImpostorSprite's should be placed in a depth sorted bin for rendering.
Definition CullSettings:147
void setCullMaskLeft(osg::Node::NodeMask nm)
Definition CullSettings:208
float getImpostorPixelErrorThreshold() const
Get the impostor error threshold.
Definition CullSettings:144
osg::Node::NodeMask getCullMask() const
Definition CullSettings:206
bool _impostorActive
Definition CullSettings:261
void setCullingMode(CullingMode mode)
Set the culling mode for the CullVisitor to use.
Definition CullSettings:199
void setComputeNearFarMode(ComputeNearFarMode cnfm)
Definition CullSettings:168
int InheritanceMask
Definition CullSettings:85
virtual void setDefaults()
ref_ptr< ClampProjectionMatrixCallback > _clampProjectionMatrixCallback
Definition CullSettings:259
int CullingMode
Definition CullSettings:196
CullSettings()
Definition CullSettings:31
InheritanceMaskActionOnAttributeSetting _inheritanceMaskActionOnAttributeSetting
Definition CullSettings:252
CullingMode getCullingMode() const
Returns the current CullingMode.
Definition CullSettings:202
Callback for overriding the CullVisitor's default clamping of the projection matrix to computed near ...
Definition CullSettings:233
virtual bool clampProjectionMatrixImplementation(osg::Matrixf &projection, double &znear, double &zfar) const =0
virtual bool clampProjectionMatrixImplementation(osg::Matrixd &projection, double &znear, double &zfar) const =0
Definition Matrixd:27
Definition Matrixf:27
unsigned int NodeMask
This is a set of bits (flags) that represent the Node.
Definition Node:363
Smart pointer for handling referenced counted objects.
Definition ref_ptr:32
Base class for providing reference counted objects.
Definition Referenced:44
#define OSG_EXPORT
Definition Export:39

osg logo
Generated at Sun Jul 20 2025 00:00:00 for the OpenSceneGraph by doxygen 1.14.0.