root/OpenSceneGraph/trunk/src/osg/CullSettings.cpp @ 10415

Revision 10415, 7.5 kB (checked in by robert, 5 years ago)

From Mathias Froehlich, "We are currently getting issues with locale settings and some osg plugins.
Therefore I have changed all the occurances of atof by asciiToFloat or
asciiToDouble.

I believe that it is safe to do so at least for all the plugins.
Included here are also asciiToFloat conversion of environment variables. One
might argue that these should be locale dependent. But IMO these should be
set and interpreted by osg independent of the current locale.
"

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