OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
WGEViewer.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 WGEVIEWER_H
00026 #define WGEVIEWER_H
00027 
00028 #include <string>
00029 
00030 #include <boost/shared_ptr.hpp>
00031 #include <boost/enable_shared_from_this.hpp>
00032 
00033 #include <osg/Node>
00034 #include <osg/Version>
00035 #include <osgGA/DriveManipulator>
00036 #include <osgGA/FlightManipulator>
00037 #include <osgGA/TerrainManipulator>
00038 #include <osgGA/UFOManipulator>
00039 #include <osgViewer/View>
00040 
00041 // OSG interface changed in 2.9.7, to make it compile also with those versions we do this:
00042 // OSG_MIN_VERSION_REQUIRED(2, 9, 8) macro is not available in e.g. OSG 2.8.1, hence we use the old way
00043 #if ( ( OPENSCENEGRAPH_MAJOR_VERSION > 2 ) || ( OPENSCENEGRAPH_MAJOR_VERSION == 2 && ( OPENSCENEGRAPH_MINOR_VERSION > 9 || \
00044                             ( OPENSCENEGRAPH_MINOR_VERSION == 9 && OPENSCENEGRAPH_PATCH_VERSION >= 8 ) ) ) )
00045     #include <osgGA/CameraManipulator>
00046     namespace osgGA
00047     {
00048         typedef CameraManipulator MatrixManipulator;
00049     }
00050 #else
00051     #include <osgGA/MatrixManipulator>
00052 #endif
00053 
00054 #include "../common/WColor.h"
00055 #include "../common/WFlag.h"
00056 #include "../common/WThreadedRunner.h"
00057 #include "WExportWGE.h"
00058 #include "WGECamera.h"
00059 #include "WGEGraphicsWindow.h"
00060 #include "WGEGroupNode.h"
00061 #include "WPickHandler.h"
00062 
00063 /**
00064  * Class for managing one view to the scene. This includes viewport, camera and graphics context.
00065  * It is, besides WGraphicsEngine, the ONLY entry point for each widget for accessing the graphics engine.
00066  * \ingroup ge
00067  */
00068 class WGE_EXPORT WGEViewer: public WGEGraphicsWindow,
00069                             public boost::enable_shared_from_this< WGEViewer >
00070 {
00071 public:
00072     /**
00073      * Default constructor.
00074      *
00075      * \param name the name of the viewer
00076      * \param wdata the WindowData instance for the widget to use as render widget
00077      * \param x X coordinate of widget where to create the context.
00078      * \param y Y coordinate of widget where to create the context.
00079      * \param width Width of the widget.
00080      * \param height Height of the Widget.
00081      * \param projectionMode Projection mode of the viewer.
00082      * \exception WGEInitFailed thrown if initialization of graphics context or graphics window has failed.
00083      */
00084     WGEViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y, int width, int height,
00085         WGECamera::ProjectionMode projectionMode = WGECamera::ORTHOGRAPHIC );
00086 
00087     /**
00088      * Destructor.
00089      */
00090     virtual ~WGEViewer();
00091     /**
00092      * Repaints the contents. Mac only.
00093      */
00094     virtual void paint();
00095 
00096     /**
00097      * Updates size information. Also updates camera.
00098      *
00099      * \param width new width.
00100      * \param height new height.
00101      */
00102     virtual void resize( int width, int height );
00103 
00104     /**
00105      * Close the viewer, but wait for the rendering thread to finish.
00106      */
00107     virtual void close();
00108 
00109     /**
00110      * Getter for OpenSceneGraph View instance.
00111      *
00112      * \return the OSG Viewer instance.
00113      */
00114 #ifdef __APPLE__
00115     osg::ref_ptr<osgViewer::Viewer> getView();
00116 #else
00117     osg::ref_ptr<osgViewer::View> getView();
00118 #endif
00119 
00120     /**
00121      * Resets the view using the installed manipulator.
00122      */
00123     void reset();
00124 
00125     /**
00126      * Sets the camera manipulator to use.
00127      *
00128      * \param manipulator the manipulator to use.
00129      */
00130     void setCameraManipulator( osg::ref_ptr<osgGA::MatrixManipulator> manipulator );
00131 
00132     /**
00133      * Returns current active camera manipulator
00134      *
00135      * \return the active camera manipulator.
00136      */
00137     osg::ref_ptr<osgGA::MatrixManipulator> getCameraManipulator();
00138 
00139     /**
00140      * Sets the current camera.
00141      *
00142      * \param camera the OSG camera instance.
00143      */
00144     void setCamera( osg::ref_ptr<osg::Camera> camera );
00145 
00146     /**
00147      * Returns the camera currently in use.
00148      *
00149      * \return the camera currently in use.
00150      */
00151     osg::ref_ptr<osg::Camera> getCamera();
00152 
00153     /**
00154      * Sets the scene graph node to be used for rendering.
00155      *
00156      * \param node part of the scene graph
00157      */
00158     void setScene( osg::ref_ptr< WGEGroupNode > node );
00159 
00160     /**
00161      * Returns the currently set OSG node.
00162      *
00163      * \return the node.
00164      */
00165     osg::ref_ptr< WGEGroupNode > getScene();
00166 
00167     /**
00168      * Returns the name of the viewer.
00169      *
00170      * \return the name
00171      */
00172     std::string getName() const;
00173 
00174     /**
00175      * Determine the color of the viewer's background.
00176      * \param bgColor the new background color
00177      */
00178     void setBgColor( const WColor& bgColor );
00179 
00180     /**
00181      * Getter for the pick handler
00182      *
00183      * \return the pick handler
00184      */
00185     osg::ref_ptr< WPickHandler > getPickHandler();
00186 
00187     /**
00188      * Queries the OpenGL vendor info.
00189      *
00190      * \return Vendor string.
00191      */
00192     std::string getOpenGLVendor() const;
00193 
00194     /**
00195      * Returns the flag which denotes whether a frame was rendered.
00196      *
00197      * \return the flag.
00198      */
00199     WBoolFlag::SPtr isFrameRendered() const;
00200 
00201 protected:
00202     /**
00203      * The OpenSceneGraph view used in this (Composite)Viewer.
00204      */
00205 #ifdef __APPLE__
00206     osg::ref_ptr< osgViewer::Viewer > m_View;
00207 #else
00208     osg::ref_ptr< osgViewer::View > m_View;
00209 #endif
00210 
00211     /**
00212      * The name of the viewer.
00213      */
00214     std::string m_name;
00215 
00216     /**
00217      * Pointer to the pick handler of the viewer.
00218      */
00219     osg::ref_ptr<WPickHandler> m_pickHandler;
00220 
00221     /**
00222      * reference to the scene which is displayed by viewer
00223      */
00224     osg::ref_ptr< WGEGroupNode > m_scene;
00225 
00226     /**
00227      * This flag is true and notifies after the first rendered frame.
00228      */
00229     WBoolFlag::SPtr m_rendered;
00230 
00231     /**
00232      * Small class used for querying glGet info during rendering.
00233      */
00234     class QueryCallback: public osg::Camera::DrawCallback
00235     {
00236     public:
00237         /**
00238          * Constructor. Automatically de-registers from camera after one run.
00239          *
00240          * \param camera the cam to which this was registered
00241          * \param run notifies the flag when run.
00242          */
00243         QueryCallback( osg::ref_ptr<osg::Camera> camera, WBoolFlag::SPtr run );
00244 
00245         /**
00246          * Destructor.
00247          */
00248         virtual ~QueryCallback();
00249 
00250         /**
00251          * Query operator.
00252          *
00253          * \param renderInfo render info object
00254          */
00255         virtual void operator()( osg::RenderInfo& renderInfo ) const;   // NOLINT - this is OSG API
00256 
00257         /**
00258          * Returns the queried vendor string.
00259          *
00260          * \return the vendor
00261          */
00262         std::string getVendor() const;
00263 
00264     protected:
00265         /**
00266          * The vendor string.
00267          */
00268         mutable std::string m_vendor;
00269 
00270         /**
00271          * True if callback was run once.
00272          */
00273         WBoolFlag::SPtr m_run;
00274 
00275         /**
00276          * The camera to which this was connected.
00277          */
00278         osg::ref_ptr<osg::Camera> m_camera;
00279     };
00280 
00281     /**
00282      * The callback used for querying OpenGL features
00283      */
00284     osg::ref_ptr< QueryCallback > m_queryCallback;
00285 private:
00286 };
00287 
00288 #endif  // WGEVIEWER_H
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends