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