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