OpenWalnut  1.4.0
WGraphicsEngine.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 WGRAPHICSENGINE_H
26 #define WGRAPHICSENGINE_H
27 
28 #include <map>
29 #include <string>
30 
31 #ifndef Q_MOC_RUN
32 #include <boost/shared_ptr.hpp>
33 #endif
34 #ifndef Q_MOC_RUN
35 #include <boost/signals2/signal.hpp>
36 #endif
37 #ifndef Q_MOC_RUN
38 #include <boost/thread/mutex.hpp>
39 #endif
40 
41 #include <osg/Camera>
42 #include <osg/Texture3D>
43 #include <osg/Vec3>
44 #include <osg/Vec4>
45 #include <osg/ref_ptr>
46 #include <osgViewer/CompositeViewer>
47 
48 #include "../common/WThreadedRunner.h"
49 #include "../common/WConditionOneShot.h"
50 #include "../common/WColor.h"
51 #include "WGEGraphicsWindow.h"
52 #include "WGEScene.h"
53 #include "WGEViewer.h"
54 #include "WGESignals.h"
55 
56 /**
57  * Base class for initializing the graphics engine. This Class also serves as adaptor to access the graphics
58  * engine.
59  * \ingroup ge
60  */
62 {
63 public:
64  /**
65  * Destructor.
66  */
67  virtual ~WGraphicsEngine();
68 
69  /**
70  * Returns the root node of the WGraphicsEngine (this is not the root node of the OSG).
71  *
72  * \return the root node.
73  */
74  osg::ref_ptr<WGEScene> getScene();
75 
76  /**
77  * Creates a new viewer. Does basic initialization and sets the default scene.
78  *
79  * \param name the name of the viewer
80  * \param wdata the WindowData instance for the widget to use as render widget
81  * \param x X coordinate of widget where to create the context.
82  * \param y Y coordinate of widget where to create the context.
83  * \param width Width of the widget.
84  * \param height Height of the Widget.
85  * \param projectionMode Projection mode of the viewer.
86  * \param bgColor background color shown in the viewer.
87  * \return the new instance, ready to be used.
88  * \exception WGEInitFailed thrown if initialization of graphics context or graphics window has failed.
89  */
90  boost::shared_ptr< WGEViewer > createViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y,
91  int width, int height, WGECamera::ProjectionMode projectionMode = WGECamera::ORTHOGRAPHIC,
92  WColor bgColor = WColor( 1.0, 1.0, 1.0, 1.0 ) );
93 
94  /**
95  * Closes a viewer and deletes it from the list of viewers.
96  *
97  * \param name the name of the viewer
98  */
99  void closeViewer( const std::string name );
100 
101  /**
102  * Searches for a viewer with a given name and returns it, if found.
103  *
104  * \param name the name of the viewer
105  * \returns a shared pointer to the viewer or NULL if not found
106  */
107  boost::shared_ptr< WGEViewer > getViewerByName( std::string name );
108 
109  /**
110  * Returns the unnamed view, which is the view for the default scene which can be acquired using getScene().
111  *
112  * \return the viewer for the default scene.
113  */
114  boost::shared_ptr< WGEViewer > getViewer();
115 
116  /**
117  * Returns instance of the graphics engine. If it does not exists, it will be created.
118  *
119  * \return the running graphics engine instance.
120  */
121  static boost::shared_ptr< WGraphicsEngine > getGraphicsEngine();
122 
123  /**
124  * This requests all shaders to reload during the next update cycle.
125  */
126  void requestShaderReload();
127 
128  /**
129  * Subscribe a specified handler to the specified signal emited by the GE.
130  *
131  * \param signal the signal to connect to
132  * \param notifier the signal handler
133  *
134  * \return connection object.
135  */
136  boost::signals2::connection subscribeSignal( GE_SIGNAL signal, t_GEGenericSignalHandlerType notifier );
137 
138  /**
139  * Checks whether the graphics engine is currently running or not.
140  *
141  * \return true if running
142  */
143  static bool isRunning();
144 
145  /**
146  * Waits for the GE to come up. Fails if engine is not started.
147  *
148  * \return true if engine now running
149  */
150  static bool waitForStartupComplete();
151 
152  /**
153  * Function notifies the viewer threads (if any) to start. This should only be called AFTER the OpenGL widgets/windows have been initialized.
154  */
155  void finalizeStartup();
156 
157  /**
158  * Wait until someone called \ref finalizeStartup().
159  */
160  void waitForFinalize();
161 
162  /**
163  * Enables multithreaded view. This MUST be called before run(). On Mac, this has no function.
164  *
165  * \param enable true if multithreaded
166  */
167  void setMultiThreadedViews( bool enable = true );
168 
169  /**
170  * Checks whether the viewers work multithreaded.
171  *
172  * \return true if multithreaded
173  */
174  bool isMultiThreadedViews() const;
175 
176 protected:
177  /**
178  * Constructors are protected because this is a Singleton.
179  */
180  explicit WGraphicsEngine();
181 
182  /**
183  * Handler for repainting and event handling. Gets executed in separate thread.
184  */
185  virtual void threadMain();
186 
187  /**
188  * Gets called when the thread should be stopped.
189  */
190  virtual void notifyStop();
191 
192  /**
193  * OpenSceneGraph root node.
194  */
195  osg::ref_ptr<WGEScene> m_rootNode;
196 
197  /**
198  * All registered viewers.
199  */
200  std::map< std::string, boost::shared_ptr< WGEViewer > > m_viewers;
201 
202  /**
203  * Mutex used to lock the map of viewers.
204  */
205  boost::mutex m_viewersLock;
206 
207  /**
208  * OpenSceneGraph composite viewer. Contains all created osgViewer::Views.
209  */
210  osg::ref_ptr<osgViewer::CompositeViewer> m_viewer;
211 
212  /**
213  * Signal getting emitted whenever a reload shader request is waiting.
214  */
215  t_GEGenericSignalType m_reloadShadersSignal;
216 
217 private:
218  /**
219  * Singleton instance of WGraphicsEngine.
220  */
221  static boost::shared_ptr< WGraphicsEngine > m_instance;
222 
223  /**
224  * True if graphics engine is running.
225  */
226  bool m_running;
227 
228  /**
229  * This condition is fired externally if all the GUI startup is done to ensure all OGL stuff is initialized prior to OSG threading startup.
230  */
232 };
233 
234 /**
235  * \defgroup ge GraphicsEngine
236  *
237  * \brief
238  * This library implements the graphics engine for OpenWalnut.
239  */
240 
241 /**
242  * Convinient functions for use with the graphics engine of OpenWalnut. ATM the
243  * namespace is filled by several files: WGEGeodeUtils, WGEGeometryUtils and
244  * WGEUtils.
245  */
246 namespace wge
247 {
248 } // end of namespace
249 
250 #endif // WGRAPHICSENGINE_H
boost::shared_ptr< WGEViewer > createViewer(std::string name, osg::ref_ptr< osg::Referenced > wdata, int x, int y, int width, int height, WGECamera::ProjectionMode projectionMode=WGECamera::ORTHOGRAPHIC, WColor bgColor=WColor(1.0, 1.0, 1.0, 1.0))
Creates a new viewer.
static bool isRunning()
Checks whether the graphics engine is currently running or not.
static boost::shared_ptr< WGraphicsEngine > m_instance
Singleton instance of WGraphicsEngine.
WConditionOneShot m_startThreadingCondition
This condition is fired externally if all the GUI startup is done to ensure all OGL stuff is initiali...
void requestShaderReload()
This requests all shaders to reload during the next update cycle.
virtual void threadMain()
Handler for repainting and event handling.
Implements a WCondition, but can be fired only ONCE.
ProjectionMode
List of possible camera modes.
Definition: WGECamera.h:43
virtual ~WGraphicsEngine()
Destructor.
osg::ref_ptr< WGEScene > getScene()
Returns the root node of the WGraphicsEngine (this is not the root node of the OSG).
Base class for all classes needing to be executed in a separate thread.
boost::mutex m_viewersLock
Mutex used to lock the map of viewers.
Extend the wge utils namespace with additional methods relating WDataTexture3D.
virtual void notifyStop()
Gets called when the thread should be stopped.
bool isMultiThreadedViews() const
Checks whether the viewers work multithreaded.
static boost::shared_ptr< WGraphicsEngine > getGraphicsEngine()
Returns instance of the graphics engine.
boost::shared_ptr< WGEViewer > getViewer()
Returns the unnamed view, which is the view for the default scene which can be acquired using getScen...
Base class for initializing the graphics engine.
void closeViewer(const std::string name)
Closes a viewer and deletes it from the list of viewers.
std::map< std::string, boost::shared_ptr< WGEViewer > > m_viewers
All registered viewers.
WGraphicsEngine()
Constructors are protected because this is a Singleton.
void finalizeStartup()
Function notifies the viewer threads (if any) to start.
bool m_running
True if graphics engine is running.
void setMultiThreadedViews(bool enable=true)
Enables multithreaded view.
void waitForFinalize()
Wait until someone called finalizeStartup().
osg::ref_ptr< osgViewer::CompositeViewer > m_viewer
OpenSceneGraph composite viewer.
osg::ref_ptr< WGEScene > m_rootNode
OpenSceneGraph root node.
static bool waitForStartupComplete()
Waits for the GE to come up.
t_GEGenericSignalType m_reloadShadersSignal
Signal getting emitted whenever a reload shader request is waiting.
boost::signals2::connection subscribeSignal(GE_SIGNAL signal, t_GEGenericSignalHandlerType notifier)
Subscribe a specified handler to the specified signal emited by the GE.
boost::shared_ptr< WGEViewer > getViewerByName(std::string name)
Searches for a viewer with a given name and returns it, if found.