OpenWalnut  1.3.1
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 "WGEGroupNode.h"
47 #include "WGENoOpManipulator.h"
48 #include "WGEZoomTrackballManipulator.h"
49 #include "WPickHandler.h"
50 #include "../common/WConditionOneShot.h"
51 
52 #include "../common/WThreadedRunner.h"
53 
54 #include "WGEViewer.h"
55 
56 WGEViewer::WGEViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y,
57  int width, int height, WGECamera::ProjectionMode projectionMode )
58  : WGEGraphicsWindow( wdata, x, y, width, height ),
60  m_name( name ),
61  m_rendered( WBoolFlag::SPtr( new WBoolFlag( new WConditionOneShot(), false ) ) ),
62  m_screenCapture( new WGEScreenCapture() ),
63  m_inAnimationMode( false )
64 {
65  try
66  {
67 #ifdef WGEMODE_MULTITHREADED
68  m_View = osg::ref_ptr<osgViewer::View>( new osgViewer::View );
69 #else
70  // on mac, this is a viewer!
71  m_View = osg::ref_ptr<osgViewer::Viewer>( new osgViewer::Viewer );
72 #endif
73 
74  m_View->setCamera( new WGECamera( width, height, projectionMode ) );
75  m_queryCallback = new QueryCallback( m_View->getCamera(), m_rendered );
76  m_View->getCamera()->setInitialDrawCallback( m_queryCallback );
77  m_View->getCamera()->setFinalDrawCallback( m_screenCapture );
78 
79 #ifdef WGEMODE_MULTITHREADED
80  m_View->getCamera()->setGraphicsContext( m_GraphicsContext.get() );
81 #else
82  m_View->getCamera()->setGraphicsContext( m_GraphicsWindow.get() );
83 #endif
84 
85  switch( projectionMode )
86  {
87  case( WGECamera::ORTHOGRAPHIC ):
88  m_pickHandler = new WPickHandler( name );
89  m_View->addEventHandler( m_pickHandler );
90  if( name != std::string( "Main View" ) )
91  break;
92  case( WGECamera::PERSPECTIVE ):
93  // camera manipulator
94  m_View->setCameraManipulator( new WGEZoomTrackballManipulator() );
95 
96  m_View->setLightingMode( osg::View::HEADLIGHT ); // this is the default anyway
97 
98  break;
99  case( WGECamera::TWO_D ):
100  // no manipulators nor gui handlers
101  break;
102  case( WGECamera::TWO_D_UNIT ):
103  // use no-op handler by default
104  m_View->setCameraManipulator( new WGENoOpManipulator() );
105  break;
106  default:
107  throw WGEInitFailed( std::string( "Unknown projection mode" ) );
108  }
109 
110  // add the stats handler
111  m_View->addEventHandler( new osgViewer::StatsHandler );
112  }
113  catch( ... )
114  {
115  throw WGEInitFailed( std::string( "Initialization of WGEViewer failed" ) );
116  }
117 }
118 
120 {
121  // cleanup
122  close();
123 }
124 
125 #ifdef WGEMODE_SINGLETHREADED
126 osg::ref_ptr<osgViewer::Viewer>
127 #else
128 osg::ref_ptr<osgViewer::View>
129 #endif
131 {
132  return m_View;
133 }
134 
135 void WGEViewer::setCameraManipulator( osg::ref_ptr<osgGA::MatrixManipulator> manipulator )
136 {
137  if( !m_inAnimationMode )
138  {
139  m_View->setCameraManipulator( manipulator );
140  }
141 }
142 
143 osg::ref_ptr<osgGA::MatrixManipulator> WGEViewer::getCameraManipulator()
144 {
145  return m_View->getCameraManipulator();
146 }
147 
148 void WGEViewer::setCamera( osg::ref_ptr<osg::Camera> camera )
149 {
150  m_View->setCamera( camera );
151  // redraw request?? No since it redraws permanently and uses the new settings
152 }
153 
154 osg::ref_ptr<osg::Camera> WGEViewer::getCamera()
155 {
156  return m_View->getCamera();
157 }
158 
159 void WGEViewer::setScene( osg::ref_ptr< WGEGroupNode > node )
160 {
161  m_View->setSceneData( node );
162  m_scene = node;
163 }
164 
165 osg::ref_ptr< WGEGroupNode > WGEViewer::getScene()
166 {
167  return m_scene;
168 }
169 
170 void WGEViewer::setBgColor( const WColor& bgColor )
171 {
172  m_View->getCamera()->setClearColor( bgColor );
173 }
174 
176 {
177 #ifdef WGEMODE_SINGLETHREADED
178  m_View->frame();
179 #endif
180 }
181 
182 void WGEViewer::resize( int width, int height )
183 {
184  m_View->getEventQueue()->windowResize( 0, 0, width, height );
185 
186  WGEGraphicsWindow::resize( width, height );
187 
188  // also update the camera
189  m_View->getCamera()->setViewport( 0, 0, width, height );
190  WGECamera* camera = dynamic_cast< WGECamera* >( m_View->getCamera() );
191  if( camera )
192  {
193  camera->resize();
194  }
195 }
196 
198 {
199  // forward close event
201 }
202 
203 std::string WGEViewer::getName() const
204 {
205  return m_name;
206 }
207 
208 osg::ref_ptr< WPickHandler > WGEViewer::getPickHandler()
209 {
210  return m_pickHandler;
211 }
212 
214 {
215  m_View->home();
216 }
217 
219 {
220  return m_screenCapture;
221 }
222 
223 std::string WGEViewer::getOpenGLVendor() const
224 {
225  return m_queryCallback->getVendor();
226 }
227 
229 {
230  return m_rendered;
231 }
232 
233 WGEViewer::QueryCallback::QueryCallback( osg::ref_ptr<osg::Camera> camera, WBoolFlag::SPtr run ):
234  m_vendor( "" ),
235  m_run( run ),
236  m_camera( camera )
237 {
238  // init
239 }
240 
242 {
243  // cleanup
244 }
245 
246 void WGEViewer::QueryCallback::operator()( osg::RenderInfo& /* renderInfo */ ) const
247 {
248  const GLubyte* vendor = glGetString( GL_VENDOR );
249  m_vendor = reinterpret_cast< const char* >( vendor );
250 
251  // job done. De-register.
252  m_camera->setInitialDrawCallback( NULL );
253  m_run->set( true );
254 }
255 
257 {
258  return m_vendor;
259 }
260 
262 {
263  if( m_inAnimationMode && !on ) // turn off mode
264  {
265  m_inAnimationMode = false;
266 
267  // restore old manipulator
268  m_View->setCameraManipulator( m_animationModeManipulatorBackup );
269  return NULL;
270  }
271  else if( !m_inAnimationMode && on ) // turn on
272  {
273  m_inAnimationMode = true;
274 
275  // backup
277 
278  // create animation manipulator
280  m_View->setCameraManipulator( anim );
281  return anim;
282  }
283  else if( m_inAnimationMode ) // already on
284  {
285  return dynamic_cast< WGEAnimationManipulator* >( getCameraManipulator().get() );
286  }
287 
288  // else: do nothing
289  return NULL;
290 }
291 
293 {
294  return m_inAnimationMode;
295 }
296