OpenWalnut  1.3.1
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 /**
63  * Class for managing one view to the scene. This includes viewport, camera and graphics context.
64  * It is, besides WGraphicsEngine, the ONLY entry point for each widget for accessing the graphics engine.
65  * \ingroup ge
66  */
68  public boost::enable_shared_from_this< WGEViewer >
69 {
70 public:
71  /**
72  * Convenience typedef
73  */
74  typedef boost::shared_ptr< WGEViewer > SPtr;
75 
76  /**
77  * Convenience typedef
78  */
79  typedef boost::shared_ptr< const WGEViewer > ConstSPtr;
80 
81  /**
82  * Default constructor.
83  *
84  * \param name the name of the viewer
85  * \param wdata the WindowData instance for the widget to use as render widget
86  * \param x X coordinate of widget where to create the context.
87  * \param y Y coordinate of widget where to create the context.
88  * \param width Width of the widget.
89  * \param height Height of the Widget.
90  * \param projectionMode Projection mode of the viewer.
91  * \exception WGEInitFailed thrown if initialization of graphics context or graphics window has failed.
92  */
93  WGEViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y, int width, int height,
94  WGECamera::ProjectionMode projectionMode = WGECamera::ORTHOGRAPHIC );
95 
96  /**
97  * Destructor.
98  */
99  virtual ~WGEViewer();
100 
101  /**
102  * Repaints the contents. Mac only.
103  */
104  virtual void paint();
105 
106  /**
107  * Updates size information. Also updates camera.
108  *
109  * \param width new width.
110  * \param height new height.
111  */
112  virtual void resize( int width, int height );
113 
114  /**
115  * Close the viewer, but wait for the rendering thread to finish.
116  */
117  virtual void close();
118 
119  /**
120  * Getter for OpenSceneGraph View instance.
121  *
122  * \return the OSG Viewer instance.
123  */
124 #ifdef WGEMODE_SINGLETHREADED
125  osg::ref_ptr<osgViewer::Viewer> getView();
126 #else
127  osg::ref_ptr<osgViewer::View> getView();
128 #endif
129 
130  /**
131  * Resets the view using the installed manipulator.
132  */
133  void reset();
134 
135  /**
136  * Sets the camera manipulator to use.
137  *
138  * \param manipulator the manipulator to use.
139  */
140  void setCameraManipulator( osg::ref_ptr<osgGA::MatrixManipulator> manipulator );
141 
142  /**
143  * Returns current active camera manipulator
144  *
145  * \return the active camera manipulator.
146  */
147  osg::ref_ptr<osgGA::MatrixManipulator> getCameraManipulator();
148 
149  /**
150  * Sets the current camera.
151  *
152  * \param camera the OSG camera instance.
153  */
154  void setCamera( osg::ref_ptr<osg::Camera> camera );
155 
156  /**
157  * Returns the camera currently in use.
158  *
159  * \return the camera currently in use.
160  */
161  osg::ref_ptr<osg::Camera> getCamera();
162 
163  /**
164  * Sets the scene graph node to be used for rendering.
165  *
166  * \param node part of the scene graph
167  */
168  void setScene( osg::ref_ptr< WGEGroupNode > node );
169 
170  /**
171  * Returns the currently set OSG node.
172  *
173  * \return the node.
174  */
175  osg::ref_ptr< WGEGroupNode > getScene();
176 
177  /**
178  * Returns the name of the viewer.
179  *
180  * \return the name
181  */
182  std::string getName() const;
183 
184  /**
185  * Determine the color of the viewer's background.
186  * \param bgColor the new background color
187  */
188  void setBgColor( const WColor& bgColor );
189 
190  /**
191  * Getter for the pick handler
192  *
193  * \return the pick handler
194  */
195  osg::ref_ptr< WPickHandler > getPickHandler();
196 
197  /**
198  * Queries the OpenGL vendor info.
199  *
200  * \return Vendor string.
201  */
202  std::string getOpenGLVendor() const;
203 
204  /**
205  * Returns the flag which denotes whether a frame was rendered.
206  *
207  * \return the flag.
208  */
210 
211  /**
212  * Returns the main cameras screen capture callback.
213  *
214  * \return the screen capture callback.
215  */
217 
218  /**
219  * The (de-)activates the animation mode. In animation mode, a special camera manipulator is used instead of the currently set. This
220  * manipulator can then play some animation path in realtime, frame-rate independent or in frame-per-frame mode which is useful if combined
221  * with the getScreenCapture() record function.
222  *
223  * If animation mode is turned off again, the previously set manipulator / camera setting is restored.
224  *
225  * \note do not modify camera or camera manipulator manually while in animation mode.
226  *
227  * \param on true to turn on.
228  *
229  * \return the animation manipulator. This, and only this should be used to provide the animation.
230  */
232 
233  /**
234  * Checks if the viewer is in animation mode.
235  *
236  * \return true if in animation mode
237  */
238  bool isAnimationMode() const;
239 
240 protected:
241  /**
242  * The OpenSceneGraph view used in this (Composite)Viewer.
243  */
244 #ifdef WGEMODE_SINGLETHREADED
245  osg::ref_ptr< osgViewer::Viewer > m_View;
246 #else
247  osg::ref_ptr< osgViewer::View > m_View;
248 #endif
249 
250  /**
251  * The name of the viewer.
252  */
253  std::string m_name;
254 
255  /**
256  * Pointer to the pick handler of the viewer.
257  */
258  osg::ref_ptr<WPickHandler> m_pickHandler;
259 
260  /**
261  * reference to the scene which is displayed by viewer
262  */
263  osg::ref_ptr< WGEGroupNode > m_scene;
264 
265  /**
266  * This flag is true and notifies after the first rendered frame.
267  */
269 
270  /**
271  * Small class used for querying glGet info during rendering.
272  */
273  class QueryCallback: public osg::Camera::DrawCallback
274  {
275  public:
276  /**
277  * Constructor. Automatically de-registers from camera after one run.
278  *
279  * \param camera the cam to which this was registered
280  * \param run notifies the flag when run.
281  */
282  QueryCallback( osg::ref_ptr<osg::Camera> camera, WBoolFlag::SPtr run );
283 
284  /**
285  * Destructor.
286  */
287  virtual ~QueryCallback();
288 
289  /**
290  * Query operator.
291  *
292  * \param renderInfo render info object
293  */
294  virtual void operator()( osg::RenderInfo& renderInfo ) const; // NOLINT - this is OSG API
295 
296  /**
297  * Returns the queried vendor string.
298  *
299  * \return the vendor
300  */
301  std::string getVendor() const;
302 
303  protected:
304  /**
305  * The vendor string.
306  */
307  mutable std::string m_vendor;
308 
309  /**
310  * True if callback was run once.
311  */
313 
314  /**
315  * The camera to which this was connected.
316  */
317  osg::ref_ptr<osg::Camera> m_camera;
318  };
319 
320  /**
321  * The callback used for querying OpenGL features
322  */
323  osg::ref_ptr< QueryCallback > m_queryCallback;
324 
325 
326  /**
327  * The screen capture callback.
328  */
330 
331  /**
332  * True -> animation mode on.
333  */
335 
336  /**
337  * The manipulator that was set before entering animation mode. Null if not in animation mode.
338  */
339  osg::ref_ptr<osgGA::MatrixManipulator> m_animationModeManipulatorBackup;
340 
341 private:
342 };
343 
344 #endif // WGEVIEWER_H