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