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

Revision 9973, 17.7 kB (checked in by robert, 5 years ago)

Added OSG_MULTI_SAMPLES env var for setting the default DisplaySettings? NumMultiSamples? value

  • 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.");
187static ApplicationUsageProxy DisplaySetting_e17(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_MULTI_SAMPLES <int>","Set the hint for the number of samples to use when multi-sampling.");
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 = atof(ptr);
272    }
273
274    if( (ptr = getenv("OSG_SCREEN_WIDTH")) != 0)
275    {
276        _screenWidth = atof(ptr);
277    }
278
279    if( (ptr = getenv("OSG_SCREEN_HEIGHT")) != 0)
280    {
281        _screenHeight = atof(ptr);
282    }
283
284    if( (ptr = getenv("OSG_SCREEN_DISTANCE")) != 0)
285    {
286        _screenDistance = atof(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    if( (ptr = getenv("OSG_MULTI_SAMPLES")) != 0)
381    {
382        _numMultiSamples = atoi(ptr);
383    }
384}
385
386void DisplaySettings::readCommandLine(ArgumentParser& arguments)
387{
388    if (_application.empty()) _application = arguments[0];
389
390    // report the usage options.
391    if (arguments.getApplicationUsage())
392    {
393        arguments.getApplicationUsage()->addCommandLineOption("--display <type>","MONITOR | POWERWALL | REALITY_CENTER | HEAD_MOUNTED_DISPLAY");
394        arguments.getApplicationUsage()->addCommandLineOption("--stereo","Use default stereo mode which is ANAGLYPHIC if not overriden by environmental variable");
395        arguments.getApplicationUsage()->addCommandLineOption("--stereo <mode>","ANAGLYPHIC | QUAD_BUFFER | HORIZONTAL_SPLIT | VERTICAL_SPLIT | LEFT_EYE | RIGHT_EYE | HORIZONTAL_INTERLACE | VERTICAL_INTERLACE | CHECKERBOARD | ON | OFF ");
396        arguments.getApplicationUsage()->addCommandLineOption("--rgba","Request a RGBA color buffer visual");
397        arguments.getApplicationUsage()->addCommandLineOption("--stencil","Request a stencil buffer visual");
398        arguments.getApplicationUsage()->addCommandLineOption("--accum-rgb","Request a rgb accumulator buffer visual");
399        arguments.getApplicationUsage()->addCommandLineOption("--accum-rgba","Request a rgb accumulator buffer visual");
400        arguments.getApplicationUsage()->addCommandLineOption("--samples <num>","Request a multisample visual");
401        arguments.getApplicationUsage()->addCommandLineOption("--cc","Request use of compile contexts and threads");
402        arguments.getApplicationUsage()->addCommandLineOption("--serialize-draw <mode>","OFF | ON - set the serialization of draw dispatch");
403    }
404
405    std::string str;
406    while(arguments.read("--display",str))
407    {
408        if (str=="MONITOR") _displayType = MONITOR;
409        else if (str=="POWERWALL") _displayType = POWERWALL;
410        else if (str=="REALITY_CENTER") _displayType = REALITY_CENTER;
411        else if (str=="HEAD_MOUNTED_DISPLAY") _displayType = HEAD_MOUNTED_DISPLAY;
412    }
413
414    int pos;
415    while ((pos=arguments.find("--stereo"))>0)
416    {
417        if (arguments.match(pos+1,"ANAGLYPHIC"))            { arguments.remove(pos,2); _stereo = true;_stereoMode = ANAGLYPHIC; }
418        else if (arguments.match(pos+1,"QUAD_BUFFER"))      { arguments.remove(pos,2); _stereo = true;_stereoMode = QUAD_BUFFER; }
419        else if (arguments.match(pos+1,"HORIZONTAL_SPLIT")) { arguments.remove(pos,2); _stereo = true;_stereoMode = HORIZONTAL_SPLIT; }
420        else if (arguments.match(pos+1,"VERTICAL_SPLIT"))   { arguments.remove(pos,2); _stereo = true;_stereoMode = VERTICAL_SPLIT; }
421        else if (arguments.match(pos+1,"HORIZONTAL_INTERLACE")) { arguments.remove(pos,2); _stereo = true;_stereoMode = HORIZONTAL_INTERLACE; }
422        else if (arguments.match(pos+1,"VERTICAL_INTERLACE"))   { arguments.remove(pos,2); _stereo = true;_stereoMode = VERTICAL_INTERLACE; }
423        else if (arguments.match(pos+1,"CHECKERBOARD"))     { arguments.remove(pos,2); _stereo = true;_stereoMode = CHECKERBOARD; }
424        else if (arguments.match(pos+1,"LEFT_EYE"))         { arguments.remove(pos,2); _stereo = true;_stereoMode = LEFT_EYE; }
425        else if (arguments.match(pos+1,"RIGHT_EYE"))        { arguments.remove(pos,2); _stereo = true;_stereoMode = RIGHT_EYE; }
426        else if (arguments.match(pos+1,"ON"))               { arguments.remove(pos,2); _stereo = true; }
427        else if (arguments.match(pos+1,"OFF"))              { arguments.remove(pos,2); _stereo = false; }
428        else                                                { arguments.remove(pos); _stereo = true; }
429    }
430
431    while (arguments.read("--rgba"))
432    {
433        _RGB = true;
434        _minimumNumberAlphaBits = 1;
435    }           
436
437    while (arguments.read("--stencil"))
438    {
439        _minimumNumberStencilBits = 1;
440    }
441
442    while (arguments.read("--accum-rgb"))
443    {
444        setMinimumNumAccumBits(8,8,8,0);
445    }
446
447    while (arguments.read("--accum-rgba"))
448    {
449        setMinimumNumAccumBits(8,8,8,8);
450    }
451
452    while(arguments.read("--samples",str))
453    {
454        _numMultiSamples = atoi(str.c_str());
455    }
456
457    while(arguments.read("--cc"))
458    {
459        _compileContextsHint = true;
460    }
461
462    while(arguments.read("--serialize-draw",str))
463    {
464        if (str=="ON") _serializeDrawDispatch = true;
465        else if (str=="OFF") _serializeDrawDispatch = false;
466    }
467
468    while(arguments.read("--num-db-threads",_numDatabaseThreadsHint)) {}
469    while(arguments.read("--num-http-threads",_numHttpDatabaseThreadsHint)) {}
470}
Note: See TracBrowser for help on using the browser.