1
/* -*-c++-*- Producer - Copyright (C) 2001-2004 Don Burns
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.
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.
15
#include <osg/ref_ptr>
16
#include <osg/io_utils>
18
#if defined(WIN32) && !defined(__CYGWIN__)
22
// set up for windows so acts just like unix access().
29
#ifdef _X11_IMPLEMENTATION
30
# include <X11/Xlib.h>
37
#include "CameraConfig.h"
40
using namespace osgProducer;
43
unsigned int CameraConfig::getNumberOfScreens()
46
return RenderSurface::getNumberOfScreens();
54
CameraConfig::CameraConfig() :
55
_can_add_visual_attributes(false),
56
_current_render_surface(NULL),
57
_can_add_render_surface_attributes(false),
58
_current_camera(NULL),
59
_can_add_camera_attributes(false),
61
_can_add_input_area_entries(false),
67
_offset_matrix[0] = 1.0; _offset_matrix[1] = 0.0; _offset_matrix[2] = 0.0; _offset_matrix[3] = 0.0;
68
_offset_matrix[4] = 0.0; _offset_matrix[5] = 1.0; _offset_matrix[6] = 0.0; _offset_matrix[7] = 0.0;
69
_offset_matrix[8] = 0.0; _offset_matrix[9] = 0.0; _offset_matrix[10] = 1.0; _offset_matrix[11] = 0.0;
70
_offset_matrix[12] = 0.0; _offset_matrix[13] = 0.0; _offset_matrix[14] = 0.0; _offset_matrix[15] = 1.0;
72
_threadModelDirective = CameraGroup::getDefaultThreadModel();
77
void CameraConfig::beginVisual( void )
79
_current_visual_chooser = new VisualChooser;
80
_can_add_visual_attributes = true;
83
void CameraConfig::beginVisual( const char * name )
85
std::pair<std::map<std::string,VisualChooser *>::iterator,bool> res =
86
_visual_map.insert(std::pair<std::string,VisualChooser *>(std::string(name), new VisualChooser));
87
_current_visual_chooser = (res.first)->second;
88
_can_add_visual_attributes = true;
91
void CameraConfig::setVisualSimpleConfiguration( void )
93
if( !_current_visual_chooser.valid() || _can_add_visual_attributes == false )
95
std::cerr << "CameraConfig::setVisualSimpleConfiguration() : ERROR no current visual\n";
98
_current_visual_chooser->setSimpleConfiguration();
101
void CameraConfig::setVisualByID( unsigned int id )
103
if( !_current_visual_chooser.valid() || _can_add_visual_attributes == false )
105
std::cerr << "CameraConfig::setVisualByID(id) : ERROR no current visual\n";
108
_current_visual_chooser->setVisualID( id );
111
void CameraConfig::addVisualAttribute( VisualChooser::AttributeName token, int param )
113
if( !_current_visual_chooser.valid() || _can_add_visual_attributes == false )
115
std::cerr << "CameraConfig::addVisualAttribute(token,param) : ERROR no current visual\n";
118
_current_visual_chooser->addAttribute( token, param );
121
void CameraConfig::addVisualAttribute( VisualChooser::AttributeName token )
123
if( !_current_visual_chooser.valid() || _can_add_visual_attributes == false)
125
std::cerr << "CameraConfig::addVisualAttribute(token) : ERROR no current visual\n";
128
_current_visual_chooser->addAttribute( token );
131
void CameraConfig::addVisualExtendedAttribute( unsigned int token )
133
if( !_current_visual_chooser.valid() || _can_add_visual_attributes == false)
135
std::cerr << "CameraConfig::addVisualExtendedAttribute(token) : ERROR no current visual\n";
138
_current_visual_chooser->addExtendedAttribute( token );
141
void CameraConfig::addVisualExtendedAttribute( unsigned int token, int param )
143
if( !_current_visual_chooser.valid() || _can_add_visual_attributes == false)
145
std::cerr << "CameraConfig::addVisualExtendedAttribute(token, param) : ERROR no current visual\n";
148
_current_visual_chooser->addExtendedAttribute( token, param );
151
void CameraConfig::endVisual( void )
153
_can_add_visual_attributes = false;
156
VisualChooser *CameraConfig::findVisual( const char *name )
158
std::map<std::string, VisualChooser *>::iterator p;
159
p = _visual_map.find( std::string(name) );
160
if( p == _visual_map.end() )
166
void CameraConfig::beginRenderSurface( const char *name )
168
std::pair<std::map<std::string, osg::ref_ptr<RenderSurface> >::iterator,bool> res =
169
_render_surface_map.insert(std::pair<std::string, osg::ref_ptr< RenderSurface> >(
172
_current_render_surface = (res.first)->second.get();
173
_current_render_surface->setWindowName( std::string(name) );
174
_can_add_render_surface_attributes = true;
177
void CameraConfig::setRenderSurfaceVisualChooser( const char *name )
179
VisualChooser *vc = findVisual( name );
180
if( vc != NULL && _current_render_surface != NULL )
181
_current_render_surface->setVisualChooser( vc );
184
void CameraConfig::setRenderSurfaceVisualChooser( void )
186
if( _current_render_surface != NULL && _current_visual_chooser.valid() )
187
_current_render_surface->setVisualChooser( _current_visual_chooser.get() );
190
void CameraConfig::setRenderSurfaceWindowRectangle( int x, int y, unsigned int width, unsigned int height )
192
if( _current_render_surface != NULL )
193
_current_render_surface->setWindowRectangle( x, y, width, height );
196
void CameraConfig::setRenderSurfaceCustomFullScreenRectangle( int x, int y, unsigned int width, unsigned int height )
198
if( _current_render_surface != NULL )
199
_current_render_surface->setCustomFullScreenRectangle( x, y, width, height );
202
void CameraConfig::setRenderSurfaceOverrideRedirect( bool flag )
204
if( _current_render_surface != NULL )
205
_current_render_surface->useOverrideRedirect( flag );
208
void CameraConfig::setRenderSurfaceHostName( const std::string &name )
210
if( _current_render_surface != NULL )
211
_current_render_surface->setHostName( name );
214
void CameraConfig::setRenderSurfaceDisplayNum( int n )
216
if( _current_render_surface != NULL )
217
_current_render_surface->setDisplayNum( n );
220
void CameraConfig::setRenderSurfaceScreen( int n )
222
if( _current_render_surface != NULL )
223
_current_render_surface->setScreenNum( n );
226
void CameraConfig::setRenderSurfaceBorder( bool flag )
228
if( _current_render_surface != NULL )
229
_current_render_surface->useBorder( flag );
232
void CameraConfig::setRenderSurfaceDrawableType( RenderSurface::DrawableType drawableType )
234
if( _current_render_surface != NULL )
235
_current_render_surface->setDrawableType( drawableType );
238
void CameraConfig::setRenderSurfaceRenderToTextureMode( RenderSurface::RenderToTextureMode rttMode )
240
if( _current_render_surface != NULL )
241
_current_render_surface->setRenderToTextureMode( rttMode );
244
void CameraConfig::setRenderSurfaceReadDrawable( const char *name )
246
if( _current_render_surface != NULL )
248
osgProducer::RenderSurface *readDrawable = findRenderSurface( name );
249
if( readDrawable == NULL )
251
std::cerr << "setRenderSurfaceReadDrawable(): No Render Surface by name of \"" << name << "\" was found!\n";
254
_current_render_surface->setReadDrawable( readDrawable );
258
void CameraConfig::setRenderSurfaceInputRectangle( float x0, float x1, float y0, float y1 )
260
if( _current_render_surface != NULL )
261
_current_render_surface->setInputRectangle( RenderSurface::InputRectangle(x0,x1,y0,y1) );
264
void CameraConfig::endRenderSurface( void )
266
_can_add_render_surface_attributes = false;
269
RenderSurface *CameraConfig::findRenderSurface( const char *name )
271
std::map<std::string, osg::ref_ptr<RenderSurface> >::iterator p;
272
p = _render_surface_map.find( std::string(name) );
273
if( p == _render_surface_map.end() )
276
return (*p).second.get();
279
unsigned int CameraConfig::getNumberOfRenderSurfaces()
281
return _render_surface_map.size();
284
RenderSurface *CameraConfig::getRenderSurface( unsigned int index )
286
if( index >= _render_surface_map.size() )
288
std::map <std::string, osg::ref_ptr<RenderSurface> >::iterator p;
291
for( p = _render_surface_map.begin(); p != _render_surface_map.end(); p++ )
294
if( p == _render_surface_map.end() )
296
return (p->second.get());
299
void CameraConfig::addCamera( std::string name, Camera *camera )
301
std::pair<std::map<std::string, osg::ref_ptr<Camera> >::iterator,bool> res =
302
_camera_map.insert(std::pair<std::string, osg::ref_ptr<Camera> >(name, camera));
303
_current_camera = (res.first)->second.get();
304
_can_add_camera_attributes = true;
306
RenderSurface *rs = camera->getRenderSurface();
307
if( rs->getWindowName() == osgProducer::RenderSurface::defaultWindowName )
310
sprintf( name, "%s (%02d)", osgProducer::RenderSurface::defaultWindowName.c_str(), (int)_render_surface_map.size() );
311
rs->setWindowName( name );
313
_render_surface_map.insert(std::pair<std::string, osg::ref_ptr<RenderSurface> >( rs->getWindowName(), rs ));
317
void CameraConfig::beginCamera( std::string name )
319
Camera *camera = new Camera;
320
std::pair<std::map<std::string, osg::ref_ptr<Camera> >::iterator,bool> res =
321
_camera_map.insert(std::pair<std::string, osg::ref_ptr<Camera> >(name, camera));
322
_current_camera = (res.first)->second.get();
323
_can_add_camera_attributes = true;
326
void CameraConfig::setCameraRenderSurface( const char *name )
328
RenderSurface *rs = findRenderSurface( name );
331
std::cerr << "setCameraRenderSurface(): No Render Surface by name of \"" << name << "\" was found!\n";
335
if( rs != NULL && _current_camera != NULL )
336
_current_camera->setRenderSurface( rs );
340
void CameraConfig::setCameraRenderSurface( void )
343
if( _current_camera != NULL && _current_render_surface != NULL )
344
_current_camera->setRenderSurface( _current_render_surface.get() );
348
void CameraConfig::setCameraProjectionRectangle( float x0, float x1, float y0, float y1 )
350
if( _current_camera != NULL )
351
_current_camera->setProjectionRectangle( x0, x1, y0, y1 );
354
void CameraConfig::setCameraProjectionRectangle( int x0, int x1, int y0, int y1 )
356
if( _current_camera != NULL )
357
_current_camera->setProjectionRectangle( x0, x1, y0, y1 );
360
void CameraConfig::setCameraOrtho( float left, float right, float bottom, float top, float nearClip, float farClip,
361
float xshear, float yshear )
363
if( _current_camera != NULL )
364
_current_camera->setLensOrtho( left, right, bottom, top, nearClip, farClip, xshear, yshear );
367
void CameraConfig::setCameraPerspective( float hfov, float vfov, float nearClip, float farClip,
368
float xshear, float yshear )
370
if( _current_camera != 0 )
371
_current_camera->setLensPerspective( hfov, vfov, nearClip, farClip, xshear, yshear );
374
void CameraConfig::setCameraFrustum( float left, float right, float bottom, float top, float nearClip, float farClip,
375
float xshear, float yshear )
377
if( _current_camera != 0 )
378
_current_camera->setLensFrustum( left, right, bottom, top, nearClip, farClip, xshear, yshear );
381
void CameraConfig::setCameraLensShear( osg::Matrix::value_type xshear, osg::Matrix::value_type yshear )
383
if( _current_camera != NULL )
384
_current_camera->setLensShear(xshear,yshear);
387
void CameraConfig::setCameraShareLens( bool shared )
389
if( _current_camera != NULL )
390
_current_camera->setShareLens( shared );
393
void CameraConfig::setCameraShareView( bool shared )
395
if( _current_camera != NULL )
396
_current_camera->setShareView( shared );
399
void CameraConfig::setCameraClearColor( float r, float g, float b, float a )
401
if( _current_camera != NULL )
402
_current_camera->setClearColor( r, g, b, a );
405
void CameraConfig::beginCameraOffset()
407
osg::Matrix::value_type id[] = {
413
memcpy( _offset_matrix, id, sizeof(osg::Matrix::value_type[16]));
414
_offset_shearx = _offset_sheary = 0.0;
417
void CameraConfig::shearCameraOffset( osg::Matrix::value_type shearx, osg::Matrix::value_type sheary )
419
_offset_shearx = shearx;
420
_offset_sheary = sheary;
423
void CameraConfig::setCameraOffsetMultiplyMethod( Camera::Offset::MultiplyMethod method )
425
if( _current_camera != NULL )
426
_current_camera->setOffsetMultiplyMethod(method);
430
void CameraConfig::endCameraOffset()
432
if( _current_camera != NULL )
433
_current_camera->setOffset( _offset_matrix, _offset_shearx, _offset_sheary);
436
void CameraConfig::endCamera( void )
438
_can_add_camera_attributes = false;
441
Camera *CameraConfig::findCamera( const char *name )
443
std::map<std::string, osg::ref_ptr<Camera> >::iterator p;
444
p = _camera_map.find( std::string(name) );
445
if( p == _camera_map.end() )
448
return (*p).second.get();
451
unsigned int CameraConfig::getNumberOfCameras() const
453
return _camera_map.size();
456
const Camera *CameraConfig::getCamera( unsigned int n ) const
458
if( n >= _camera_map.size() )
462
std::map <std::string, osg::ref_ptr<Camera> >::const_iterator p;
463
for( i = 0, p = _camera_map.begin(); p != _camera_map.end(); p++ )
466
if( p == _camera_map.end() )
468
return p->second.get();
471
Camera *CameraConfig::getCamera( unsigned int n )
473
if( n >= _camera_map.size() )
477
std::map <std::string, osg::ref_ptr<Camera> >::iterator p;
478
for( i = 0, p = _camera_map.begin(); p != _camera_map.end(); p++ )
481
if( p == _camera_map.end() )
483
return p->second.get();
486
void CameraConfig::beginInputArea()
488
_input_area = new InputArea;
489
_can_add_input_area_entries = true;
492
void CameraConfig::addInputAreaEntry( char *renderSurfaceName )
494
osgProducer::RenderSurface *rs = findRenderSurface( renderSurfaceName );
497
std::cerr << "setInputAreaEntry(): No Render Surface by name of \"" << renderSurfaceName << "\" was found!\n";
500
if( _input_area != NULL && _can_add_input_area_entries == true )
501
_input_area->addRenderSurface( rs );
504
void CameraConfig::endInputArea()
506
_can_add_input_area_entries = false;
509
void CameraConfig::setInputArea(InputArea *ia)
514
InputArea *CameraConfig::getInputArea()
516
return _input_area.get();
519
const InputArea *CameraConfig::getInputArea() const
521
return _input_area.get();
525
void CameraConfig::realize( void )
527
std::map <std::string, osg::ref_ptr<RenderSurface> >::iterator p;
528
for( p = _render_surface_map.begin(); p != _render_surface_map.end(); p++ )
530
((*p).second)->realize();
535
std::string findFile( std::string );
537
void CameraConfig::addStereoSystemCommand( int screen, std::string stereoCmd, std::string monoCmd )
539
_stereoSystemCommands.push_back(StereoSystemCommand( screen, stereoCmd, monoCmd ));
542
const std::vector<CameraConfig::StereoSystemCommand> &CameraConfig::getStereoSystemCommands()
544
return _stereoSystemCommands;
548
CameraConfig::~CameraConfig()
552
//////////////////////
554
void CameraConfig::rotateCameraOffset( osg::Matrix::value_type deg, osg::Matrix::value_type x, osg::Matrix::value_type y, osg::Matrix::value_type z )
557
m.invert(osg::Matrix::rotate( osg::DegreesToRadians(deg), x,y,z));
558
m = m * osg::Matrix(_offset_matrix);
559
memcpy( _offset_matrix, m.ptr(), sizeof( osg::Matrix::value_type[16] ));
562
void CameraConfig::translateCameraOffset( osg::Matrix::value_type x, osg::Matrix::value_type y, osg::Matrix::value_type z )
565
m.invert(osg::Matrix::translate( x,y,z));
566
m = m * osg::Matrix(_offset_matrix);
567
memcpy( _offset_matrix, m.ptr(), sizeof( osg::Matrix::value_type[16] ));
570
void CameraConfig::scaleCameraOffset( osg::Matrix::value_type x, osg::Matrix::value_type y, osg::Matrix::value_type z )
572
osg::Matrix m = osg::Matrix::scale( x,y,z) * osg::Matrix(_offset_matrix);
573
memcpy( _offset_matrix, m.ptr(), sizeof( osg::Matrix::value_type[16] ));
576
bool CameraConfig::fileExists(const std::string& filename)
578
return access( filename.c_str(), F_OK ) == 0;
581
// Order of precedence:
583
std::string CameraConfig::findFile( std::string filename )
585
if (filename.empty()) return filename;
589
char *ptr = getenv( "PRODUCER_CONFIG_FILE_PATH");
592
path = std::string(ptr) + '/' + filename;
593
if( fileExists(path))
597
// Check standard location(s)
599
path = std::string( "/usr/local/share/Producer/Config/") + filename;
600
if( fileExists(path) )
604
path = std::string( "/usr/share/Producer/Config/") + filename;
605
if( fileExists(path) )
608
// Check local directory
609
if(fileExists(filename))
613
return std::string();
616
bool CameraConfig::defaultConfig()
618
if( getNumberOfCameras() != 0 ) return false;
620
char *env = getenv( "PRODUCER_CAMERA_CONFIG_FILE" );
622
// Backwards compatibility
624
env = getenv( "PRODUCER_CONFIG_FILE" );
628
std::string file = findFile(env);
629
return parseFile( file.c_str() );
632
unsigned int numScreens = getNumberOfScreens();
633
if( numScreens == 0 )
636
float xshear = float(numScreens-1);
638
float input_xMin = -1.0f;
639
float input_yMin = -1.0f;
640
float input_width = 2.0f/float(numScreens);
641
float input_height = 2.0f;
643
// use an InputArea if there is more than one screen.
644
InputArea *ia = (numScreens>1) ? new InputArea : 0;
647
for( unsigned int i = 0; i < numScreens; i++ )
649
std::string name = "Screen" + i;
650
std::pair<std::map<std::string, osg::ref_ptr<Camera> >::iterator,bool> res =
651
_camera_map.insert(std::pair<std::string, osg::ref_ptr<Camera> >(name, new Camera));
653
((res.first)->second)->getRenderSurface()->setScreenNum( i );
654
((res.first)->second)->setLensShear( xshear, yshear );
656
RenderSurface *rs = ((res.first)->second)->getRenderSurface();
657
rs->setWindowName( name );
660
rs->setInputRectangle( RenderSurface::InputRectangle(input_xMin, input_xMin+input_width, input_yMin, input_yMin+input_height) );
661
input_xMin += input_width;
662
ia->addRenderSurface(rs);
665
_render_surface_map.insert(std::pair<std::string,
666
osg::ref_ptr<RenderSurface> >( rs->getWindowName(), rs ));
671
_threadModelDirective = CameraGroup::getDefaultThreadModel();