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