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