root/OpenSceneGraph/trunk/src/osgPlugins/cfg/CameraConfig.cpp @ 13041

Revision 13041, 19.5 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- Producer - Copyright (C) 2001-2004  Don Burns
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
14#include <osg/Notify>
15#include <osg/ref_ptr>
16#include <osg/io_utils>
17
18#include <osgDB/FileUtils>
19
20#ifdef _X11_IMPLEMENTATION
21include <X11/Xlib.h>
22#endif
23
24#include <memory.h>
25#include <stdlib.h>
26#include <iostream>
27#include <sstream>
28
29#include "CameraConfig.h"
30
31
32using namespace osgProducer;
33
34
35unsigned int CameraConfig::getNumberOfScreens()
36{
37#if 0
38    return RenderSurface::getNumberOfScreens();
39#else
40    return 1;
41#endif
42}
43
44
45//////////////////
46CameraConfig::CameraConfig() :
47    _can_add_visual_attributes(false),
48    _current_render_surface(NULL),
49    _can_add_render_surface_attributes(false),
50    _current_camera(NULL),
51    _can_add_camera_attributes(false),
52    _input_area(NULL),
53    _can_add_input_area_entries(false),
54    _offset_shearx(0.0f),
55    _offset_sheary(0.0f),
56    _postmultiply(false)
57
58{
59    _offset_matrix[0] = 1.0; _offset_matrix[1] = 0.0; _offset_matrix[2] = 0.0; _offset_matrix[3] = 0.0;
60    _offset_matrix[4] = 0.0; _offset_matrix[5] = 1.0; _offset_matrix[6] = 0.0; _offset_matrix[7] = 0.0;
61    _offset_matrix[8] = 0.0; _offset_matrix[9] = 0.0; _offset_matrix[10] = 1.0; _offset_matrix[11] = 0.0;
62    _offset_matrix[12] = 0.0; _offset_matrix[13] = 0.0; _offset_matrix[14] = 0.0; _offset_matrix[15] = 1.0;
63
64    _threadModelDirective = CameraGroup::getDefaultThreadModel();
65
66}
67
68
69void CameraConfig::beginVisual( void )
70{
71    _current_visual_chooser = new VisualChooser;
72    _can_add_visual_attributes = true;
73}
74
75void CameraConfig::beginVisual( const char * name )
76{
77    std::pair<std::map<std::string,VisualChooser *>::iterator,bool> res =
78      _visual_map.insert(std::pair<std::string,VisualChooser *>(std::string(name), new VisualChooser));
79    _current_visual_chooser = (res.first)->second;
80    _can_add_visual_attributes = true;
81}
82
83void CameraConfig::setVisualSimpleConfiguration( void )
84{
85     if( !_current_visual_chooser.valid() || _can_add_visual_attributes == false )
86     {
87         std::cerr << "CameraConfig::setVisualSimpleConfiguration() : ERROR no current visual\n";
88         return;
89     }
90     _current_visual_chooser->setSimpleConfiguration();
91}
92
93void CameraConfig::setVisualByID( unsigned int id )
94{
95     if( !_current_visual_chooser.valid() || _can_add_visual_attributes == false )
96     {
97         std::cerr << "CameraConfig::setVisualByID(id) : ERROR no current visual\n";
98         return;
99     }
100     _current_visual_chooser->setVisualID( id );
101}
102
103void CameraConfig::addVisualAttribute( VisualChooser::AttributeName token, int param )
104{
105     if( !_current_visual_chooser.valid() || _can_add_visual_attributes == false )
106     {
107         std::cerr << "CameraConfig::addVisualAttribute(token,param) : ERROR no current visual\n";
108         return;
109     }
110     _current_visual_chooser->addAttribute( token, param );
111}
112
113void CameraConfig::addVisualAttribute( VisualChooser::AttributeName token )
114{
115     if( !_current_visual_chooser.valid()  || _can_add_visual_attributes == false)
116     {
117         std::cerr << "CameraConfig::addVisualAttribute(token) : ERROR no current visual\n";
118         return;
119     }
120     _current_visual_chooser->addAttribute( token );
121}
122
123void CameraConfig::addVisualExtendedAttribute( unsigned int token )
124{
125     if( !_current_visual_chooser.valid()  || _can_add_visual_attributes == false)
126     {
127         std::cerr << "CameraConfig::addVisualExtendedAttribute(token) : ERROR no current visual\n";
128         return;
129     }
130     _current_visual_chooser->addExtendedAttribute( token );
131}
132
133void CameraConfig::addVisualExtendedAttribute( unsigned int token, int param )
134{
135     if( !_current_visual_chooser.valid()  || _can_add_visual_attributes == false)
136     {
137         std::cerr << "CameraConfig::addVisualExtendedAttribute(token, param) : ERROR no current visual\n";
138         return;
139     }
140     _current_visual_chooser->addExtendedAttribute( token, param );
141}
142
143void CameraConfig::endVisual( void )
144{
145    _can_add_visual_attributes = false;
146}
147
148VisualChooser *CameraConfig::findVisual( const char *name )
149{
150    std::map<std::string, VisualChooser *>::iterator p;
151    p = _visual_map.find( std::string(name) );
152    if( p == _visual_map.end() )
153        return NULL;
154    else
155            return (*p).second;
156}
157
158void CameraConfig::beginRenderSurface( const char *name )
159{
160    std::pair<std::map<std::string,  osg::ref_ptr<RenderSurface> >::iterator,bool> res =
161      _render_surface_map.insert(std::pair<std::string, osg::ref_ptr< RenderSurface> >(
162                                      std::string(name),
163                                      new RenderSurface));
164    _current_render_surface = (res.first)->second.get();
165    _current_render_surface->setWindowName( std::string(name) );
166    _can_add_render_surface_attributes = true;
167}
168
169void CameraConfig::setRenderSurfaceVisualChooser( const char *name )
170{
171    VisualChooser *vc = findVisual( name );
172    if( vc != NULL && _current_render_surface != NULL )
173        _current_render_surface->setVisualChooser( vc );
174}
175
176void CameraConfig::setRenderSurfaceVisualChooser( void )
177{
178    if( _current_render_surface != NULL && _current_visual_chooser.valid() )
179        _current_render_surface->setVisualChooser( _current_visual_chooser.get() );
180}
181
182void CameraConfig::setRenderSurfaceWindowRectangle( int x, int y,  unsigned int width, unsigned int height )
183{
184    if( _current_render_surface != NULL )
185        _current_render_surface->setWindowRectangle( x, y, width, height );
186}
187
188void CameraConfig::setRenderSurfaceCustomFullScreenRectangle( int x, int y, unsigned int width, unsigned int height )
189{
190    if( _current_render_surface != NULL )
191        _current_render_surface->setCustomFullScreenRectangle( x, y, width, height );
192}
193
194void CameraConfig::setRenderSurfaceOverrideRedirect( bool flag )
195{
196    if( _current_render_surface != NULL )
197        _current_render_surface->useOverrideRedirect( flag );
198}
199
200void CameraConfig::setRenderSurfaceHostName( const std::string &name )
201{
202    if( _current_render_surface != NULL )
203        _current_render_surface->setHostName( name );
204}
205
206void CameraConfig::setRenderSurfaceDisplayNum( int n )
207{
208    if( _current_render_surface != NULL )
209        _current_render_surface->setDisplayNum( n );
210}
211
212void CameraConfig::setRenderSurfaceScreen( int n )
213{
214    if( _current_render_surface != NULL )
215        _current_render_surface->setScreenNum( n );
216}
217
218void CameraConfig::setRenderSurfaceBorder( bool flag )
219{
220    if( _current_render_surface != NULL )
221        _current_render_surface->useBorder( flag );
222}
223
224void CameraConfig::setRenderSurfaceDrawableType( RenderSurface::DrawableType drawableType )
225{
226    if( _current_render_surface != NULL )
227        _current_render_surface->setDrawableType( drawableType );
228}
229
230void CameraConfig::setRenderSurfaceRenderToTextureMode( RenderSurface::RenderToTextureMode rttMode )
231{
232    if( _current_render_surface != NULL )
233        _current_render_surface->setRenderToTextureMode( rttMode );
234}
235
236void CameraConfig::setRenderSurfaceReadDrawable( const char *name )
237{
238    if( _current_render_surface != NULL )
239    {
240        osgProducer::RenderSurface *readDrawable = findRenderSurface( name );
241        if( readDrawable == NULL )
242        {
243            std::cerr << "setRenderSurfaceReadDrawable(): No Render Surface by name of \"" << name << "\" was found!\n";
244            return;
245        }
246        _current_render_surface->setReadDrawable( readDrawable );
247    }
248}
249
250void CameraConfig::setRenderSurfaceInputRectangle( float x0, float x1, float y0, float y1 )
251{
252    if( _current_render_surface != NULL )
253        _current_render_surface->setInputRectangle( RenderSurface::InputRectangle(x0,x1,y0,y1) );
254}
255
256void CameraConfig::endRenderSurface( void )
257{
258    _can_add_render_surface_attributes = false;
259}
260
261RenderSurface *CameraConfig::findRenderSurface( const char *name )
262{
263    std::map<std::string,  osg::ref_ptr<RenderSurface> >::iterator p;
264    p = _render_surface_map.find( std::string(name) );
265    if( p == _render_surface_map.end() )
266        return NULL;
267    else
268        return (*p).second.get();
269}
270
271unsigned int CameraConfig::getNumberOfRenderSurfaces()
272{
273    return _render_surface_map.size();
274}
275
276RenderSurface *CameraConfig::getRenderSurface( unsigned int index )
277{
278    if( index >= _render_surface_map.size() )
279            return NULL;
280    std::map <std::string,  osg::ref_ptr<RenderSurface> >::iterator p;
281
282    unsigned int i = 0;
283    for( p = _render_surface_map.begin(); p != _render_surface_map.end(); p++ )
284        if( i++ == index )
285                break;
286    if(  p == _render_surface_map.end() )
287            return NULL;
288    return (p->second.get());
289}
290
291void CameraConfig::addCamera( std::string name, Camera *camera )
292{
293    std::pair<std::map<std::string, osg::ref_ptr<Camera> >::iterator,bool> res =
294      _camera_map.insert(std::pair<std::string, osg::ref_ptr<Camera> >(name, camera));
295    _current_camera = (res.first)->second.get();
296    _can_add_camera_attributes = true;
297
298    RenderSurface *rs = camera->getRenderSurface();
299    if( rs->getWindowName() == osgProducer::RenderSurface::defaultWindowName )
300    {
301        char name[80];
302        sprintf( name, "%s (%02d)", osgProducer::RenderSurface::defaultWindowName.c_str(), (int)_render_surface_map.size() );
303        rs->setWindowName( name );
304    }
305    _render_surface_map.insert(std::pair<std::string, osg::ref_ptr<RenderSurface> >( rs->getWindowName(), rs ));
306}
307
308
309void CameraConfig::beginCamera( std::string name )
310{
311    Camera *camera = new Camera;
312    std::pair<std::map<std::string,  osg::ref_ptr<Camera> >::iterator,bool> res =
313      _camera_map.insert(std::pair<std::string, osg::ref_ptr<Camera> >(name, camera));
314    _current_camera = (res.first)->second.get();
315    _can_add_camera_attributes = true;
316}
317
318void CameraConfig::setCameraRenderSurface( const char *name )
319{
320    RenderSurface *rs = findRenderSurface( name );
321    if( rs == NULL )
322    {
323        std::cerr << "setCameraRenderSurface(): No Render Surface by name of \"" << name << "\" was found!\n";
324        return;
325    }
326
327    if( rs != NULL && _current_camera != NULL )
328        _current_camera->setRenderSurface( rs );
329
330}
331
332void CameraConfig::setCameraRenderSurface( void )
333{
334
335    if( _current_camera != NULL && _current_render_surface != NULL )
336        _current_camera->setRenderSurface( _current_render_surface.get() );
337
338}
339
340void CameraConfig::setCameraProjectionRectangle( float x0, float x1, float y0, float y1 )
341{
342    if( _current_camera != NULL )
343        _current_camera->setProjectionRectangle( x0, x1, y0, y1 );
344}
345
346void CameraConfig::setCameraProjectionRectangle( int x0, int x1, int y0, int y1 )
347{
348    if( _current_camera != NULL )
349        _current_camera->setProjectionRectangle( x0, x1, y0, y1 );
350}
351
352void CameraConfig::setCameraOrtho( float left, float right, float bottom, float top, float nearClip, float farClip,
353                        float xshear, float yshear )
354{
355    if( _current_camera != NULL )
356        _current_camera->setLensOrtho( left, right, bottom, top, nearClip, farClip, xshear, yshear );
357}
358
359void CameraConfig::setCameraPerspective( float hfov, float vfov, float nearClip, float farClip,
360                        float xshear, float yshear )
361{
362    if( _current_camera != 0 )
363        _current_camera->setLensPerspective( hfov, vfov, nearClip, farClip, xshear, yshear );
364}
365
366void CameraConfig::setCameraFrustum( float left, float right, float bottom, float top, float nearClip, float farClip,
367                        float xshear, float yshear )
368{
369    if( _current_camera != 0 )
370        _current_camera->setLensFrustum( left, right, bottom, top, nearClip, farClip, xshear, yshear );
371}
372
373void CameraConfig::setCameraLensShear( osg::Matrix::value_type xshear, osg::Matrix::value_type yshear )
374{
375    if( _current_camera != NULL )
376        _current_camera->setLensShear(xshear,yshear);
377}
378
379void CameraConfig::setCameraShareLens( bool shared )
380{
381    if( _current_camera != NULL )
382        _current_camera->setShareLens( shared );
383}
384
385void CameraConfig::setCameraShareView( bool shared )
386{
387    if( _current_camera != NULL )
388        _current_camera->setShareView( shared );
389}
390
391void CameraConfig::setCameraClearColor( float r, float g, float b, float a )
392{
393    if( _current_camera != NULL )
394        _current_camera->setClearColor( r, g, b, a );
395}
396
397void CameraConfig::beginCameraOffset()
398{
399  osg::Matrix::value_type id[] = {
400        1, 0, 0, 0,
401        0, 1, 0, 0,
402        0, 0, 1, 0,
403        0, 0, 0, 1
404        };
405  memcpy( _offset_matrix, id, sizeof(osg::Matrix::value_type[16]));
406    _offset_shearx = _offset_sheary = 0.0;
407}
408
409void CameraConfig::shearCameraOffset( osg::Matrix::value_type shearx, osg::Matrix::value_type sheary )
410{
411    _offset_shearx = shearx;
412    _offset_sheary = sheary;
413}
414
415void CameraConfig::setCameraOffsetMultiplyMethod( Camera::Offset::MultiplyMethod method )
416{
417    if( _current_camera != NULL )
418        _current_camera->setOffsetMultiplyMethod(method);
419}
420
421
422void CameraConfig::endCameraOffset()
423{
424    if( _current_camera != NULL )
425        _current_camera->setOffset( _offset_matrix, _offset_shearx, _offset_sheary);
426}
427
428void  CameraConfig::endCamera( void )
429{
430    _can_add_camera_attributes = false;
431}
432
433Camera *CameraConfig::findCamera( const char *name )
434{
435    std::map<std::string,  osg::ref_ptr<Camera> >::iterator p;
436    p = _camera_map.find( std::string(name) );
437    if( p == _camera_map.end() )
438        return NULL;
439    else
440        return (*p).second.get();
441}
442
443unsigned int CameraConfig::getNumberOfCameras() const
444{
445    return _camera_map.size();
446}
447
448const Camera *CameraConfig::getCamera( unsigned int n ) const
449{
450    if( n >= _camera_map.size() )
451            return NULL;
452
453    unsigned int i;
454    std::map <std::string,  osg::ref_ptr<Camera> >::const_iterator p;
455    for( i = 0, p = _camera_map.begin(); p != _camera_map.end(); p++ )
456        if( i++ == n )
457                break;
458    if(  p == _camera_map.end() )
459            return NULL;
460    return p->second.get();
461}
462
463Camera *CameraConfig::getCamera( unsigned int n )
464{
465    if( n >= _camera_map.size() )
466            return NULL;
467
468    unsigned int i;
469    std::map <std::string,  osg::ref_ptr<Camera> >::iterator p;
470    for( i = 0, p = _camera_map.begin(); p != _camera_map.end(); p++ )
471        if( i++ == n )
472                break;
473    if(  p == _camera_map.end() )
474            return NULL;
475    return p->second.get();
476}
477
478void CameraConfig::beginInputArea()
479{
480    _input_area = new InputArea;
481    _can_add_input_area_entries = true;
482}
483
484void CameraConfig::addInputAreaEntry( char *renderSurfaceName )
485{
486    osgProducer::RenderSurface *rs = findRenderSurface( renderSurfaceName );
487    if( rs == NULL )
488    {
489        std::cerr << "setInputAreaEntry(): No Render Surface by name of \"" << renderSurfaceName << "\" was found!\n";
490        return;
491    }
492    if( _input_area != NULL && _can_add_input_area_entries == true )
493        _input_area->addRenderSurface( rs );
494}
495
496void CameraConfig::endInputArea()
497{
498    _can_add_input_area_entries = false;
499}
500
501void CameraConfig::setInputArea(InputArea *ia)
502{
503    _input_area=ia;
504}
505
506InputArea *CameraConfig::getInputArea()
507{
508    return _input_area.get();
509}
510
511const InputArea *CameraConfig::getInputArea() const
512{
513    return _input_area.get();
514}
515
516#if 0
517void CameraConfig::realize( void )
518{
519    std::map <std::string,  osg::ref_ptr<RenderSurface> >::iterator p;
520    for( p = _render_surface_map.begin(); p != _render_surface_map.end(); p++ )
521    {
522        ((*p).second)->realize();
523    }
524}
525#endif
526
527std::string findFile( std::string );
528
529void CameraConfig::addStereoSystemCommand( int screen, std::string stereoCmd, std::string monoCmd )
530{
531    _stereoSystemCommands.push_back(StereoSystemCommand( screen, stereoCmd, monoCmd ));
532}
533
534const std::vector<CameraConfig::StereoSystemCommand> &CameraConfig::getStereoSystemCommands()
535{
536    return _stereoSystemCommands;
537}
538
539
540CameraConfig::~CameraConfig()
541{
542}
543
544//////////////////////
545
546void CameraConfig::rotateCameraOffset( osg::Matrix::value_type deg, osg::Matrix::value_type x, osg::Matrix::value_type y, osg::Matrix::value_type z )
547{
548  osg::Matrix m;
549  m.invert(osg::Matrix::rotate( osg::DegreesToRadians(deg), x,y,z));
550  m = m * osg::Matrix(_offset_matrix);
551    memcpy( _offset_matrix, m.ptr(), sizeof( osg::Matrix::value_type[16] ));
552}
553
554void CameraConfig::translateCameraOffset( osg::Matrix::value_type x, osg::Matrix::value_type y, osg::Matrix::value_type z )
555{
556  osg::Matrix m;
557    m.invert(osg::Matrix::translate( x,y,z));
558    m = m * osg::Matrix(_offset_matrix);
559    memcpy( _offset_matrix, m.ptr(), sizeof( osg::Matrix::value_type[16] ));
560}
561
562void CameraConfig::scaleCameraOffset( osg::Matrix::value_type x, osg::Matrix::value_type y, osg::Matrix::value_type z )
563{
564  osg::Matrix m = osg::Matrix::scale( x,y,z) * osg::Matrix(_offset_matrix);
565  memcpy( _offset_matrix, m.ptr(), sizeof( osg::Matrix::value_type[16] ));
566}
567
568// Order of precedence:
569//
570std::string CameraConfig::findFile( std::string filename )
571{
572    if (filename.empty()) return filename;
573
574    std::string path;
575    // Check env var
576    char *ptr = getenv( "PRODUCER_CONFIG_FILE_PATH");
577    if( ptr != NULL )
578    {
579         path = std::string(ptr) + '/' + filename;
580        if( osgDB::fileExists(path))
581            return path;
582    }
583
584    // Check standard location(s)
585    //path.clear();
586    path = std::string( "/usr/local/share/Producer/Config/") + filename;
587    if( osgDB::fileExists(path) )
588        return path;
589
590    //path.clear();
591    path = std::string( "/usr/share/Producer/Config/") + filename;
592    if( osgDB::fileExists(path) )
593        return path;
594
595    // Check local directory
596    if(osgDB::fileExists(filename))
597        return filename;
598
599    // Fail
600    return std::string();
601}
602
603bool CameraConfig::defaultConfig()
604{
605    if( getNumberOfCameras() != 0 ) return false;
606
607    char *env = getenv( "PRODUCER_CAMERA_CONFIG_FILE" );
608
609    // Backwards compatibility
610    if( env == NULL )
611        env = getenv( "PRODUCER_CONFIG_FILE" );
612
613    if( env != NULL )
614    {
615        std::string file = findFile(env);
616        return parseFile( file.c_str() );
617    }
618
619    unsigned int numScreens =  getNumberOfScreens();
620    if( numScreens == 0 )
621        return false;
622
623    float xshear = float(numScreens-1);
624    float yshear = 0.0;
625    float input_xMin = -1.0f;
626    float input_yMin = -1.0f;
627    float input_width = 2.0f/float(numScreens);
628    float input_height = 2.0f;
629
630    // use an InputArea if there is more than one screen.
631    InputArea *ia = (numScreens>1) ? new InputArea : 0;
632    setInputArea(ia);
633
634    for( unsigned int i = 0; i < numScreens; i++ )
635    {
636      std::stringstream sstr; sstr<<"Screen"<<i;
637      std::string name = sstr.str();
638      std::pair<std::map<std::string, osg::ref_ptr<Camera> >::iterator,bool> res =
639      _camera_map.insert(std::pair<std::string, osg::ref_ptr<Camera> >(name, new Camera));
640
641      ((res.first)->second)->getRenderSurface()->setScreenNum( i );
642      ((res.first)->second)->setLensShear( xshear, yshear );
643
644       RenderSurface *rs = ((res.first)->second)->getRenderSurface();
645       rs->setWindowName( name );
646       if (ia)
647       {
648           rs->setInputRectangle( RenderSurface::InputRectangle(input_xMin, input_xMin+input_width, input_yMin, input_yMin+input_height) );
649           input_xMin += input_width;
650           ia->addRenderSurface(rs);
651       }
652
653       _render_surface_map.insert(std::pair<std::string,
654            osg::ref_ptr<RenderSurface> >( rs->getWindowName(), rs ));
655
656      xshear -= 2.0;
657    }
658
659    _threadModelDirective = CameraGroup::getDefaultThreadModel();
660
661    return true;
662}
Note: See TracBrowser for help on using the browser.