root/OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osg/DisplaySettings.cpp @ 10413

Revision 10413, 17.5 kB (checked in by robert, 5 years ago)

From Mathias Froehlich, changes from atof to use osg::asciiToFloat() to avoid locale issues with atof

  • 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.");
188
189void DisplaySettings::readEnvironmentalVariables()
190{
191    const char* ptr = 0;
192   
193    if ((ptr = getenv("OSG_DISPLAY_TYPE")) != 0)
194    {
195        if (strcmp(ptr,"MONITOR")==0)
196        {
197            _displayType = MONITOR;
198        }
199        else
200        if (strcmp(ptr,"POWERWALL")==0)
201        {
202            _displayType = POWERWALL;
203        }
204        else
205        if (strcmp(ptr,"REALITY_CENTER")==0)
206        {
207            _displayType = REALITY_CENTER;
208        }
209        else
210        if (strcmp(ptr,"HEAD_MOUNTED_DISPLAY")==0)
211        {
212            _displayType = HEAD_MOUNTED_DISPLAY;
213        }
214    }
215   
216    if( (ptr = getenv("OSG_STEREO_MODE")) != 0)
217    {
218        if (strcmp(ptr,"QUAD_BUFFER")==0)
219        {
220            _stereoMode = QUAD_BUFFER;
221        }
222        else if (strcmp(ptr,"ANAGLYPHIC")==0)
223        {
224            _stereoMode = ANAGLYPHIC;
225        }
226        else if (strcmp(ptr,"HORIZONTAL_SPLIT")==0)
227        {
228            _stereoMode = HORIZONTAL_SPLIT;
229        }
230        else if (strcmp(ptr,"VERTICAL_SPLIT")==0)
231        {
232            _stereoMode = VERTICAL_SPLIT;
233        }
234        else if (strcmp(ptr,"LEFT_EYE")==0)
235        {
236            _stereoMode = LEFT_EYE;
237        }
238        else if (strcmp(ptr,"RIGHT_EYE")==0)
239        {
240            _stereoMode = RIGHT_EYE;
241        }
242        else if (strcmp(ptr,"HORIZONTAL_INTERLACE")==0)
243        {
244            _stereoMode = HORIZONTAL_INTERLACE;
245        }
246        else if (strcmp(ptr,"VERTICAL_INTERLACE")==0)
247        {
248            _stereoMode = VERTICAL_INTERLACE;
249        }
250        else if (strcmp(ptr,"CHECKERBOARD")==0)
251        {
252            _stereoMode = CHECKERBOARD;
253        }
254    }
255
256    if( (ptr = getenv("OSG_STEREO")) != 0)
257    {
258        if (strcmp(ptr,"OFF")==0)
259        {
260            _stereo = false;
261        }
262        else
263        if (strcmp(ptr,"ON")==0)
264        {
265            _stereo = true;
266        }
267    }
268
269    if( (ptr = getenv("OSG_EYE_SEPARATION")) != 0)
270    {
271        _eyeSeparation = osg::asciiToFloat(ptr);
272    }
273
274    if( (ptr = getenv("OSG_SCREEN_WIDTH")) != 0)
275    {
276        _screenWidth = osg::asciiToFloat(ptr);
277    }
278
279    if( (ptr = getenv("OSG_SCREEN_HEIGHT")) != 0)
280    {
281        _screenHeight = osg::asciiToFloat(ptr);
282    }
283
284    if( (ptr = getenv("OSG_SCREEN_DISTANCE")) != 0)
285    {
286        _screenDistance = osg::asciiToFloat(ptr);
287    }
288
289    if( (ptr = getenv("OSG_SPLIT_STEREO_HORIZONTAL_EYE_MAPPING")) != 0)
290    {
291        if (strcmp(ptr,"LEFT_EYE_LEFT_VIEWPORT")==0)
292        {
293            _splitStereoHorizontalEyeMapping = LEFT_EYE_LEFT_VIEWPORT;
294        }
295        else
296        if (strcmp(ptr,"LEFT_EYE_RIGHT_VIEWPORT")==0)
297        {
298            _splitStereoHorizontalEyeMapping = LEFT_EYE_RIGHT_VIEWPORT;
299        }
300    }
301
302    if( (ptr = getenv("OSG_SPLIT_STEREO_HORIZONTAL_SEPARATION")) != 0)
303    {
304        _splitStereoHorizontalSeparation = atoi(ptr);
305    }
306
307
308    if( (ptr = getenv("OSG_SPLIT_STEREO_VERTICAL_EYE_MAPPING")) != 0)
309    {
310        if (strcmp(ptr,"LEFT_EYE_TOP_VIEWPORT")==0)
311        {
312            _splitStereoVerticalEyeMapping = LEFT_EYE_TOP_VIEWPORT;
313        }
314        else
315        if (strcmp(ptr,"LEFT_EYE_BOTTOM_VIEWPORT")==0)
316        {
317            _splitStereoVerticalEyeMapping = LEFT_EYE_BOTTOM_VIEWPORT;
318        }
319    }
320   
321    if( (ptr = getenv("OSG_SPLIT_STEREO_AUTO_ADJUST_ASPECT_RATIO")) != 0)
322    {
323        if (strcmp(ptr,"OFF")==0)
324        {
325            _splitStereoAutoAdjustAspectRatio = false;
326        }
327        else
328        if (strcmp(ptr,"ON")==0)
329        {
330            _splitStereoAutoAdjustAspectRatio = true;
331        }
332    }
333
334    if( (ptr = getenv("OSG_SPLIT_STEREO_VERTICAL_SEPARATION")) != 0)
335    {
336        _splitStereoVerticalSeparation = atoi(ptr);
337    }
338
339    if( (ptr = getenv("OSG_MAX_NUMBER_OF_GRAPHICS_CONTEXTS")) != 0)
340    {
341        _maxNumOfGraphicsContexts = atoi(ptr);
342    }
343
344    if( (ptr = getenv("OSG_COMPIlE_CONTEXTS")) != 0)
345    {
346        if (strcmp(ptr,"OFF")==0)
347        {
348            _compileContextsHint = false;
349        }
350        else
351        if (strcmp(ptr,"ON")==0)
352        {
353            _compileContextsHint = true;
354        }
355    }
356   
357    if( (ptr = getenv("OSG_SERIALIZE_DRAW_DISPATCH")) != 0)
358    {
359        if (strcmp(ptr,"OFF")==0)
360        {
361            _serializeDrawDispatch = false;
362        }
363        else
364        if (strcmp(ptr,"ON")==0)
365        {
366            _serializeDrawDispatch = true;
367        }
368    }
369
370    if( (ptr = getenv("OSG_NUM_DATABASE_THREADS")) != 0)
371    {
372        _numDatabaseThreadsHint = atoi(ptr);
373    }
374
375    if( (ptr = getenv("OSG_NUM_HTTP_DATABASE_THREADS")) != 0)
376    {
377        _numHttpDatabaseThreadsHint = atoi(ptr);
378    }
379}
380
381void DisplaySettings::readCommandLine(ArgumentParser& arguments)
382{
383    if (_application.empty()) _application = arguments[0];
384
385    // report the usage options.
386    if (arguments.getApplicationUsage())
387    {
388        arguments.getApplicationUsage()->addCommandLineOption("--display <type>","MONITOR | POWERWALL | REALITY_CENTER | HEAD_MOUNTED_DISPLAY");
389        arguments.getApplicationUsage()->addCommandLineOption("--stereo","Use default stereo mode which is ANAGLYPHIC if not overriden by environmental variable");
390        arguments.getApplicationUsage()->addCommandLineOption("--stereo <mode>","ANAGLYPHIC | QUAD_BUFFER | HORIZONTAL_SPLIT | VERTICAL_SPLIT | LEFT_EYE | RIGHT_EYE | HORIZONTAL_INTERLACE | VERTICAL_INTERLACE | CHECKERBOARD | ON | OFF ");
391        arguments.getApplicationUsage()->addCommandLineOption("--rgba","Request a RGBA color buffer visual");
392        arguments.getApplicationUsage()->addCommandLineOption("--stencil","Request a stencil buffer visual");
393        arguments.getApplicationUsage()->addCommandLineOption("--accum-rgb","Request a rgb accumulator buffer visual");
394        arguments.getApplicationUsage()->addCommandLineOption("--accum-rgba","Request a rgb accumulator buffer visual");
395        arguments.getApplicationUsage()->addCommandLineOption("--samples <num>","Request a multisample visual");
396        arguments.getApplicationUsage()->addCommandLineOption("--cc","Request use of compile contexts and threads");
397        arguments.getApplicationUsage()->addCommandLineOption("--serialize-draw <mode>","OFF | ON - set the serialization of draw dispatch");
398    }
399
400    std::string str;
401    while(arguments.read("--display",str))
402    {
403        if (str=="MONITOR") _displayType = MONITOR;
404        else if (str=="POWERWALL") _displayType = POWERWALL;
405        else if (str=="REALITY_CENTER") _displayType = REALITY_CENTER;
406        else if (str=="HEAD_MOUNTED_DISPLAY") _displayType = HEAD_MOUNTED_DISPLAY;
407    }
408
409    int pos;
410    while ((pos=arguments.find("--stereo"))>0)
411    {
412        if (arguments.match(pos+1,"ANAGLYPHIC"))            { arguments.remove(pos,2); _stereo = true;_stereoMode = ANAGLYPHIC; }
413        else if (arguments.match(pos+1,"QUAD_BUFFER"))      { arguments.remove(pos,2); _stereo = true;_stereoMode = QUAD_BUFFER; }
414        else if (arguments.match(pos+1,"HORIZONTAL_SPLIT")) { arguments.remove(pos,2); _stereo = true;_stereoMode = HORIZONTAL_SPLIT; }
415        else if (arguments.match(pos+1,"VERTICAL_SPLIT"))   { arguments.remove(pos,2); _stereo = true;_stereoMode = VERTICAL_SPLIT; }
416        else if (arguments.match(pos+1,"HORIZONTAL_INTERLACE")) { arguments.remove(pos,2); _stereo = true;_stereoMode = HORIZONTAL_INTERLACE; }
417        else if (arguments.match(pos+1,"VERTICAL_INTERLACE"))   { arguments.remove(pos,2); _stereo = true;_stereoMode = VERTICAL_INTERLACE; }
418        else if (arguments.match(pos+1,"CHECKERBOARD"))     { arguments.remove(pos,2); _stereo = true;_stereoMode = CHECKERBOARD; }
419        else if (arguments.match(pos+1,"LEFT_EYE"))         { arguments.remove(pos,2); _stereo = true;_stereoMode = LEFT_EYE; }
420        else if (arguments.match(pos+1,"RIGHT_EYE"))        { arguments.remove(pos,2); _stereo = true;_stereoMode = RIGHT_EYE; }
421        else if (arguments.match(pos+1,"ON"))               { arguments.remove(pos,2); _stereo = true; }
422        else if (arguments.match(pos+1,"OFF"))              { arguments.remove(pos,2); _stereo = false; }
423        else                                                { arguments.remove(pos); _stereo = true; }
424    }
425
426    while (arguments.read("--rgba"))
427    {
428        _RGB = true;
429        _minimumNumberAlphaBits = 1;
430    }           
431
432    while (arguments.read("--stencil"))
433    {
434        _minimumNumberStencilBits = 1;
435    }
436
437    while (arguments.read("--accum-rgb"))
438    {
439        setMinimumNumAccumBits(8,8,8,0);
440    }
441
442    while (arguments.read("--accum-rgba"))
443    {
444        setMinimumNumAccumBits(8,8,8,8);
445    }
446
447    while(arguments.read("--samples",str))
448    {
449        _numMultiSamples = atoi(str.c_str());
450    }
451
452    while(arguments.read("--cc"))
453    {
454        _compileContextsHint = true;
455    }
456
457    while(arguments.read("--serialize-draw",str))
458    {
459        if (str=="ON") _serializeDrawDispatch = true;
460        else if (str=="OFF") _serializeDrawDispatch = false;
461    }
462
463    while(arguments.read("--num-db-threads",_numDatabaseThreadsHint)) {}
464    while(arguments.read("--num-http-threads",_numHttpDatabaseThreadsHint)) {}
465}
Note: See TracBrowser for help on using the browser.