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 #include <stdlib.h>
14 #include <string.h>
15 
16 #include <osg/CullSettings>
17 #include <osg/ArgumentParser>
18 #include <osg/ApplicationUsage>
19 #include <osg/os_utils>
20 
21 #include <osg/Notify>
22 
23 using namespace osg;
24 
CullSettings(const CullSettings & cs)25 CullSettings::CullSettings(const CullSettings& cs)
26 {
27     setCullSettings(cs);
28 }
29 
setDefaults()30 void CullSettings::setDefaults()
31 {
32     _inheritanceMask = ALL_VARIABLES;
33     _inheritanceMaskActionOnAttributeSetting = DISABLE_ASSOCIATED_INHERITANCE_MASK_BIT;
34     _cullingMode = DEFAULT_CULLING;
35     _LODScale = 1.0f;
36     _smallFeatureCullingPixelSize = 2.0f;
37 
38     _computeNearFar = COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES;
39     _nearFarRatio = 0.0005;
40     _impostorActive = true;
41     _depthSortImpostorSprites = false;
42     _impostorPixelErrorThreshold = 4.0f;
43     _numFramesToKeepImpostorSprites = 10;
44     _cullMask = 0xffffffff;
45     _cullMaskLeft = 0xffffffff;
46     _cullMaskRight = 0xffffffff;
47 
48     // override during testing
49     //_computeNearFar = COMPUTE_NEAR_FAR_USING_PRIMITIVES;
50     //_nearFarRatio = 0.00005f;
51 }
52 
setCullSettings(const CullSettings & rhs)53 void CullSettings::setCullSettings(const CullSettings& rhs)
54 {
55     _inheritanceMask = rhs._inheritanceMask;
56     _inheritanceMaskActionOnAttributeSetting = rhs._inheritanceMaskActionOnAttributeSetting;
57 
58     _computeNearFar = rhs._computeNearFar;
59     _cullingMode = rhs._cullingMode;
60     _LODScale = rhs._LODScale;
61     _smallFeatureCullingPixelSize = rhs._smallFeatureCullingPixelSize;
62 
63     _clampProjectionMatrixCallback = rhs._clampProjectionMatrixCallback;
64     _nearFarRatio = rhs._nearFarRatio;
65     _impostorActive = rhs._impostorActive;
66     _depthSortImpostorSprites = rhs._depthSortImpostorSprites;
67     _impostorPixelErrorThreshold = rhs._impostorPixelErrorThreshold;
68     _numFramesToKeepImpostorSprites = rhs._numFramesToKeepImpostorSprites;
69 
70     _cullMask = rhs._cullMask;
71     _cullMaskLeft = rhs._cullMaskLeft;
72     _cullMaskRight =  rhs._cullMaskRight;
73 }
74 
75 
inheritCullSettings(const CullSettings & settings,unsigned int inheritanceMask)76 void CullSettings::inheritCullSettings(const CullSettings& settings, unsigned int inheritanceMask)
77 {
78     if (inheritanceMask & COMPUTE_NEAR_FAR_MODE) _computeNearFar = settings._computeNearFar;
79     if (inheritanceMask & NEAR_FAR_RATIO) _nearFarRatio = settings._nearFarRatio;
80     if (inheritanceMask & IMPOSTOR_ACTIVE) _impostorActive = settings._impostorActive;
81     if (inheritanceMask & DEPTH_SORT_IMPOSTOR_SPRITES) _depthSortImpostorSprites = settings._depthSortImpostorSprites;
82     if (inheritanceMask & IMPOSTOR_PIXEL_ERROR_THRESHOLD) _impostorPixelErrorThreshold = settings._impostorPixelErrorThreshold;
83     if (inheritanceMask & NUM_FRAMES_TO_KEEP_IMPOSTORS_SPRITES) _numFramesToKeepImpostorSprites = settings._numFramesToKeepImpostorSprites;
84     if (inheritanceMask & CULL_MASK) _cullMask = settings._cullMask;
85     if (inheritanceMask & CULL_MASK_LEFT) _cullMaskLeft = settings._cullMaskLeft;
86     if (inheritanceMask & CULL_MASK_RIGHT) _cullMaskRight = settings._cullMaskRight;
87     if (inheritanceMask & CULLING_MODE) _cullingMode = settings._cullingMode;
88     if (inheritanceMask & LOD_SCALE) _LODScale = settings._LODScale;
89     if (inheritanceMask & SMALL_FEATURE_CULLING_PIXEL_SIZE) _smallFeatureCullingPixelSize = settings._smallFeatureCullingPixelSize;
90     if (inheritanceMask & CLAMP_PROJECTION_MATRIX_CALLBACK) _clampProjectionMatrixCallback = settings._clampProjectionMatrixCallback;
91 }
92 
93 
94 static ApplicationUsageProxy ApplicationUsageProxyCullSettings_e0(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_COMPUTE_NEAR_FAR_MODE <mode>","DO_NOT_COMPUTE_NEAR_FAR | COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES | COMPUTE_NEAR_FAR_USING_PRIMITIVES");
95 static ApplicationUsageProxy ApplicationUsageProxyCullSettings_e1(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_NEAR_FAR_RATIO <float>","Set the ratio between near and far planes - must greater than 0.0 but less than 1.0.");
96 
readEnvironmentalVariables()97 void CullSettings::readEnvironmentalVariables()
98 {
99     OSG_INFO<<"CullSettings::readEnvironmentalVariables()"<<std::endl;
100 
101     std::string value;
102     if (getEnvVar("OSG_COMPUTE_NEAR_FAR_MODE", value))
103     {
104         if (value=="DO_NOT_COMPUTE_NEAR_FAR") _computeNearFar = DO_NOT_COMPUTE_NEAR_FAR;
105         else if (value=="COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES") _computeNearFar = COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES;
106         else if (value=="COMPUTE_NEAR_FAR_USING_PRIMITIVES") _computeNearFar = COMPUTE_NEAR_FAR_USING_PRIMITIVES;
107 
108         OSG_INFO<<"Set compute near far mode to "<<_computeNearFar<<std::endl;
109 
110     }
111 
112     if (getEnvVar("OSG_NEAR_FAR_RATIO", _nearFarRatio))
113     {
114         OSG_INFO<<"Set near/far ratio to "<<_nearFarRatio<<std::endl;
115     }
116 }
117 
readCommandLine(ArgumentParser & arguments)118 void CullSettings::readCommandLine(ArgumentParser& arguments)
119 {
120     OSG_INFO<<"CullSettings::readCommandLine(ArgumentParser& arguments)"<<std::endl;
121 
122     // report the usage options.
123     if (arguments.getApplicationUsage())
124     {
125         arguments.getApplicationUsage()->addCommandLineOption("--COMPUTE_NEAR_FAR_MODE <mode>","DO_NOT_COMPUTE_NEAR_FAR | COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES | COMPUTE_NEAR_FAR_USING_PRIMITIVES");
126         arguments.getApplicationUsage()->addCommandLineOption("--NEAR_FAR_RATIO <float>","Set the ratio between near and far planes - must greater than 0.0 but less than 1.0.");
127     }
128 
129     while(arguments.read("--NO_CULLING")) setCullingMode(NO_CULLING);
130     while(arguments.read("--VIEW_FRUSTUM")) setCullingMode(VIEW_FRUSTUM_CULLING);
131     while(arguments.read("--VIEW_FRUSTUM_SIDES") || arguments.read("--vfs") ) setCullingMode(VIEW_FRUSTUM_SIDES_CULLING);
132 
133 
134     std::string str;
135     while(arguments.read("--COMPUTE_NEAR_FAR_MODE",str))
136     {
137         if (str=="DO_NOT_COMPUTE_NEAR_FAR") _computeNearFar = DO_NOT_COMPUTE_NEAR_FAR;
138         else if (str=="COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES") _computeNearFar = COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES;
139         else if (str=="COMPUTE_NEAR_FAR_USING_PRIMITIVES") _computeNearFar = COMPUTE_NEAR_FAR_USING_PRIMITIVES;
140 
141         OSG_INFO<<"Set compute near far mode to "<<_computeNearFar<<std::endl;
142     }
143 
144     double value;
145     while(arguments.read("--NEAR_FAR_RATIO",value))
146     {
147         _nearFarRatio = value;
148 
149         OSG_INFO<<"Set near/far ratio to "<<_nearFarRatio<<std::endl;
150     }
151 
152 }
153 
write(std::ostream & out)154 void CullSettings::write(std::ostream& out)
155 {
156     out<<"CullSettings: "<<this<<" {"<<std::endl;
157 
158     out<<"    _inheritanceMask = "<<_inheritanceMask<<std::endl;
159     out<<"    _inheritanceMaskActionOnAttributeSetting = "<<_inheritanceMaskActionOnAttributeSetting<<std::endl;
160     out<<"    _computeNearFar = "<<_computeNearFar<<std::endl;
161     out<<"    _cullingMode = "<<_cullingMode<<std::endl;
162     out<<"    _LODScale = "<<_LODScale<<std::endl;
163     out<<"    _smallFeatureCullingPixelSize = "<<_smallFeatureCullingPixelSize<<std::endl;
164     out<<"    _clampProjectionMatrixCallback = "<<_clampProjectionMatrixCallback.get()<<std::endl;
165     out<<"    _nearFarRatio = "<<_nearFarRatio<<std::endl;
166     out<<"    _impostorActive = "<<_impostorActive<<std::endl;
167     out<<"    _depthSortImpostorSprites = "<<_depthSortImpostorSprites<<std::endl;
168     out<<"    _impostorPixelErrorThreshold = "<<_impostorPixelErrorThreshold<<std::endl;
169     out<<"    _numFramesToKeepImpostorSprites = "<<_numFramesToKeepImpostorSprites<<std::endl;
170     out<<"    _cullMask = "<<_cullMask<<std::endl;
171     out<<"    _cullMaskLeft = "<<_cullMaskLeft<<std::endl;
172     out<<"    _cullMaskRight = "<<_cullMaskRight<<std::endl;
173 
174     out<<"{"<<std::endl;
175 }
176 
177