OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 <osgGA/DriveManipulator>
36 #include <osgGA/FlightManipulator>
37 #include <osgGA/TerrainManipulator>
38 #include <osgGA/UFOManipulator>
39 #include <osgViewer/View>
40 
41 // OSG interface changed in 2.9.7, to make it compile also with those versions we do this:
42 // OSG_MIN_VERSION_REQUIRED(2, 9, 8) macro is not available in e.g. OSG 2.8.1, hence we use the old way
43 #if ( ( OPENSCENEGRAPH_MAJOR_VERSION > 2 ) || ( OPENSCENEGRAPH_MAJOR_VERSION == 2 && ( OPENSCENEGRAPH_MINOR_VERSION > 9 || \
44  ( OPENSCENEGRAPH_MINOR_VERSION == 9 && OPENSCENEGRAPH_PATCH_VERSION >= 8 ) ) ) )
45  #include <osgGA/CameraManipulator>
46  namespace osgGA
47  {
48  typedef CameraManipulator MatrixManipulator;
49  }
50 #else
51  #include <osgGA/MatrixManipulator>
52 #endif
53 
54 #include "../common/WColor.h"
55 #include "../common/WFlag.h"
56 #include "../common/WThreadedRunner.h"
57 #include "WExportWGE.h"
58 #include "WGECamera.h"
59 #include "WGEGraphicsWindow.h"
60 #include "WGEGroupNode.h"
61 #include "WPickHandler.h"
62 
63 /**
64  * Class for managing one view to the scene. This includes viewport, camera and graphics context.
65  * It is, besides WGraphicsEngine, the ONLY entry point for each widget for accessing the graphics engine.
66  * \ingroup ge
67  */
68 class WGE_EXPORT WGEViewer: public WGEGraphicsWindow,
69  public boost::enable_shared_from_this< WGEViewer >
70 {
71 public:
72  /**
73  * Default constructor.
74  *
75  * \param name the name of the viewer
76  * \param wdata the WindowData instance for the widget to use as render widget
77  * \param x X coordinate of widget where to create the context.
78  * \param y Y coordinate of widget where to create the context.
79  * \param width Width of the widget.
80  * \param height Height of the Widget.
81  * \param projectionMode Projection mode of the viewer.
82  * \exception WGEInitFailed thrown if initialization of graphics context or graphics window has failed.
83  */
84  WGEViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y, int width, int height,
85  WGECamera::ProjectionMode projectionMode = WGECamera::ORTHOGRAPHIC );
86 
87  /**
88  * Destructor.
89  */
90  virtual ~WGEViewer();
91  /**
92  * Repaints the contents. Mac only.
93  */
94  virtual void paint();
95 
96  /**
97  * Updates size information. Also updates camera.
98  *
99  * \param width new width.
100  * \param height new height.
101  */
102  virtual void resize( int width, int height );
103 
104  /**
105  * Close the viewer, but wait for the rendering thread to finish.
106  */
107  virtual void close();
108 
109  /**
110  * Getter for OpenSceneGraph View instance.
111  *
112  * \return the OSG Viewer instance.
113  */
114 #ifdef __APPLE__
115  osg::ref_ptr<osgViewer::Viewer> getView();
116 #else
117  osg::ref_ptr<osgViewer::View> getView();
118 #endif
119 
120  /**
121  * Resets the view using the installed manipulator.
122  */
123  void reset();
124 
125  /**
126  * Sets the camera manipulator to use.
127  *
128  * \param manipulator the manipulator to use.
129  */
130  void setCameraManipulator( osg::ref_ptr<osgGA::MatrixManipulator> manipulator );
131 
132  /**
133  * Returns current active camera manipulator
134  *
135  * \return the active camera manipulator.
136  */
137  osg::ref_ptr<osgGA::MatrixManipulator> getCameraManipulator();
138 
139  /**
140  * Sets the current camera.
141  *
142  * \param camera the OSG camera instance.
143  */
144  void setCamera( osg::ref_ptr<osg::Camera> camera );
145 
146  /**
147  * Returns the camera currently in use.
148  *
149  * \return the camera currently in use.
150  */
151  osg::ref_ptr<osg::Camera> getCamera();
152 
153  /**
154  * Sets the scene graph node to be used for rendering.
155  *
156  * \param node part of the scene graph
157  */
158  void setScene( osg::ref_ptr< WGEGroupNode > node );
159 
160  /**
161  * Returns the currently set OSG node.
162  *
163  * \return the node.
164  */
165  osg::ref_ptr< WGEGroupNode > getScene();
166 
167  /**
168  * Returns the name of the viewer.
169  *
170  * \return the name
171  */
172  std::string getName() const;
173 
174  /**
175  * Determine the color of the viewer's background.
176  * \param bgColor the new background color
177  */
178  void setBgColor( const WColor& bgColor );
179 
180  /**
181  * Getter for the pick handler
182  *
183  * \return the pick handler
184  */
185  osg::ref_ptr< WPickHandler > getPickHandler();
186 
187  /**
188  * Queries the OpenGL vendor info.
189  *
190  * \return Vendor string.
191  */
192  std::string getOpenGLVendor() const;
193 
194  /**
195  * Returns the flag which denotes whether a frame was rendered.
196  *
197  * \return the flag.
198  */
199  WBoolFlag::SPtr isFrameRendered() const;
200 
201 protected:
202  /**
203  * The OpenSceneGraph view used in this (Composite)Viewer.
204  */
205 #ifdef __APPLE__
206  osg::ref_ptr< osgViewer::Viewer > m_View;
207 #else
208  osg::ref_ptr< osgViewer::View > m_View;
209 #endif
210 
211  /**
212  * The name of the viewer.
213  */
214  std::string m_name;
215 
216  /**
217  * Pointer to the pick handler of the viewer.
218  */
219  osg::ref_ptr<WPickHandler> m_pickHandler;
220 
221  /**
222  * reference to the scene which is displayed by viewer
223  */
224  osg::ref_ptr< WGEGroupNode > m_scene;
225 
226  /**
227  * This flag is true and notifies after the first rendered frame.
228  */
230 
231  /**
232  * Small class used for querying glGet info during rendering.
233  */
234  class QueryCallback: public osg::Camera::DrawCallback
235  {
236  public:
237  /**
238  * Constructor. Automatically de-registers from camera after one run.
239  *
240  * \param camera the cam to which this was registered
241  * \param run notifies the flag when run.
242  */
243  QueryCallback( osg::ref_ptr<osg::Camera> camera, WBoolFlag::SPtr run );
244 
245  /**
246  * Destructor.
247  */
248  virtual ~QueryCallback();
249 
250  /**
251  * Query operator.
252  *
253  * \param renderInfo render info object
254  */
255  virtual void operator()( osg::RenderInfo& renderInfo ) const; // NOLINT - this is OSG API
256 
257  /**
258  * Returns the queried vendor string.
259  *
260  * \return the vendor
261  */
262  std::string getVendor() const;
263 
264  protected:
265  /**
266  * The vendor string.
267  */
268  mutable std::string m_vendor;
269 
270  /**
271  * True if callback was run once.
272  */
274 
275  /**
276  * The camera to which this was connected.
277  */
278  osg::ref_ptr<osg::Camera> m_camera;
279  };
280 
281  /**
282  * The callback used for querying OpenGL features
283  */
284  osg::ref_ptr< QueryCallback > m_queryCallback;
285 private:
286 };
287 
288 #endif // WGEVIEWER_H