OpenWalnut  1.4.0
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 
00031 #include <boost/shared_ptr.hpp>
00032 #include <boost/signals2/signal.hpp>
00033 #include <boost/thread/mutex.hpp>
00034 
00035 #include <osg/Camera>
00036 #include <osg/Texture3D>
00037 #include <osg/Vec3>
00038 #include <osg/Vec4>
00039 #include <osg/ref_ptr>
00040 #include <osgViewer/CompositeViewer>
00041 
00042 #include "../common/WThreadedRunner.h"
00043 #include "../common/WConditionOneShot.h"
00044 #include "../common/WColor.h"
00045 #include "WGEGraphicsWindow.h"
00046 #include "WGEScene.h"
00047 #include "WGEViewer.h"
00048 #include "WGESignals.h"
00049 
00050 /**
00051  * Base class for initializing the graphics engine. This Class also serves as adaptor to access the graphics
00052  * engine.
00053  * \ingroup ge
00054  */
00055 class WGraphicsEngine: public WThreadedRunner
00056 {
00057 public:
00058     /**
00059      * Destructor.
00060      */
00061     virtual ~WGraphicsEngine();
00062 
00063     /**
00064      * Returns the root node of the WGraphicsEngine (this is not the root node of the OSG).
00065      *
00066      * \return the root node.
00067      */
00068     osg::ref_ptr<WGEScene> getScene();
00069 
00070     /**
00071      * Creates a new viewer. Does basic initialization and sets the default scene.
00072      *
00073      * \param name the name of the viewer
00074      * \param wdata the WindowData instance for the widget to use as render widget
00075      * \param x X coordinate of widget where to create the context.
00076      * \param y Y coordinate of widget where to create the context.
00077      * \param width Width of the widget.
00078      * \param height Height of the Widget.
00079      * \param projectionMode Projection mode of the viewer.
00080      * \param bgColor background color shown in the viewer.
00081      * \return the new instance, ready to be used.
00082      * \exception WGEInitFailed thrown if initialization of graphics context or graphics window has failed.
00083      */
00084     boost::shared_ptr< WGEViewer > createViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y,
00085                                                int width, int height, WGECamera::ProjectionMode projectionMode = WGECamera::ORTHOGRAPHIC,
00086                                                WColor bgColor = WColor( 1.0, 1.0, 1.0, 1.0 ) );
00087 
00088     /**
00089      * Closes a viewer and deletes it from the list of viewers.
00090      *
00091      * \param name the name of the viewer
00092      */
00093     void closeViewer( const std::string name );
00094 
00095     /**
00096      * Searches for a viewer with a given name and returns it, if found.
00097      *
00098      * \param name the name of the viewer
00099      * \returns a shared pointer to the viewer or NULL if not found
00100      */
00101     boost::shared_ptr< WGEViewer > getViewerByName( std::string name );
00102 
00103     /**
00104      * Returns the unnamed view, which is the view for the default scene which can be acquired using getScene().
00105      *
00106      * \return the viewer for the default scene.
00107      */
00108     boost::shared_ptr< WGEViewer > getViewer();
00109 
00110     /**
00111      * Returns instance of the graphics engine. If it does not exists, it will be created.
00112      *
00113      * \return the running graphics engine instance.
00114      */
00115     static boost::shared_ptr< WGraphicsEngine > getGraphicsEngine();
00116 
00117     /**
00118      * This requests all shaders to reload during the next update cycle.
00119      */
00120     void requestShaderReload();
00121 
00122     /**
00123      * Subscribe a specified handler to the specified signal emited by the GE.
00124      *
00125      * \param signal    the signal to connect to
00126      * \param notifier the signal handler
00127      *
00128      * \return  connection object.
00129      */
00130     boost::signals2::connection subscribeSignal( GE_SIGNAL signal, t_GEGenericSignalHandlerType notifier );
00131 
00132     /**
00133      * Checks whether the graphics engine is currently running or not.
00134      *
00135      * \return true if running
00136      */
00137     static bool isRunning();
00138 
00139     /**
00140      * Waits for the GE to come up. Fails if engine is not started.
00141      *
00142      * \return true if engine now running
00143      */
00144     static bool waitForStartupComplete();
00145 
00146     /**
00147      * Function notifies the viewer threads (if any) to start. This should only be called AFTER the OpenGL widgets/windows have been initialized.
00148      */
00149     void finalizeStartup();
00150 
00151     /**
00152      * Wait until someone called \ref finalizeStartup().
00153      */
00154     void waitForFinalize();
00155 
00156     /**
00157      * Enables multithreaded view. This MUST be called before run(). On Mac, this has no function.
00158      *
00159      * \param enable true if multithreaded
00160      */
00161     void setMultiThreadedViews( bool enable = true );
00162 
00163     /**
00164      * Checks whether the viewers work multithreaded.
00165      *
00166      * \return true if multithreaded
00167      */
00168     bool isMultiThreadedViews() const;
00169 
00170 protected:
00171     /**
00172      * Constructors are protected because this is a Singleton.
00173      */
00174     explicit WGraphicsEngine();
00175 
00176     /**
00177      * Handler for repainting and event handling. Gets executed in separate thread.
00178      */
00179     virtual void threadMain();
00180 
00181     /**
00182      * Gets called when the thread should be stopped.
00183      */
00184     virtual void notifyStop();
00185 
00186     /**
00187      * OpenSceneGraph root node.
00188      */
00189     osg::ref_ptr<WGEScene> m_rootNode;
00190 
00191     /**
00192      * All registered viewers.
00193      */
00194     std::map< std::string, boost::shared_ptr< WGEViewer > > m_viewers;
00195 
00196     /**
00197      * Mutex used to lock the map of viewers.
00198      */
00199     boost::mutex m_viewersLock;
00200 
00201     /**
00202      * OpenSceneGraph composite viewer. Contains all created osgViewer::Views.
00203      */
00204     osg::ref_ptr<osgViewer::CompositeViewer> m_viewer;
00205 
00206     /**
00207      * Signal getting emitted whenever a reload shader request is waiting.
00208      */
00209     t_GEGenericSignalType m_reloadShadersSignal;
00210 
00211 private:
00212     /**
00213      * Singleton instance of WGraphicsEngine.
00214      */
00215     static boost::shared_ptr< WGraphicsEngine > m_instance;
00216 
00217     /**
00218      * True if graphics engine is running.
00219      */
00220     bool m_running;
00221 
00222     /**
00223      * This condition is fired externally if all the GUI startup is done to ensure all OGL stuff is initialized prior to OSG threading startup.
00224      */
00225     WConditionOneShot m_startThreadingCondition;
00226 };
00227 
00228 /**
00229  * \defgroup ge GraphicsEngine
00230  *
00231  * \brief
00232  * This library implements the graphics engine for OpenWalnut.
00233  */
00234 
00235 /**
00236  * Convinient functions for use with the graphics engine of OpenWalnut. ATM the
00237  * namespace is filled by several files: WGEGeodeUtils, WGEGeometryUtils and
00238  * WGEUtils.
00239  */
00240 namespace wge
00241 {
00242 } // end of namespace
00243 
00244 #endif  // WGRAPHICSENGINE_H