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