OpenWalnut  1.4.0
WGEViewer.h
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 #ifndef WGEVIEWER_H
26 #define WGEVIEWER_H
27 
28 #include <string>
29 
30 #ifndef Q_MOC_RUN
31 #include <boost/shared_ptr.hpp>
32 #endif
33 #ifndef Q_MOC_RUN
34 #include <boost/enable_shared_from_this.hpp>
35 #endif
36 
37 #include <osg/Node>
38 #include <osg/Version>
39 #include <osgViewer/View>
40 #include <osgViewer/Viewer>
41 
42 // OSG interface changed in 2.9.7, to make it compile also with those versions we do this:
43 // OSG_MIN_VERSION_REQUIRED(2, 9, 8) macro is not available in e.g. OSG 2.8.1, hence we use the old way
44 #if ( ( OPENSCENEGRAPH_MAJOR_VERSION > 2 ) || ( OPENSCENEGRAPH_MAJOR_VERSION == 2 && ( OPENSCENEGRAPH_MINOR_VERSION > 9 || \
45  ( OPENSCENEGRAPH_MINOR_VERSION == 9 && OPENSCENEGRAPH_PATCH_VERSION >= 8 ) ) ) )
46  #include <osgGA/CameraManipulator>
47  namespace osgGA
48  {
49  typedef CameraManipulator MatrixManipulator;
50  }
51 #else
52  #include <osgGA/MatrixManipulator>
53 #endif
54 
55 #include "../common/WColor.h"
56 #include "../common/WFlag.h"
57 
58 #include "WGECamera.h"
59 #include "WGEGraphicsWindow.h"
60 #include "WGraphicsEngineMode.h"
61 class WGEGroupNode;
62 #include "WGEScreenCapture.h"
63 class WPickHandler;
64 #include "animation/WGEAnimationManipulator.h"
65 
66 #include "WGEViewerEffectHorizon.h"
67 #include "WGEViewerEffectVignette.h"
68 #include "WGEViewerEffectImageOverlay.h"
69 
70 /**
71  * Class for managing one view to the scene. This includes viewport, camera and graphics context.
72  * It is, besides WGraphicsEngine, the ONLY entry point for each widget for accessing the graphics engine.
73  * \ingroup ge
74  */
76  public boost::enable_shared_from_this< WGEViewer >
77 {
78 public:
79  /**
80  * Convenience typedef
81  */
82  typedef boost::shared_ptr< WGEViewer > SPtr;
83 
84  /**
85  * Convenience typedef
86  */
87  typedef boost::shared_ptr< const WGEViewer > ConstSPtr;
88 
89  /**
90  * Default constructor.
91  *
92  * \param name the name of the viewer
93  * \param wdata the WindowData instance for the widget to use as render widget
94  * \param x X coordinate of widget where to create the context.
95  * \param y Y coordinate of widget where to create the context.
96  * \param width Width of the widget.
97  * \param height Height of the Widget.
98  * \param projectionMode Projection mode of the viewer.
99  * \exception WGEInitFailed thrown if initialization of graphics context or graphics window has failed.
100  */
101  WGEViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y, int width, int height,
102  WGECamera::ProjectionMode projectionMode = WGECamera::ORTHOGRAPHIC );
103 
104  /**
105  * Destructor.
106  */
107  virtual ~WGEViewer();
108 
109  /**
110  * Repaints the contents. Mac only.
111  */
112  virtual void paint();
113 
114  /**
115  * Updates size information. Also updates camera.
116  *
117  * \param width new width.
118  * \param height new height.
119  */
120  virtual void resize( int width, int height );
121 
122  /**
123  * Close the viewer, but wait for the rendering thread to finish.
124  */
125  virtual void close();
126 
127  /**
128  * Getter for OpenSceneGraph View instance.
129  *
130  * \return the OSG Viewer instance.
131  */
132 #ifdef WGEMODE_SINGLETHREADED
133  osg::ref_ptr<osgViewer::Viewer> getView();
134 #else
135  osg::ref_ptr<osgViewer::View> getView();
136 #endif
137 
138  /**
139  * Resets the view using the installed manipulator.
140  */
141  void reset();
142 
143  /**
144  * Sets the camera manipulator to use.
145  *
146  * \param manipulator the manipulator to use.
147  */
148  void setCameraManipulator( osg::ref_ptr<osgGA::MatrixManipulator> manipulator );
149 
150  /**
151  * Returns current active camera manipulator
152  *
153  * \return the active camera manipulator.
154  */
155  osg::ref_ptr<osgGA::MatrixManipulator> getCameraManipulator();
156 
157  /**
158  * Sets the current camera.
159  *
160  * \param camera the OSG camera instance.
161  */
162  void setCamera( osg::ref_ptr< WGECamera > camera );
163 
164  /**
165  * Returns the camera currently in use.
166  *
167  * \return the camera currently in use.
168  */
169  osg::ref_ptr< WGECamera > getCamera();
170 
171  /**
172  * Sets the scene graph node to be used for rendering.
173  *
174  * \param node part of the scene graph
175  */
176  void setScene( osg::ref_ptr< WGEGroupNode > node );
177 
178  /**
179  * Returns the currently set OSG node.
180  *
181  * \return the node.
182  */
183  osg::ref_ptr< WGEGroupNode > getScene();
184 
185  /**
186  * Returns the name of the viewer.
187  *
188  * \return the name
189  */
190  std::string getName() const;
191 
192  /**
193  * Determine the color of the viewer's background.
194  *
195  * \note This is only useful when the background effect is disabled.
196  *
197  * \param bgColor the new background color
198  */
199  void setBgColor( const WColor& bgColor );
200 
201  /**
202  * Returns the current default background color. This color is only visible if no camera effect overrides it.
203  *
204  * \return The color.
205  */
206  WColor getBgColor() const;
207 
208  /**
209  * Getter for the pick handler
210  *
211  * \return the pick handler
212  */
213  osg::ref_ptr< WPickHandler > getPickHandler();
214 
215  /**
216  * Queries the OpenGL vendor info.
217  *
218  * \return Vendor string.
219  */
220  std::string getOpenGLVendor() const;
221 
222  /**
223  * Returns the flag which denotes whether a frame was rendered.
224  *
225  * \return the flag.
226  */
228 
229  /**
230  * Returns the main cameras screen capture callback.
231  *
232  * \return the screen capture callback.
233  */
235 
236  /**
237  * The (de-)activates the animation mode. In animation mode, a special camera manipulator is used instead of the currently set. This
238  * manipulator can then play some animation path in realtime, frame-rate independent or in frame-per-frame mode which is useful if combined
239  * with the getScreenCapture() record function.
240  *
241  * If animation mode is turned off again, the previously set manipulator / camera setting is restored.
242  *
243  * \note do not modify camera or camera manipulator manually while in animation mode.
244  *
245  * \param on true to turn on.
246  *
247  * \return the animation manipulator. This, and only this should be used to provide the animation.
248  */
250 
251  /**
252  * Checks if the viewer is in animation mode.
253  *
254  * \return true if in animation mode
255  */
256  bool isAnimationMode() const;
257 
258  /**
259  * Return the background render effect for modification.
260  *
261  * \return the effect
262  */
264 
265  /**
266  * Return the overlay render effect for modification.
267  *
268  * \return the effect
269  */
271 
272  /**
273  * Return the vignette render effect for modification.
274  *
275  * \return the effect
276  */
278 
279  /**
280  * Return the background render effect for modification.
281  *
282  * \return the effect
283  */
285 
286  /**
287  * Return the overlay render effect for modification.
288  *
289  * \return the effect
290  */
292 
293  /**
294  * Return the vignette render effect for modification.
295  *
296  * \return the effect
297  */
299 
300  /**
301  * Activate viewer effects by default. If the user has deactivated them, this method does not cause any change.
302  *
303  * \param activeByDefault if true all effects are active by default
304  */
305  void setEffectsActiveDefault( bool activeByDefault = true );
306 
307  /**
308  * Return a pointer to the properties object of the view.
309  *
310  * \return the properties.
311  */
313 
314 protected:
315  /**
316  * The OpenSceneGraph view used in this (Composite)Viewer.
317  */
318 #ifdef WGEMODE_SINGLETHREADED
319  osg::ref_ptr< osgViewer::Viewer > m_View;
320 #else
321  osg::ref_ptr< osgViewer::View > m_View;
322 #endif
323 
324  /**
325  * The name of the viewer.
326  */
327  std::string m_name;
328 
329  /**
330  * Pointer to the pick handler of the viewer.
331  */
332  osg::ref_ptr<WPickHandler> m_pickHandler;
333 
334  /**
335  * reference to the scene which is displayed by viewer
336  */
337  osg::ref_ptr< WGEGroupNode > m_scene;
338 
339  /**
340  * Keep the currently set scene node. Unlike m_scene, it is the user set node.
341  */
342  osg::ref_ptr< WGEGroupNode > m_sceneMainNode;
343 
344  /**
345  * This flag is true and notifies after the first rendered frame.
346  */
348 
349  /**
350  * Small class used for querying glGet info during rendering.
351  */
352  class QueryCallback: public WGECamera::DrawCallback
353  {
354  public:
355  /**
356  * Constructor. Automatically de-registers from camera after one run.
357  *
358  * \param camera the cam to which this was registered
359  * \param run notifies the flag when run.
360  */
361  QueryCallback( osg::ref_ptr< WGECamera> camera, WBoolFlag::SPtr run );
362 
363  /**
364  * Destructor.
365  */
366  virtual ~QueryCallback();
367 
368  /**
369  * Query operator.
370  *
371  * \param renderInfo render info object
372  */
373  virtual void operator()( osg::RenderInfo& renderInfo ) const; // NOLINT - this is OSG API
374 
375  /**
376  * Returns the queried vendor string.
377  *
378  * \return the vendor
379  */
380  std::string getVendor() const;
381 
382  protected:
383  /**
384  * The vendor string.
385  */
386  mutable std::string m_vendor;
387 
388  /**
389  * True if callback was run once.
390  */
392 
393  /**
394  * The camera to which this was connected.
395  */
396  osg::ref_ptr< WGECamera > m_camera;
397  };
398 
399  /**
400  * The callback used for querying OpenGL features
401  */
402  osg::ref_ptr< QueryCallback > m_queryCallback;
403 
404 
405  /**
406  * The screen capture callback.
407  */
409 
410  /**
411  * True -> animation mode on.
412  */
414 
415  /**
416  * The manipulator that was set before entering animation mode. Null if not in animation mode.
417  */
418  osg::ref_ptr<osgGA::MatrixManipulator> m_animationModeManipulatorBackup;
419 
420  /**
421  * Horizon effect.
422  */
424 
425  /**
426  * Vignette effect.
427  */
429 
430  /**
431  * Image overlay effect.
432  */
434 
435  /**
436  * The property object for the view.
437  */
439 private:
440  /**
441  * The default clear color (bg color).
442  */
443  WPropColor m_bgColor;
444 
445  /**
446  * The switch to enable the throw- functionality of some OSG manipulators.
447  */
448  WPropBool m_throwing;
449 
450  /**
451  * Update the default clear color (bg color). Called by the m_bgColor property.
452  */
453  void updateBgColor();
454 
455  /**
456  * Update throw setting of the manipulator (if supported).
457  */
458  void updateThrowing();
459 };
460 
461 #endif // WGEVIEWER_H
WGEViewerEffectVignette::SPtr m_effectVignette
Vignette effect.
Definition: WGEViewer.h:428
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
WBoolFlag::SPtr m_rendered
This flag is true and notifies after the first rendered frame.
Definition: WGEViewer.h:347
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:408
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
boost::shared_ptr< const WGEViewer > ConstSPtr
Convenience typedef.
Definition: WGEViewer.h:87
osg::ref_ptr< osgGA::MatrixManipulator > m_animationModeManipulatorBackup
The manipulator that was set before entering animation mode.
Definition: WGEViewer.h:418
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:50
ProjectionMode
List of possible camera modes.
Definition: WGECamera.h:43
osg::ref_ptr< WGECamera > m_camera
The camera to which this was connected.
Definition: WGEViewer.h:396
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:448
std::string m_vendor
The vendor string.
Definition: WGEViewer.h:386
osg::ref_ptr< WGEAnimationManipulator > RefPtr
Convenience typedef.
Class to handle events with a pick.
Definition: WPickHandler.h:45
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:402
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
osg::ref_ptr< WGEGroupNode > m_scene
reference to the scene which is displayed by viewer
Definition: WGEViewer.h:337
osg::ref_ptr< osgGA::MatrixManipulator > getCameraManipulator()
Returns current active camera manipulator.
Definition: WGEViewer.cpp:173
Class to wrap around the osg Group node and providing a thread safe add/removal mechanism.
Definition: WGEGroupNode.h:48
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
WBoolFlag::SPtr m_run
True if callback was run once.
Definition: WGEViewer.h:391
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:342
bool m_inAnimationMode
True -> animation mode on.
Definition: WGEViewer.h:413
Class for managing one view to the scene.
Definition: WGEViewer.h:75
osg::ref_ptr< WPickHandler > m_pickHandler
Pointer to the pick handler of the viewer.
Definition: WGEViewer.h:332
std::string m_name
The name of the viewer.
Definition: WGEViewer.h:327
Small class used for querying glGet info during rendering.
Definition: WGEViewer.h:352
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
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
WGEViewerEffectImageOverlay::SPtr m_effectImageOverlay
Image overlay effect.
Definition: WGEViewer.h:433
virtual ~QueryCallback()
Destructor.
Definition: WGEViewer.cpp:298
WProperties::SPtr m_properties
The property object for the view.
Definition: WGEViewer.h:438
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:443
boost::shared_ptr< WGEViewer > SPtr
Convenience typedef.
Definition: WGEViewer.h:82
WGEViewerEffectHorizon::SPtr m_effectHorizon
Horizon effect.
Definition: WGEViewer.h:423
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:321
bool isAnimationMode() const
Checks if the viewer is in animation mode.
Definition: WGEViewer.cpp:349