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

Revision 10415, 17.8 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 <osg/DisplaySettings>
14#include <osg/ArgumentParser>
15#include <osg/ApplicationUsage>
16#include <osg/Math>
17#include <osg/Notify>
18#include <osg/ref_ptr>
19
20#include <algorithm>
21#include <string.h>
22
23using namespace osg;
24using namespace std;
25
26DisplaySettings* DisplaySettings::instance()
27{
28    static ref_ptr<DisplaySettings> s_displaySettings = new DisplaySettings;
29    return s_displaySettings.get();
30}
31
32DisplaySettings::DisplaySettings(const DisplaySettings& vs):Referenced(true)
33{
34    setDisplaySettings(vs);
35}
36
37DisplaySettings::~DisplaySettings()
38{
39}
40
41 
42 DisplaySettings& DisplaySettings::operator = (const DisplaySettings& vs)
43{
44    if (this==&vs) return *this;
45    setDisplaySettings(vs);
46    return *this;
47}
48
49void DisplaySettings::setDisplaySettings(const DisplaySettings& vs)
50{
51    _displayType = vs._displayType;
52    _stereo = vs._stereo;
53    _stereoMode = vs._stereoMode;
54    _eyeSeparation = vs._eyeSeparation;
55    _screenWidth = vs._screenWidth;
56    _screenHeight = vs._screenHeight;
57    _screenDistance = vs._screenDistance;
58
59    _splitStereoHorizontalEyeMapping = vs._splitStereoHorizontalEyeMapping;
60    _splitStereoHorizontalSeparation = vs._splitStereoHorizontalSeparation;
61
62    _splitStereoVerticalEyeMapping = vs._splitStereoVerticalEyeMapping;
63    _splitStereoVerticalSeparation = vs._splitStereoVerticalSeparation;
64   
65    _splitStereoAutoAdjustAspectRatio = vs._splitStereoAutoAdjustAspectRatio;
66
67    _doubleBuffer = vs._doubleBuffer;
68    _RGB = vs._RGB;
69    _depthBuffer = vs._depthBuffer;
70    _minimumNumberAlphaBits = vs._minimumNumberAlphaBits;
71    _minimumNumberStencilBits = vs._minimumNumberStencilBits;
72
73    _maxNumOfGraphicsContexts = vs._maxNumOfGraphicsContexts;
74    _numMultiSamples = vs._numMultiSamples;
75   
76    _compileContextsHint = vs._compileContextsHint;
77    _serializeDrawDispatch = vs._serializeDrawDispatch;
78   
79    _numDatabaseThreadsHint = vs._numDatabaseThreadsHint;
80    _numHttpDatabaseThreadsHint = vs._numHttpDatabaseThreadsHint;
81   
82    _application = vs._application;
83}
84
85void DisplaySettings::merge(const DisplaySettings& vs)
86{
87    if (_stereo       || vs._stereo)        _stereo = true;
88   
89    // need to think what to do about merging the stereo mode.
90   
91    if (_doubleBuffer || vs._doubleBuffer)  _doubleBuffer = true;
92    if (_RGB          || vs._RGB)           _RGB = true;
93    if (_depthBuffer  || vs._depthBuffer)   _depthBuffer = true;
94   
95    if (vs._minimumNumberAlphaBits>_minimumNumberAlphaBits) _minimumNumberAlphaBits = vs._minimumNumberAlphaBits;
96    if (vs._minimumNumberStencilBits>_minimumNumberStencilBits) _minimumNumberStencilBits = vs._minimumNumberStencilBits;
97    if (vs._numMultiSamples>_numMultiSamples) _numMultiSamples = vs._numMultiSamples;
98
99    if (vs._compileContextsHint) _compileContextsHint = vs._compileContextsHint;
100    if (vs._serializeDrawDispatch) _serializeDrawDispatch = vs._serializeDrawDispatch;
101
102    if (vs._numDatabaseThreadsHint>_numDatabaseThreadsHint) _numDatabaseThreadsHint = vs._numDatabaseThreadsHint;
103    if (vs._numHttpDatabaseThreadsHint>_numHttpDatabaseThreadsHint) _numHttpDatabaseThreadsHint = vs._numHttpDatabaseThreadsHint;
104
105    if (_application.empty()) _application = vs._application;
106}
107
108void DisplaySettings::setDefaults()
109{
110    _displayType = MONITOR;
111
112    _stereo = false;
113    _stereoMode = ANAGLYPHIC;
114    _eyeSeparation = 0.05f;
115    _screenWidth = 0.325f;
116    _screenHeight = 0.26f;
117    _screenDistance = 0.5f;
118
119    _splitStereoHorizontalEyeMapping = LEFT_EYE_LEFT_VIEWPORT;
120    _splitStereoHorizontalSeparation = 0;
121
122    _splitStereoVerticalEyeMapping = LEFT_EYE_TOP_VIEWPORT;
123    _splitStereoVerticalSeparation = 0;
124
125    _splitStereoAutoAdjustAspectRatio = true;
126
127    _doubleBuffer = true;
128    _RGB = true;
129    _depthBuffer = true;
130    _minimumNumberAlphaBits = 0;
131    _minimumNumberStencilBits = 0;
132    _minimumNumberAccumRedBits = 0;
133    _minimumNumberAccumGreenBits = 0;
134    _minimumNumberAccumBlueBits = 0;
135    _minimumNumberAccumAlphaBits = 0;
136   
137    _maxNumOfGraphicsContexts = 32;
138    _numMultiSamples = 0;
139
140    #ifdef __sgi
141    // switch on anti-aliasing by default, just in case we have an Onyx :-)
142    _numMultiSamples = 4;
143    #endif
144   
145    _compileContextsHint = false;
146    _serializeDrawDispatch = true;
147
148    _numDatabaseThreadsHint = 2;
149    _numHttpDatabaseThreadsHint = 1;
150}
151
152void DisplaySettings::setMaxNumberOfGraphicsContexts(unsigned int num)
153{
154    _maxNumOfGraphicsContexts = num;
155}
156
157unsigned int DisplaySettings::getMaxNumberOfGraphicsContexts() const
158{
159    // osg::notify(osg::NOTICE)<<"getMaxNumberOfGraphicsContexts()="<<_maxNumOfGraphicsContexts<<std::endl;
160    return _maxNumOfGraphicsContexts;
161}
162
163void DisplaySettings::setMinimumNumAccumBits(unsigned int red, unsigned int green, unsigned int blue, unsigned int alpha)
164{
165    _minimumNumberAccumRedBits = red;
166    _minimumNumberAccumGreenBits = green;
167    _minimumNumberAccumBlueBits = blue;
168    _minimumNumberAccumAlphaBits = alpha;
169}
170
171static ApplicationUsageProxy DisplaySetting_e0(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_DISPLAY_TYPE <type>","MONITOR | POWERWALL | REALITY_CENTER | HEAD_MOUNTED_DISPLAY");
172static ApplicationUsageProxy DisplaySetting_e1(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_STEREO_MODE <mode>","QUAD_BUFFER | ANAGLYPHIC | HORIZONTAL_SPLIT | VERTICAL_SPLIT | LEFT_EYE | RIGHT_EYE | VERTICAL_INTERLACE | HORIZONTAL_INTERLACE");
173static ApplicationUsageProxy DisplaySetting_e2(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_STEREO <mode>","OFF | ON");
174static ApplicationUsageProxy DisplaySetting_e3(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_EYE_SEPARATION <float>","physical distance between eyes");
175static ApplicationUsageProxy DisplaySetting_e4(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_SCREEN_DISTANCE <float>","physical distance between eyes and screen");
176static ApplicationUsageProxy DisplaySetting_e5(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_SCREEN_HEIGHT <float>","physical screen height");
177static ApplicationUsageProxy DisplaySetting_e6(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_SCREEN_WIDTH <float>","physical screen width");
178static ApplicationUsageProxy DisplaySetting_e7(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_SPLIT_STEREO_HORIZONTAL_EYE_MAPPING <mode>","LEFT_EYE_LEFT_VIEWPORT | LEFT_EYE_RIGHT_VIEWPORT");
179static ApplicationUsageProxy DisplaySetting_e8(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_SPLIT_STEREO_HORIZONTAL_SEPARATION <float>","number of pixels between viewports");
180static ApplicationUsageProxy DisplaySetting_e9(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_SPLIT_STEREO_VERTICAL_EYE_MAPPING <mode>","LEFT_EYE_TOP_VIEWPORT | LEFT_EYE_BOTTOM_VIEWPORT");
181static ApplicationUsageProxy DisplaySetting_e10(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_SPLIT_STEREO_AUTO_ADJUST_ASPECT_RATIO <mode>","OFF | ON  Default to ON to compenstate for the compression of the aspect ratio when viewing in split screen stereo.  Note, if you are setting fovx and fovy explicityly OFF should be used.");
182static ApplicationUsageProxy DisplaySetting_e11(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_SPLIT_STEREO_VERTICAL_SEPARATION <float>","number of pixels between viewports");
183static ApplicationUsageProxy DisplaySetting_e12(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_MAX_NUMBER_OF_GRAPHICS_CONTEXTS <int>","maximum number of graphics contexts to be used with applications.");
184static ApplicationUsageProxy DisplaySetting_e13(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_COMPILE_CONTEXTS <mode>","OFF | ON Enable/disable the use a backgrouind compile contexts and threads.");
185static ApplicationUsageProxy DisplaySetting_e14(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_SERIALIZE_DRAW_DISPATCH <mode>","OFF | ON Enable/disable the use a muetx to serialize the draw dispatch when there are multiple graphics threads.");
186static ApplicationUsageProxy DisplaySetting_e15(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_NUM_DATABASE_THREADS <int>","Set the hint for the total number of threads to set up in the DatabasePager.");
187static ApplicationUsageProxy DisplaySetting_e16(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_NUM_HTTP_DATABASE_THREADS <int>","Set the hint for the total number of threads dedicated to http requests to set up in the DatabasePager.");
188static ApplicationUsageProxy DisplaySetting_e17(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_MULTI_SAMPLES <int>","Set the hint for the number of samples to use when multi-sampling.");
189
190void DisplaySettings::readEnvironmentalVariables()
191{
192    const char* ptr = 0;
193   
194    if ((ptr = getenv("OSG_DISPLAY_TYPE")) != 0)
195    {
196        if (strcmp(ptr,"MONITOR")==0)
197        {
198            _displayType = MONITOR;
199        }
200        else
201        if (strcmp(ptr,"POWERWALL")==0)
202        {
203            _displayType = POWERWALL;
204        }
205        else
206        if (strcmp(ptr,"REALITY_CENTER")==0)
207        {
208            _displayType = REALITY_CENTER;
209        }
210        else
211        if (strcmp(ptr,"HEAD_MOUNTED_DISPLAY")==0)
212        {
213            _displayType = HEAD_MOUNTED_DISPLAY;
214        }
215    }
216   
217    if( (ptr = getenv("OSG_STEREO_MODE")) != 0)
218    {
219        if (strcmp(ptr,"QUAD_BUFFER")==0)
220        {
221            _stereoMode = QUAD_BUFFER;
222        }
223        else if (strcmp(ptr,"ANAGLYPHIC")==0)
224        {
225            _stereoMode = ANAGLYPHIC;
226        }
227        else if (strcmp(ptr,"HORIZONTAL_SPLIT")==0)
228        {
229            _stereoMode = HORIZONTAL_SPLIT;
230        }
231        else if (strcmp(ptr,"VERTICAL_SPLIT")==0)
232        {
233            _stereoMode = VERTICAL_SPLIT;
234        }
235        else if (strcmp(ptr,"LEFT_EYE")==0)
236        {
237            _stereoMode = LEFT_EYE;
238        }
239        else if (strcmp(ptr,"RIGHT_EYE")==0)
240        {
241            _stereoMode = RIGHT_EYE;
242        }
243        else if (strcmp(ptr,"HORIZONTAL_INTERLACE")==0)
244        {
245            _stereoMode = HORIZONTAL_INTERLACE;
246        }
247        else if (strcmp(ptr,"VERTICAL_INTERLACE")==0)
248        {
249            _stereoMode = VERTICAL_INTERLACE;
250        }
251        else if (strcmp(ptr,"CHECKERBOARD")==0)
252        {
253            _stereoMode = CHECKERBOARD;
254        }
255    }
256
257    if( (ptr = getenv("OSG_STEREO")) != 0)
258    {
259        if (strcmp(ptr,"OFF")==0)
260        {
261            _stereo = false;
262        }
263        else
264        if (strcmp(ptr,"ON")==0)
265        {
266            _stereo = true;
267        }
268    }
269
270    if( (ptr = getenv("OSG_EYE_SEPARATION")) != 0)
271    {
272        _eyeSeparation = osg::asciiToFloat(ptr);
273    }
274
275    if( (ptr = getenv("OSG_SCREEN_WIDTH")) != 0)
276    {
277        _screenWidth = osg::asciiToFloat(ptr);
278    }
279
280    if( (ptr = getenv("OSG_SCREEN_HEIGHT")) != 0)
281    {
282        _screenHeight = osg::asciiToFloat(ptr);
283    }
284
285    if( (ptr = getenv("OSG_SCREEN_DISTANCE")) != 0)
286    {
287        _screenDistance = osg::asciiToFloat(ptr);
288    }
289
290    if( (ptr = getenv("OSG_SPLIT_STEREO_HORIZONTAL_EYE_MAPPING")) != 0)
291    {
292        if (strcmp(ptr,"LEFT_EYE_LEFT_VIEWPORT")==0)
293        {
294            _splitStereoHorizontalEyeMapping = LEFT_EYE_LEFT_VIEWPORT;
295        }
296        else
297        if (strcmp(ptr,"LEFT_EYE_RIGHT_VIEWPORT")==0)
298        {
299            _splitStereoHorizontalEyeMapping = LEFT_EYE_RIGHT_VIEWPORT;
300        }
301    }
302
303    if( (ptr = getenv("OSG_SPLIT_STEREO_HORIZONTAL_SEPARATION")) != 0)
304    {
305        _splitStereoHorizontalSeparation = atoi(ptr);
306    }
307
308
309    if( (ptr = getenv("OSG_SPLIT_STEREO_VERTICAL_EYE_MAPPING")) != 0)
310    {
311        if (strcmp(ptr,"LEFT_EYE_TOP_VIEWPORT")==0)
312        {
313            _splitStereoVerticalEyeMapping = LEFT_EYE_TOP_VIEWPORT;
314        }
315        else
316        if (strcmp(ptr,"LEFT_EYE_BOTTOM_VIEWPORT")==0)
317        {
318            _splitStereoVerticalEyeMapping = LEFT_EYE_BOTTOM_VIEWPORT;
319        }
320    }
321   
322    if( (ptr = getenv("OSG_SPLIT_STEREO_AUTO_ADJUST_ASPECT_RATIO")) != 0)
323    {
324        if (strcmp(ptr,"OFF")==0)
325        {
326            _splitStereoAutoAdjustAspectRatio = false;
327        }
328        else
329        if (strcmp(ptr,"ON")==0)
330        {
331            _splitStereoAutoAdjustAspectRatio = true;
332        }
333    }
334
335    if( (ptr = getenv("OSG_SPLIT_STEREO_VERTICAL_SEPARATION")) != 0)
336    {
337        _splitStereoVerticalSeparation = atoi(ptr);
338    }
339
340    if( (ptr = getenv("OSG_MAX_NUMBER_OF_GRAPHICS_CONTEXTS")) != 0)
341    {
342        _maxNumOfGraphicsContexts = atoi(ptr);
343    }
344
345    if( (ptr = getenv("OSG_COMPIlE_CONTEXTS")) != 0)
346    {
347        if (strcmp(ptr,"OFF")==0)
348        {
349            _compileContextsHint = false;
350        }
351        else
352        if (strcmp(ptr,"ON")==0)
353        {
354            _compileContextsHint = true;
355        }
356    }
357   
358    if( (ptr = getenv("OSG_SERIALIZE_DRAW_DISPATCH")) != 0)
359    {
360        if (strcmp(ptr,"OFF")==0)
361        {
362            _serializeDrawDispatch = false;
363        }
364        else
365        if (strcmp(ptr,"ON")==0)
366        {
367            _serializeDrawDispatch = true;
368        }
369    }
370
371    if( (ptr = getenv("OSG_NUM_DATABASE_THREADS")) != 0)
372    {
373        _numDatabaseThreadsHint = atoi(ptr);
374    }
375
376    if( (ptr = getenv("OSG_NUM_HTTP_DATABASE_THREADS")) != 0)
377    {
378        _numHttpDatabaseThreadsHint = atoi(ptr);
379    }
380
381    if( (ptr = getenv("OSG_MULTI_SAMPLES")) != 0)
382    {
383        _numMultiSamples = atoi(ptr);
384    }
385}
386
387void DisplaySettings::readCommandLine(ArgumentParser& arguments)
388{
389    if (_application.empty()) _application = arguments[0];
390
391    // report the usage options.
392    if (arguments.getApplicationUsage())
393    {
394        arguments.getApplicationUsage()->addCommandLineOption("--display <type>","MONITOR | POWERWALL | REALITY_CENTER | HEAD_MOUNTED_DISPLAY");
395        arguments.getApplicationUsage()->addCommandLineOption("--stereo","Use default stereo mode which is ANAGLYPHIC if not overriden by environmental variable");
396        arguments.getApplicationUsage()->addCommandLineOption("--stereo <mode>","ANAGLYPHIC | QUAD_BUFFER | HORIZONTAL_SPLIT | VERTICAL_SPLIT | LEFT_EYE | RIGHT_EYE | HORIZONTAL_INTERLACE | VERTICAL_INTERLACE | CHECKERBOARD | ON | OFF ");
397        arguments.getApplicationUsage()->addCommandLineOption("--rgba","Request a RGBA color buffer visual");
398        arguments.getApplicationUsage()->addCommandLineOption("--stencil","Request a stencil buffer visual");
399        arguments.getApplicationUsage()->addCommandLineOption("--accum-rgb","Request a rgb accumulator buffer visual");
400        arguments.getApplicationUsage()->addCommandLineOption("--accum-rgba","Request a rgb accumulator buffer visual");
401        arguments.getApplicationUsage()->addCommandLineOption("--samples <num>","Request a multisample visual");
402        arguments.getApplicationUsage()->addCommandLineOption("--cc","Request use of compile contexts and threads");
403        arguments.getApplicationUsage()->addCommandLineOption("--serialize-draw <mode>","OFF | ON - set the serialization of draw dispatch");
404    }
405
406    std::string str;
407    while(arguments.read("--display",str))
408    {
409        if (str=="MONITOR") _displayType = MONITOR;
410        else if (str=="POWERWALL") _displayType = POWERWALL;
411        else if (str=="REALITY_CENTER") _displayType = REALITY_CENTER;
412        else if (str=="HEAD_MOUNTED_DISPLAY") _displayType = HEAD_MOUNTED_DISPLAY;
413    }
414
415    int pos;
416    while ((pos=arguments.find("--stereo"))>0)
417    {
418        if (arguments.match(pos+1,"ANAGLYPHIC"))            { arguments.remove(pos,2); _stereo = true;_stereoMode = ANAGLYPHIC; }
419        else if (arguments.match(pos+1,"QUAD_BUFFER"))      { arguments.remove(pos,2); _stereo = true;_stereoMode = QUAD_BUFFER; }
420        else if (arguments.match(pos+1,"HORIZONTAL_SPLIT")) { arguments.remove(pos,2); _stereo = true;_stereoMode = HORIZONTAL_SPLIT; }
421        else if (arguments.match(pos+1,"VERTICAL_SPLIT"))   { arguments.remove(pos,2); _stereo = true;_stereoMode = VERTICAL_SPLIT; }
422        else if (arguments.match(pos+1,"HORIZONTAL_INTERLACE")) { arguments.remove(pos,2); _stereo = true;_stereoMode = HORIZONTAL_INTERLACE; }
423        else if (arguments.match(pos+1,"VERTICAL_INTERLACE"))   { arguments.remove(pos,2); _stereo = true;_stereoMode = VERTICAL_INTERLACE; }
424        else if (arguments.match(pos+1,"CHECKERBOARD"))     { arguments.remove(pos,2); _stereo = true;_stereoMode = CHECKERBOARD; }
425        else if (arguments.match(pos+1,"LEFT_EYE"))         { arguments.remove(pos,2); _stereo = true;_stereoMode = LEFT_EYE; }
426        else if (arguments.match(pos+1,"RIGHT_EYE"))        { arguments.remove(pos,2); _stereo = true;_stereoMode = RIGHT_EYE; }
427        else if (arguments.match(pos+1,"ON"))               { arguments.remove(pos,2); _stereo = true; }
428        else if (arguments.match(pos+1,"OFF"))              { arguments.remove(pos,2); _stereo = false; }
429        else                                                { arguments.remove(pos); _stereo = true; }
430    }
431
432    while (arguments.read("--rgba"))
433    {
434        _RGB = true;
435        _minimumNumberAlphaBits = 1;
436    }           
437
438    while (arguments.read("--stencil"))
439    {
440        _minimumNumberStencilBits = 1;
441    }
442
443    while (arguments.read("--accum-rgb"))
444    {
445        setMinimumNumAccumBits(8,8,8,0);
446    }
447
448    while (arguments.read("--accum-rgba"))
449    {
450        setMinimumNumAccumBits(8,8,8,8);
451    }
452
453    while(arguments.read("--samples",str))
454    {
455        _numMultiSamples = atoi(str.c_str());
456    }
457
458    while(arguments.read("--cc"))
459    {
460        _compileContextsHint = true;
461    }
462
463    while(arguments.read("--serialize-draw",str))
464    {
465        if (str=="ON") _serializeDrawDispatch = true;
466        else if (str=="OFF") _serializeDrawDispatch = false;
467    }
468
469    while(arguments.read("--num-db-threads",_numDatabaseThreadsHint)) {}
470    while(arguments.read("--num-http-threads",_numHttpDatabaseThreadsHint)) {}
471}
Note: See TracBrowser for help on using the browser.