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