OpenWalnut  1.5.0dev
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 <iostream>
26 #include <memory>
27 #include <string>
28 
29 #include <osg/Camera>
30 #include <osg/Geode>
31 #include <osg/ShapeDrawable>
32 #include <osgDB/ReadFile>
33 #include <osgGA/AnimationPathManipulator>
34 #include <osgGA/DriveManipulator>
35 #include <osgGA/FlightManipulator>
36 #include <osgGA/KeySwitchMatrixManipulator>
37 #include <osgGA/StateSetManipulator>
38 #include <osgGA/TerrainManipulator>
39 #include <osgGA/UFOManipulator>
40 #include <osgViewer/View>
41 #include <osgViewer/ViewerEventHandlers>
42 
43 #include "WGE2DManipulator.h"
44 #include "WGEGroupNode.h"
45 #include "WGENoOpManipulator.h"
46 #include "WGEViewer.h"
47 #include "WGEZoomTrackballManipulator.h"
48 #include "WGraphicsEngine.h"
49 #include "WMouseLocationHandler.h"
50 #include "WPickHandler.h"
51 #include "core/common/WConditionOneShot.h"
52 #include "core/common/WLogger.h"
53 #include "core/common/WThreadedRunner.h"
54 #include "exceptions/WGEInitFailed.h"
55 
56 WGEViewer::WGEViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y, int width, int height,
57  WGECamera::ProjectionMode projectionMode ):
58  WGEGraphicsWindow( wdata, x, y, width, height ),
59  std::enable_shared_from_this< WGEViewer >(),
60  m_name( name ),
61  m_scene( new WGEGroupNode ),
62  m_rendered( WBoolFlag::SPtr( new WBoolFlag( new WConditionOneShot(), false ) ) ),
63  m_screenCapture( new WGEScreenCapture() ),
64  m_inAnimationMode( false ),
65  m_effectHorizon( new WGEViewerEffectHorizon() ),
66  m_effectVignette( new WGEViewerEffectVignette() ),
67  m_effectImageOverlay( new WGEViewerEffectImageOverlay() ),
68  m_paused( false )
69 {
70  try
71  {
72  m_View = osg::ref_ptr<osgViewer::Viewer>( new osgViewer::Viewer );
73 
74  osg::ref_ptr< WGECamera > cam( new WGECamera( width, height, projectionMode ) );
75  m_View->setCamera( cam );
77  m_View->getCamera()->setInitialDrawCallback( m_queryCallback );
78  m_View->getCamera()->setFinalDrawCallback( m_screenCapture );
79 
80  m_View->getCamera()->setGraphicsContext( m_GraphicsWindow.get() );
81 
82  m_View->getCamera()->getGraphicsContext()->getState()->setUseModelViewAndProjectionUniforms( true );
83 
85  m_View->addEventHandler( m_mouseLocationHandler );
86 
87  switch( projectionMode )
88  {
89  case( WGECamera::ORTHOGRAPHIC ):
90  m_pickHandler = new WPickHandler( name );
91  m_View->addEventHandler( m_pickHandler );
92  if( name != std::string( "Main View" ) )
93  break;
94  /* FALLTHRU */
95  case( WGECamera::PERSPECTIVE ):
96  // camera manipulator
97  m_View->setCameraManipulator( new WGEZoomTrackballManipulator() );
98 
99  m_View->setLightingMode( osg::View::HEADLIGHT ); // this is the default anyway
100 
101  break;
102  case( WGECamera::TWO_D ):
103  // no manipulators nor gui handlers
104  break;
105  case( WGECamera::TWO_D_UNIT ):
106  // use no-op handler by default
107  m_View->setCameraManipulator( new WGENoOpManipulator() );
108  break;
109  default:
110  throw WGEInitFailed( std::string( "Unknown projection mode" ) );
111  }
112 
113  // add the stats handler
114  m_View->addEventHandler( new osgViewer::StatsHandler );
115 
116  // Properties of the view. Collects props of the effects and similar
117  m_properties = std::shared_ptr< WProperties >( new WProperties( "Viewer Properties", "The view's properties" ) );
118  m_bgColor = m_properties->addProperty( "Background Color", "Default background color if not overwritten by a camera effect.",
119  defaultColor::WHITE,
120  boost::bind( &WGEViewer::updateBgColor, this ) );
121  m_throwing = m_properties->addProperty( "Throwing", "If checked, you can grab the scene and throw it. It will keep the rotation impulse.",
122  false,
123  boost::bind( &WGEViewer::updateThrowing, this ) );
124 
125  WPropGroup effects = m_properties->addPropertyGroup( "Camera Effects", "Several effects that to not depend on any scene content." );
126  effects->addProperty( m_effectHorizon->getProperties() );
127  effects->addProperty( m_effectVignette->getProperties() );
128  effects->addProperty( m_effectImageOverlay->getProperties() );
129 
130  // scene node
131  m_View->setSceneData( m_scene );
132  // add effects to it:
133  m_scene->insert( m_effectVignette );
134  m_scene->insert( m_effectImageOverlay );
135  m_scene->insert( m_effectHorizon );
136 
137  // apply the above default
138  updateThrowing();
139  updateBgColor();
140  }
141  catch( ... )
142  {
143  throw WGEInitFailed( std::string( "Initialization of WGEViewer failed" ) );
144  }
145 }
146 
148 {
149  // cleanup
150  close();
151 }
152 
153 osg::ref_ptr<osgViewer::Viewer> WGEViewer::getView()
154 {
155  return m_View;
156 }
157 
158 void WGEViewer::setCameraManipulator( osg::ref_ptr<osgGA::MatrixManipulator> manipulator )
159 {
160  if( !m_inAnimationMode )
161  {
162  m_View->setCameraManipulator( manipulator );
163  }
164 }
165 
166 osg::ref_ptr<osgGA::MatrixManipulator> WGEViewer::getCameraManipulator()
167 {
168  return m_View->getCameraManipulator();
169 }
170 
171 void WGEViewer::setCamera( osg::ref_ptr<WGECamera> camera )
172 {
173  m_View->setCamera( camera );
174  // redraw request?? No since it redraws permanently and uses the new settings
175 }
176 
177 osg::ref_ptr<WGECamera> WGEViewer::getCamera()
178 {
179  return dynamic_cast< WGECamera* >( m_View->getCamera() );
180 }
181 
182 void WGEViewer::setScene( osg::ref_ptr< WGEGroupNode > node )
183 {
184  m_sceneMainNode = node;
185 
186  m_effectImageOverlay->setReferenceViewer( shared_from_this() );
187 
188  m_scene->clear();
189  m_scene->insert( m_sceneMainNode );
190  // add effects to scene node. We cleared it earlier.
191  m_scene->insert( m_effectVignette );
192  m_scene->insert( m_effectImageOverlay );
193  m_scene->insert( m_effectHorizon );
194 }
195 
196 osg::ref_ptr< WGEGroupNode > WGEViewer::getScene()
197 {
198  return m_sceneMainNode;
199 }
200 
202 {
203  WGEZoomTrackballManipulator* manipulator = dynamic_cast< WGEZoomTrackballManipulator* >( getCameraManipulator().get() );
204  if( manipulator )
205  {
206  manipulator->setThrow( m_throwing->get() );
207  }
208 }
209 
211 {
212  m_View->getCamera()->setClearColor( m_bgColor->get() );
213 }
214 
215 void WGEViewer::setBgColor( const WColor& bgColor )
216 {
217  m_bgColor->set( bgColor );
218 }
219 
220 WColor WGEViewer::getBgColor() const
221 {
222  return m_bgColor->get();
223 }
224 
226 {
227  m_View->frame();
228 }
229 
230 void WGEViewer::resize( int width, int height )
231 {
232  m_View->getEventQueue()->windowResize( 0, 0, width, height );
233 
234  WGEGraphicsWindow::resize( width, height );
235 
236  // also update the camera
237  m_View->getCamera()->setViewport( 0, 0, width, height );
238  WGECamera* camera = dynamic_cast< WGECamera* >( m_View->getCamera() );
239  if( camera )
240  {
241  camera->resize();
242  }
243 }
244 
246 {
247  // delete/unset all the objects we sponsored a "shared_from_this" pointer to ensure the viewer gets deleted after close
248  m_effectImageOverlay->setReferenceViewer( WGEViewer::SPtr() );
249 
250  // forward close event
252 }
253 
254 std::string WGEViewer::getName() const
255 {
256  return m_name;
257 }
258 
259 osg::ref_ptr< WPickHandler > WGEViewer::getPickHandler()
260 {
261  return m_pickHandler;
262 }
263 
264 osg::ref_ptr< WMouseLocationHandler > WGEViewer::getMouseLocationHandler()
265 {
266  return m_mouseLocationHandler;
267 }
268 
270 {
271  m_View->home();
272 }
273 
275 {
276  return m_screenCapture;
277 }
278 
279 std::string WGEViewer::getOpenGLVendor() const
280 {
281  return m_queryCallback->getVendor();
282 }
283 
285 {
286  return m_rendered;
287 }
288 
289 WGEViewer::QueryCallback::QueryCallback( osg::ref_ptr<WGECamera> camera, WBoolFlag::SPtr run ):
290  m_vendor( "" ),
291  m_run( run ),
292  m_camera( camera )
293 {
294  // init
295 }
296 
298 {
299  // cleanup
300 }
301 
302 void WGEViewer::QueryCallback::operator()( osg::RenderInfo& /* renderInfo */ ) const
303 {
304  const GLubyte* vendor = glGetString( GL_VENDOR );
305  m_vendor = reinterpret_cast< const char* >( vendor );
306 
307  // job done. De-register.
308  m_camera->setInitialDrawCallback( NULL );
309  m_run->set( true );
310 }
311 
313 {
314  return m_vendor;
315 }
316 
318 {
319  if( m_inAnimationMode && !on ) // turn off mode
320  {
321  m_inAnimationMode = false;
322 
323  // restore old manipulator
324  m_View->setCameraManipulator( m_animationModeManipulatorBackup );
325  return NULL;
326  }
327  else if( !m_inAnimationMode && on ) // turn on
328  {
329  m_inAnimationMode = true;
330 
331  // backup
333 
334  // create animation manipulator
336  m_View->setCameraManipulator( anim );
337  return anim;
338  }
339  else if( m_inAnimationMode ) // already on
340  {
341  return dynamic_cast< WGEAnimationManipulator* >( getCameraManipulator().get() );
342  }
343 
344  // else: do nothing
345  return NULL;
346 }
347 
349 {
350  return m_inAnimationMode;
351 }
352 
354 {
355  return m_effectHorizon;
356 }
357 
359 {
360  return m_effectImageOverlay;
361 }
362 
364 {
365  return m_effectVignette;
366 }
367 
369 {
370  return m_effectHorizon;
371 }
372 
374 {
375  return m_effectImageOverlay;
376 }
377 
379 {
380  return m_effectVignette;
381 }
382 
384 {
385  return m_properties;
386 }
387 
388 void WGEViewer::setEffectsActiveDefault( bool activeByDefault )
389 {
390  getBackground()->setEnabledByDefault( activeByDefault );
391  getImageOverlay()->setEnabledByDefault( activeByDefault );
392  getVignette()->setEnabledByDefault( activeByDefault );
393 }
394 
395 void WGEViewer::setPaused( bool pause )
396 {
397  m_paused = pause;
398 }
399 
401 {
402  return m_paused;
403 }
404 
406 {
407  getView()->getScene()->getSceneData()->setNodeMask( visible * 0xFFFFFFFF );
408 }
409 
410 void WGEViewer::requestContinuousUpdate( bool continuous )
411 {
412  getView()->requestContinuousUpdate( continuous );
413 }
Implements a WCondition, but can be fired only ONCE.
std::shared_ptr< WFlag< bool > > SPtr
Convenience typedef for a std::shared_ptr.
Definition: WFlag.h:49
New OSG manipulator: AnimationManipulator.
osg::ref_ptr< WGEAnimationManipulator > RefPtr
Convenience typedef.
Class for wrapping around the OSG Camera class.
Definition: WGECamera.h:36
ProjectionMode
List of possible camera modes.
Definition: WGECamera.h:44
void resize()
Change camera parameters which should be changed on a resize.
Definition: WGECamera.cpp:114
Class managing a single graphics context and OSG GraphicsWindow.
virtual void close()
Initiates a close event for this viewer.
virtual void resize(int width, int height)
Updates size information.
osg::ref_ptr< osgViewer::GraphicsWindow > m_GraphicsWindow
OpenSceneGraph render window.
Class to wrap around the osg Group node and providing a thread safe add/removal mechanism.
Definition: WGEGroupNode.h:48
Exception thrown if initialization of the graphics engine fails.
Definition: WGEInitFailed.h:38
This is an OSG Manipulator implementation which does nothing.
This class is a screen recorder.
osg::ref_ptr< WGEScreenCapture > RefPtr
Convenience typedef.
osg::ref_ptr< const WGEViewerEffectHorizon > ConstSPtr
Convenience typedef for a std::shared_ptr< const WGEViewerEffectHorizon >.
osg::ref_ptr< WGEViewerEffectHorizon > SPtr
Convenience typedef for a std::shared_ptr< WGEViewerEffectHorizon >.
osg::ref_ptr< const WGEViewerEffectImageOverlay > ConstSPtr
Convenience typedef for a std::shared_ptr< const WGEViewerEffectImageOverlay >.
osg::ref_ptr< WGEViewerEffectImageOverlay > SPtr
Convenience typedef for a std::shared_ptr< WGEViewerEffectImageOverlay >.
osg::ref_ptr< const WGEViewerEffectVignette > ConstSPtr
Convenience typedef for a std::shared_ptr< const WGEViewerEffectVignette >.
osg::ref_ptr< WGEViewerEffectVignette > SPtr
Convenience typedef for a std::shared_ptr< WGEViewerEffectVignette >.
Small class used for querying glGet info during rendering.
Definition: WGEViewer.h:380
QueryCallback(osg::ref_ptr< WGECamera > camera, WBoolFlag::SPtr run)
Constructor.
Definition: WGEViewer.cpp:289
virtual void operator()(osg::RenderInfo &renderInfo) const
Query operator.
Definition: WGEViewer.cpp:302
std::string getVendor() const
Returns the queried vendor string.
Definition: WGEViewer.cpp:312
virtual ~QueryCallback()
Destructor.
Definition: WGEViewer.cpp:297
Class for managing one view to the scene.
Definition: WGEViewer.h:71
osg::ref_ptr< osgViewer::Viewer > m_View
The OpenSceneGraph view used in this (Composite)Viewer.
Definition: WGEViewer.h:344
WBoolFlag::SPtr isFrameRendered() const
Returns the flag which denotes whether a frame was rendered.
Definition: WGEViewer.cpp:284
osg::ref_ptr< osgGA::MatrixManipulator > getCameraManipulator()
Returns current active camera manipulator.
Definition: WGEViewer.cpp:166
void handleVisibilityChange(bool visible)
If the widget is not visible, we might be able to reduce CPU load.
Definition: WGEViewer.cpp:405
void reset()
Resets the view using the installed manipulator.
Definition: WGEViewer.cpp:269
WGEScreenCapture::RefPtr m_screenCapture
The screen capture callback.
Definition: WGEViewer.h:435
virtual ~WGEViewer()
Destructor.
Definition: WGEViewer.cpp:147
WGEViewer(std::string name, osg::ref_ptr< osg::Referenced > wdata, int x, int y, int width, int height, WGECamera::ProjectionMode projectionMode=WGECamera::ORTHOGRAPHIC)
Default constructor.
Definition: WGEViewer.cpp:56
std::string getName() const
Returns the name of the viewer.
Definition: WGEViewer.cpp:254
WColor getBgColor() const
Returns the current default background color.
Definition: WGEViewer.cpp:220
void setBgColor(const WColor &bgColor)
Determine the color of the viewer's background.
Definition: WGEViewer.cpp:215
virtual void resize(int width, int height)
Updates size information.
Definition: WGEViewer.cpp:230
void setEffectsActiveDefault(bool activeByDefault=true)
Activate viewer effects by default.
Definition: WGEViewer.cpp:388
WGEViewerEffectHorizon::SPtr m_effectHorizon
Horizon effect.
Definition: WGEViewer.h:450
osg::ref_ptr< WGECamera > getCamera()
Returns the camera currently in use.
Definition: WGEViewer.cpp:177
WPropBool m_throwing
The switch to enable the throw- functionality of some OSG manipulators.
Definition: WGEViewer.h:475
osg::ref_ptr< osgViewer::Viewer > getView()
Getter for OpenSceneGraph View instance.
Definition: WGEViewer.cpp:153
osg::ref_ptr< WPickHandler > getPickHandler()
Getter for the pick handler Warning: At the moment only the orthographic projection mode supports a p...
Definition: WGEViewer.cpp:259
bool isAnimationMode() const
Checks if the viewer is in animation mode.
Definition: WGEViewer.cpp:348
std::shared_ptr< WGEViewer > SPtr
Convenience typedef.
Definition: WGEViewer.h:76
WGEViewerEffectHorizon::SPtr getBackground()
Return the background render effect for modification.
Definition: WGEViewer.cpp:353
osg::ref_ptr< QueryCallback > m_queryCallback
The callback used for querying OpenGL features.
Definition: WGEViewer.h:429
WGEScreenCapture::RefPtr getScreenCapture() const
Returns the main cameras screen capture callback.
Definition: WGEViewer.cpp:274
osg::ref_ptr< WGEGroupNode > m_scene
reference to the scene which is displayed by viewer
Definition: WGEViewer.h:364
WGEViewerEffectImageOverlay::SPtr getImageOverlay()
Return the overlay render effect for modification.
Definition: WGEViewer.cpp:358
void updateThrowing()
Update throw setting of the manipulator (if supported).
Definition: WGEViewer.cpp:201
void setCameraManipulator(osg::ref_ptr< osgGA::MatrixManipulator > manipulator)
Sets the camera manipulator to use.
Definition: WGEViewer.cpp:158
void setPaused(bool pause=true)
Pause rendering.
Definition: WGEViewer.cpp:395
osg::ref_ptr< WPickHandler > m_pickHandler
Pointer to the pick handler of the viewer.
Definition: WGEViewer.h:354
void setScene(osg::ref_ptr< WGEGroupNode > node)
Sets the scene graph node to be used for rendering.
Definition: WGEViewer.cpp:182
void updateBgColor()
Update the default clear color (bg color).
Definition: WGEViewer.cpp:210
std::string getOpenGLVendor() const
Queries the OpenGL vendor info.
Definition: WGEViewer.cpp:279
WPropColor m_bgColor
The default clear color (bg color).
Definition: WGEViewer.h:470
void requestContinuousUpdate(bool continuous=true)
Update the view automatically (the default).
Definition: WGEViewer.cpp:410
osg::ref_ptr< WGEGroupNode > getScene()
Returns the currently set OSG node.
Definition: WGEViewer.cpp:196
osg::ref_ptr< WGEGroupNode > m_sceneMainNode
Keep the currently set scene node.
Definition: WGEViewer.h:369
WProperties::SPtr getProperties() const
Return a pointer to the properties object of the view.
Definition: WGEViewer.cpp:383
WGEViewerEffectVignette::SPtr getVignette()
Return the vignette render effect for modification.
Definition: WGEViewer.cpp:363
WGEAnimationManipulator::RefPtr animationMode(bool on=true)
The (de-)activates the animation mode.
Definition: WGEViewer.cpp:317
WGEViewerEffectVignette::SPtr m_effectVignette
Vignette effect.
Definition: WGEViewer.h:455
bool getPaused() const
Query whether the view is paused or not.
Definition: WGEViewer.cpp:400
WBoolFlag::SPtr m_rendered
This flag is true and notifies after the first rendered frame.
Definition: WGEViewer.h:374
osg::ref_ptr< WMouseLocationHandler > m_mouseLocationHandler
Pointer to the mouse location handler of the viewer.
Definition: WGEViewer.h:359
WGEViewerEffectImageOverlay::SPtr m_effectImageOverlay
Image overlay effect.
Definition: WGEViewer.h:460
osg::ref_ptr< osgGA::MatrixManipulator > m_animationModeManipulatorBackup
The manipulator that was set before entering animation mode.
Definition: WGEViewer.h:445
virtual void close()
Close the viewer, but wait for the rendering thread to finish.
Definition: WGEViewer.cpp:245
WProperties::SPtr m_properties
The property object for the view.
Definition: WGEViewer.h:465
osg::ref_ptr< WMouseLocationHandler > getMouseLocationHandler()
Getter for the mouse loection handler.
Definition: WGEViewer.cpp:264
bool m_inAnimationMode
True -> animation mode on.
Definition: WGEViewer.h:440
bool m_paused
Flag denoting whether the view is paused or not.
Definition: WGEViewer.h:490
void setCamera(osg::ref_ptr< WGECamera > camera)
Sets the current camera.
Definition: WGEViewer.cpp:171
std::string m_name
The name of the viewer.
Definition: WGEViewer.h:349
virtual void paint()
Repaints the contents.
Definition: WGEViewer.cpp:225
New OSG manipulator: TrackballManipulator with added mouse wheel zoom.
void setThrow(bool allowThrow=true)
En-/Disables throwing.
Class to handle providing information about pixel position of mouse.
Class to handle events with a pick.
Definition: WPickHandler.h:44
Class to manage properties of an object and to provide convenience methods for easy access and manipu...
std::shared_ptr< WPropertyGroup > SPtr
shared pointer to object of this type