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