WGraphicsEngine.h

00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WGRAPHICSENGINE_H
00026 #define WGRAPHICSENGINE_H
00027 
00028 #include <map>
00029 #include <string>
00030 #include <vector>
00031 
00032 #include <boost/shared_ptr.hpp>
00033 #include <boost/signals2/signal.hpp>
00034 #include <boost/function.hpp>
00035 #include <boost/thread/mutex.hpp>
00036 #include <boost/thread/thread.hpp>
00037 
00038 #include <osg/Camera>
00039 #include <osg/Texture3D>
00040 #include <osg/Vec3>
00041 #include <osg/Vec4>
00042 #include <osg/ref_ptr>
00043 #include <osgViewer/CompositeViewer>
00044 #include <osgViewer/View>
00045 #include <osgViewer/Viewer>
00046 
00047 #include "../common/WThreadedRunner.h"
00048 #include "../common/WConditionOneShot.h"
00049 #include "../common/WColor.h"
00050 #include "../common/math/linearAlgebra/WLinearAlgebra.h"
00051 #include "WGEGraphicsWindow.h"
00052 #include "WGEScene.h"
00053 #include "WGEViewer.h"
00054 #include "WGESignals.h"
00055 
00056 #include "WExportWGE.h"
00057 
00058 /**
00059  * Base class for initializing the graphics engine. This Class also serves as adaptor to access the graphics
00060  * engine.
00061  * \ingroup ge
00062  */
00063 class WGE_EXPORT WGraphicsEngine: public WThreadedRunner
00064 {
00065 public:
00066 
00067     /**
00068      * Destructor.
00069      */
00070     virtual ~WGraphicsEngine();
00071 
00072     /**
00073      * Returns the root node of the WGraphicsEngine (this is not the root node of the OSG).
00074      *
00075      * \return the root node.
00076      */
00077     osg::ref_ptr<WGEScene> getScene();
00078 
00079     /**
00080      * Creates a new viewer. Does basic initialization and sets the default scene.
00081      *
00082      * \param name the name of the viewer
00083      * \param wdata the WindowData instance for the widget to use as render widget
00084      * \param x X coordinate of widget where to create the context.
00085      * \param y Y coordinate of widget where to create the context.
00086      * \param width Width of the widget.
00087      * \param height Height of the Widget.
00088      * \param projectionMode Projection mode of the viewer.
00089      * \param bgColor background color shown in the viewer.
00090      * \return the new instance, ready to be used.
00091      * \exception WGEInitFailed thrown if initialization of graphics context or graphics window has failed.
00092      */
00093     boost::shared_ptr< WGEViewer > createViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y,
00094                                                int width, int height, WGECamera::ProjectionMode projectionMode = WGECamera::ORTHOGRAPHIC,
00095                                                WColor bgColor = WColor( 0.9, 0.9, 0.9, 1.0 ) );
00096 
00097     /**
00098      * Closes a viewer and deletes it from the list of viewers.
00099      *
00100      * \param name the name of the viewer
00101      */
00102     void closeViewer( const std::string name );
00103 
00104     /**
00105      * Searches for a viewer with a given name and returns it, if found.
00106      *
00107      * \param name the name of the viewer
00108      * \returns a shared pointer to the viewer or NULL if not found
00109      */
00110     boost::shared_ptr< WGEViewer > getViewerByName( std::string name );
00111 
00112     /**
00113      * Returns the unnamed view, which is the view for the default scene which can be acquired using getScene().
00114      *
00115      * \return the viewer for the default scene.
00116      */
00117     boost::shared_ptr< WGEViewer > getViewer();
00118 
00119     /**
00120      * Returns instance of the graphics engine. If it does not exists, it will be created.
00121      *
00122      * \return the running graphics engine instance.
00123      */
00124     static boost::shared_ptr< WGraphicsEngine > getGraphicsEngine();
00125 
00126     /**
00127      * This requests all shaders to reload during the next update cycle.
00128      */
00129     void requestShaderReload();
00130 
00131     /**
00132      * Subscribe a specified handler to the specified signal emited by the GE.
00133      *
00134      * \param signal    the signal to connect to
00135      * \param notifier the signal handler
00136      *
00137      * \return  connection object.
00138      */
00139     boost::signals2::connection subscribeSignal( GE_SIGNAL signal, t_GEGenericSignalHandlerType notifier );
00140 
00141     /**
00142      * Checks whether the graphics engine is currently running or not.
00143      *
00144      * \return true if running
00145      */
00146     static bool isRunning();
00147 
00148     /**
00149      * Waits for the GE to come up. Fails if engine is not started.
00150      *
00151      * \return true if engine now running
00152      */
00153     static bool waitForStartupComplete();
00154 
00155     /**
00156      * Function notifies the viewer threads (if any) to start. This should only be called AFTER the OpenGL widgets/windows have been initialized.
00157      */
00158     void finalizeStartup();
00159 
00160     /**
00161      * Enables multithreaded view. This MUST be called before run(). On Mac, this has no function.
00162      *
00163      * \param enable true if multithreaded
00164      */
00165     void setMultiThreadedViews( bool enable = true );
00166 
00167     /**
00168      * Checks whether the viewers work multithreaded.
00169      *
00170      * \return true if multithreaded
00171      */
00172     bool isMultiThreadedViews() const;
00173 
00174 protected:
00175 
00176     /**
00177      * Constructors are protected because this is a Singleton.
00178      */
00179     explicit WGraphicsEngine();
00180 
00181     /**
00182      * Handler for repainting and event handling. Gets executed in separate thread.
00183      */
00184     virtual void threadMain();
00185 
00186     /**
00187      * Gets called when the thread should be stopped.
00188      */
00189     virtual void notifyStop();
00190 
00191     /**
00192      * OpenSceneGraph root node.
00193      */
00194     osg::ref_ptr<WGEScene> m_rootNode;
00195 
00196     /**
00197      * All registered viewers.
00198      */
00199     std::map< std::string, boost::shared_ptr< WGEViewer > > m_viewers;
00200 
00201     /**
00202      * Mutex used to lock the map of viewers.
00203      */
00204     boost::mutex m_viewersLock;
00205 
00206     /**
00207      * OpenSceneGraph composite viewer. Contains all created osgViewer::Views.
00208      */
00209     osg::ref_ptr<osgViewer::CompositeViewer> m_viewer;
00210 
00211     /**
00212      * Signal getting emitted whenever a reload shader request is waiting.
00213      */
00214     t_GEGenericSignalType m_reloadShadersSignal;
00215 
00216 private:
00217     /**
00218      * Singleton instance of WGraphicsEngine.
00219      */
00220     static boost::shared_ptr< WGraphicsEngine > m_instance;
00221 
00222     /**
00223      * True if graphics engine is running.
00224      */
00225     bool m_running;
00226 
00227     /**
00228      * This condition is fired externally if all the GUI startup is done to ensure all OGL stuff is initialized prior to OSG threading startup.
00229      */
00230     WConditionOneShot m_startThreadingCondition;
00231 };
00232 
00233 /**
00234  * \defgroup ge GraphicsEngine
00235  *
00236  * \brief
00237  * This library implements the graphics engine for OpenWalnut.
00238  */
00239 
00240 /**
00241  * Convinient functions for use with the graphics engine of OpenWalnut. ATM the
00242  * namespace is filled by several files: WGEGeodeUtils, WGEGeometryUtils and
00243  * WGEUtils.
00244  */
00245 namespace wge
00246 {
00247 } // end of namespace
00248 
00249 #endif  // WGRAPHICSENGINE_H
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends