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

Revision 9191, 17.5 kB (checked in by robert, 6 years ago)

Added DisplaySettings::s/getApplication() to help with gecko plugin initialization when it requires the application name

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