OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WGEViewer.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <string>
26 #include <iostream>
27 
28 #include <osg/ShapeDrawable>
29 #include <osg/Geode>
30 #include <osg/Camera>
31 
32 #include <osgGA/FlightManipulator>
33 #include <osgGA/DriveManipulator>
34 #include <osgGA/UFOManipulator>
35 #include <osgGA/KeySwitchMatrixManipulator>
36 #include <osgGA/StateSetManipulator>
37 #include <osgGA/AnimationPathManipulator>
38 #include <osgGA/TerrainManipulator>
39 #include <osgViewer/ViewerEventHandlers>
40 #include <osgViewer/View>
41 
42 #include <osgDB/ReadFile>
43 
44 #include "exceptions/WGEInitFailed.h"
45 #include "WGE2DManipulator.h"
46 #include "WGENoOpManipulator.h"
47 #include "WGEZoomTrackballManipulator.h"
48 #include "WPickHandler.h"
49 #include "../common/WConditionOneShot.h"
50 
51 #include "WGEViewer.h"
52 
53 WGEViewer::WGEViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y,
54  int width, int height, WGECamera::ProjectionMode projectionMode )
55  : WGEGraphicsWindow( wdata, x, y, width, height ),
57  m_name( name ),
58  m_rendered( WBoolFlag::SPtr( new WBoolFlag( new WConditionOneShot(), false ) ) )
59 {
60  try
61  {
62 #ifndef __APPLE__
63  m_View = osg::ref_ptr<osgViewer::View>( new osgViewer::View );
64 #else
65  // on mac, this is a viewer!
66  m_View = osg::ref_ptr<osgViewer::Viewer>( new osgViewer::Viewer );
67 #endif
68 
69  m_View->setCamera( new WGECamera( width, height, projectionMode ) );
70  m_queryCallback = new QueryCallback( m_View->getCamera(), m_rendered );
71  m_View->getCamera()->setInitialDrawCallback( m_queryCallback );
72 
73 #ifndef __APPLE__
74  m_View->getCamera()->setGraphicsContext( m_GraphicsContext.get() );
75 #else
76  m_View->getCamera()->setGraphicsContext( m_GraphicsWindow.get() );
77 #endif
78 
79  switch( projectionMode )
80  {
81  case( WGECamera::ORTHOGRAPHIC ):
82  m_pickHandler = new WPickHandler( name );
83  m_View->addEventHandler( m_pickHandler );
84  if( name != std::string( "main" ) )
85  break;
86  case( WGECamera::PERSPECTIVE ):
87  // camera manipulator
88  m_View->setCameraManipulator( new WGEZoomTrackballManipulator() );
89 
90  m_View->setLightingMode( osg::View::HEADLIGHT ); // this is the default anyway
91 
92  break;
93  case( WGECamera::TWO_D ):
94  // no manipulators nor gui handlers
95  break;
96  case( WGECamera::TWO_D_UNIT ):
97  // use no-op handler by default
98  m_View->setCameraManipulator( new WGENoOpManipulator() );
99  break;
100  default:
101  throw WGEInitFailed( std::string( "Unknown projection mode" ) );
102  }
103 
104  // add the stats handler
105  m_View->addEventHandler( new osgViewer::StatsHandler );
106  }
107  catch( ... )
108  {
109  throw WGEInitFailed( std::string( "Initialization of WGEViewer failed" ) );
110  }
111 }
112 
114 {
115  // cleanup
116  close();
117 }
118 
119 #ifdef __APPLE__
120 osg::ref_ptr<osgViewer::Viewer>
121 #else
122 osg::ref_ptr<osgViewer::View>
123 #endif
125 {
126  return m_View;
127 }
128 
129 void WGEViewer::setCameraManipulator( osg::ref_ptr<osgGA::MatrixManipulator> manipulator )
130 {
131  m_View->setCameraManipulator( manipulator );
132  // redraw request?? no since it redraws permanently and uses the new settings
133 }
134 
135 osg::ref_ptr<osgGA::MatrixManipulator> WGEViewer::getCameraManipulator()
136 {
137  return m_View->getCameraManipulator();
138 }
139 
140 void WGEViewer::setCamera( osg::ref_ptr<osg::Camera> camera )
141 {
142  m_View->setCamera( camera );
143  // redraw request?? No since it redraws permanently and uses the new settings
144 }
145 
146 osg::ref_ptr<osg::Camera> WGEViewer::getCamera()
147 {
148  return m_View->getCamera();
149 }
150 
151 void WGEViewer::setScene( osg::ref_ptr< WGEGroupNode > node )
152 {
153  m_View->setSceneData( node );
154  m_scene = node;
155 }
156 
157 osg::ref_ptr< WGEGroupNode > WGEViewer::getScene()
158 {
159  return m_scene;
160 }
161 
162 void WGEViewer::setBgColor( const WColor& bgColor )
163 {
164  m_View->getCamera()->setClearColor( bgColor );
165 }
166 
168 {
169 #ifdef __APPLE__
170  m_View->frame();
171 #endif
172 }
173 
174 void WGEViewer::resize( int width, int height )
175 {
176  m_View->getEventQueue()->windowResize( 0, 0, width, height );
177 
178  WGEGraphicsWindow::resize( width, height );
179 
180  // also update the camera
181  m_View->getCamera()->setViewport( 0, 0, width, height );
182  WGECamera* camera = dynamic_cast< WGECamera* >( m_View->getCamera() );
183  if( camera )
184  {
185  camera->resize();
186  }
187 }
188 
190 {
191  // forward close event
193 }
194 
195 std::string WGEViewer::getName() const
196 {
197  return m_name;
198 }
199 
200 osg::ref_ptr< WPickHandler > WGEViewer::getPickHandler()
201 {
202  return m_pickHandler;
203 }
204 
206 {
207  m_View->home();
208 }
209 
210 std::string WGEViewer::getOpenGLVendor() const
211 {
212  return m_queryCallback->getVendor();
213 }
214 
216 {
217  return m_rendered;
218 }
219 
220 WGEViewer::QueryCallback::QueryCallback( osg::ref_ptr<osg::Camera> camera, WBoolFlag::SPtr run ):
221  m_vendor( "" ),
222  m_run( run ),
223  m_camera( camera )
224 {
225  // init
226 }
227 
229 {
230  // cleanup
231 }
232 
233 void WGEViewer::QueryCallback::operator()( osg::RenderInfo& /* renderInfo */ ) const
234 {
235  const GLubyte* vendor = glGetString( GL_VENDOR );
236  m_vendor = reinterpret_cast< const char* >( vendor );
237 
238  // job done. De-register.
239  m_camera->setInitialDrawCallback( NULL );
240  m_run->set( true );
241 }
242 
244 {
245  return m_vendor;
246 }
247