OpenWalnut 1.3.1
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 <osgViewer/View>
00036 #include <osgViewer/Viewer>
00037 
00038 // OSG interface changed in 2.9.7, to make it compile also with those versions we do this:
00039 // OSG_MIN_VERSION_REQUIRED(2, 9, 8) macro is not available in e.g. OSG 2.8.1, hence we use the old way
00040 #if ( ( OPENSCENEGRAPH_MAJOR_VERSION > 2 ) || ( OPENSCENEGRAPH_MAJOR_VERSION == 2 && ( OPENSCENEGRAPH_MINOR_VERSION > 9 || \
00041                             ( OPENSCENEGRAPH_MINOR_VERSION == 9 && OPENSCENEGRAPH_PATCH_VERSION >= 8 ) ) ) )
00042     #include <osgGA/CameraManipulator>
00043     namespace osgGA
00044     {
00045         typedef CameraManipulator MatrixManipulator;
00046     }
00047 #else
00048     #include <osgGA/MatrixManipulator>
00049 #endif
00050 
00051 #include "../common/WColor.h"
00052 #include "../common/WFlag.h"
00053 
00054 #include "WGECamera.h"
00055 #include "WGEGraphicsWindow.h"
00056 #include "WGraphicsEngineMode.h"
00057 class WGEGroupNode;
00058 #include "WGEScreenCapture.h"
00059 class WPickHandler;
00060 #include "animation/WGEAnimationManipulator.h"
00061 
00062 /**
00063  * Class for managing one view to the scene. This includes viewport, camera and graphics context.
00064  * It is, besides WGraphicsEngine, the ONLY entry point for each widget for accessing the graphics engine.
00065  * \ingroup ge
00066  */
00067 class WGEViewer: public WGEGraphicsWindow,
00068                             public boost::enable_shared_from_this< WGEViewer >
00069 {
00070 public:
00071     /**
00072      * Convenience typedef
00073      */
00074     typedef boost::shared_ptr< WGEViewer > SPtr;
00075 
00076     /**
00077      * Convenience typedef
00078      */
00079     typedef boost::shared_ptr< const WGEViewer > ConstSPtr;
00080 
00081     /**
00082      * Default constructor.
00083      *
00084      * \param name the name of the viewer
00085      * \param wdata the WindowData instance for the widget to use as render widget
00086      * \param x X coordinate of widget where to create the context.
00087      * \param y Y coordinate of widget where to create the context.
00088      * \param width Width of the widget.
00089      * \param height Height of the Widget.
00090      * \param projectionMode Projection mode of the viewer.
00091      * \exception WGEInitFailed thrown if initialization of graphics context or graphics window has failed.
00092      */
00093     WGEViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y, int width, int height,
00094         WGECamera::ProjectionMode projectionMode = WGECamera::ORTHOGRAPHIC );
00095 
00096     /**
00097      * Destructor.
00098      */
00099     virtual ~WGEViewer();
00100 
00101     /**
00102      * Repaints the contents. Mac only.
00103      */
00104     virtual void paint();
00105 
00106     /**
00107      * Updates size information. Also updates camera.
00108      *
00109      * \param width new width.
00110      * \param height new height.
00111      */
00112     virtual void resize( int width, int height );
00113 
00114     /**
00115      * Close the viewer, but wait for the rendering thread to finish.
00116      */
00117     virtual void close();
00118 
00119     /**
00120      * Getter for OpenSceneGraph View instance.
00121      *
00122      * \return the OSG Viewer instance.
00123      */
00124 #ifdef WGEMODE_SINGLETHREADED
00125     osg::ref_ptr<osgViewer::Viewer> getView();
00126 #else
00127     osg::ref_ptr<osgViewer::View> getView();
00128 #endif
00129 
00130     /**
00131      * Resets the view using the installed manipulator.
00132      */
00133     void reset();
00134 
00135     /**
00136      * Sets the camera manipulator to use.
00137      *
00138      * \param manipulator the manipulator to use.
00139      */
00140     void setCameraManipulator( osg::ref_ptr<osgGA::MatrixManipulator> manipulator );
00141 
00142     /**
00143      * Returns current active camera manipulator
00144      *
00145      * \return the active camera manipulator.
00146      */
00147     osg::ref_ptr<osgGA::MatrixManipulator> getCameraManipulator();
00148 
00149     /**
00150      * Sets the current camera.
00151      *
00152      * \param camera the OSG camera instance.
00153      */
00154     void setCamera( osg::ref_ptr<osg::Camera> camera );
00155 
00156     /**
00157      * Returns the camera currently in use.
00158      *
00159      * \return the camera currently in use.
00160      */
00161     osg::ref_ptr<osg::Camera> getCamera();
00162 
00163     /**
00164      * Sets the scene graph node to be used for rendering.
00165      *
00166      * \param node part of the scene graph
00167      */
00168     void setScene( osg::ref_ptr< WGEGroupNode > node );
00169 
00170     /**
00171      * Returns the currently set OSG node.
00172      *
00173      * \return the node.
00174      */
00175     osg::ref_ptr< WGEGroupNode > getScene();
00176 
00177     /**
00178      * Returns the name of the viewer.
00179      *
00180      * \return the name
00181      */
00182     std::string getName() const;
00183 
00184     /**
00185      * Determine the color of the viewer's background.
00186      * \param bgColor the new background color
00187      */
00188     void setBgColor( const WColor& bgColor );
00189 
00190     /**
00191      * Getter for the pick handler
00192      *
00193      * \return the pick handler
00194      */
00195     osg::ref_ptr< WPickHandler > getPickHandler();
00196 
00197     /**
00198      * Queries the OpenGL vendor info.
00199      *
00200      * \return Vendor string.
00201      */
00202     std::string getOpenGLVendor() const;
00203 
00204     /**
00205      * Returns the flag which denotes whether a frame was rendered.
00206      *
00207      * \return the flag.
00208      */
00209     WBoolFlag::SPtr isFrameRendered() const;
00210 
00211     /**
00212      * Returns the main cameras screen capture callback.
00213      *
00214      * \return the screen capture callback.
00215      */
00216     WGEScreenCapture::RefPtr getScreenCapture() const;
00217 
00218     /**
00219      * The (de-)activates the animation mode. In animation mode, a special camera manipulator is used instead of the currently set. This
00220      * manipulator can then play some animation path in realtime, frame-rate independent or in frame-per-frame mode which is useful if combined
00221      * with the getScreenCapture() record function.
00222      *
00223      * If animation mode is turned off again, the previously set manipulator / camera setting is restored.
00224      *
00225      * \note do not modify camera or camera manipulator manually while in animation mode.
00226      *
00227      * \param on true to turn on.
00228      *
00229      * \return the animation manipulator. This, and only this should be used to provide the animation.
00230      */
00231     WGEAnimationManipulator::RefPtr animationMode( bool on = true );
00232 
00233     /**
00234      * Checks if the viewer is in animation mode.
00235      *
00236      * \return true if in animation mode
00237      */
00238     bool isAnimationMode() const;
00239 
00240 protected:
00241     /**
00242      * The OpenSceneGraph view used in this (Composite)Viewer.
00243      */
00244 #ifdef WGEMODE_SINGLETHREADED
00245     osg::ref_ptr< osgViewer::Viewer > m_View;
00246 #else
00247     osg::ref_ptr< osgViewer::View > m_View;
00248 #endif
00249 
00250     /**
00251      * The name of the viewer.
00252      */
00253     std::string m_name;
00254 
00255     /**
00256      * Pointer to the pick handler of the viewer.
00257      */
00258     osg::ref_ptr<WPickHandler> m_pickHandler;
00259 
00260     /**
00261      * reference to the scene which is displayed by viewer
00262      */
00263     osg::ref_ptr< WGEGroupNode > m_scene;
00264 
00265     /**
00266      * This flag is true and notifies after the first rendered frame.
00267      */
00268     WBoolFlag::SPtr m_rendered;
00269 
00270     /**
00271      * Small class used for querying glGet info during rendering.
00272      */
00273     class QueryCallback: public osg::Camera::DrawCallback
00274     {
00275     public:
00276         /**
00277          * Constructor. Automatically de-registers from camera after one run.
00278          *
00279          * \param camera the cam to which this was registered
00280          * \param run notifies the flag when run.
00281          */
00282         QueryCallback( osg::ref_ptr<osg::Camera> camera, WBoolFlag::SPtr run );
00283 
00284         /**
00285          * Destructor.
00286          */
00287         virtual ~QueryCallback();
00288 
00289         /**
00290          * Query operator.
00291          *
00292          * \param renderInfo render info object
00293          */
00294         virtual void operator()( osg::RenderInfo& renderInfo ) const;   // NOLINT - this is OSG API
00295 
00296         /**
00297          * Returns the queried vendor string.
00298          *
00299          * \return the vendor
00300          */
00301         std::string getVendor() const;
00302 
00303     protected:
00304         /**
00305          * The vendor string.
00306          */
00307         mutable std::string m_vendor;
00308 
00309         /**
00310          * True if callback was run once.
00311          */
00312         WBoolFlag::SPtr m_run;
00313 
00314         /**
00315          * The camera to which this was connected.
00316          */
00317         osg::ref_ptr<osg::Camera> m_camera;
00318     };
00319 
00320     /**
00321      * The callback used for querying OpenGL features
00322      */
00323     osg::ref_ptr< QueryCallback > m_queryCallback;
00324 
00325 
00326     /**
00327      * The screen capture callback.
00328      */
00329     WGEScreenCapture::RefPtr m_screenCapture;
00330 
00331     /**
00332      * True -> animation mode on.
00333      */
00334     bool m_inAnimationMode;
00335 
00336     /**
00337      * The manipulator that was set before entering animation mode. Null if not in animation mode.
00338      */
00339     osg::ref_ptr<osgGA::MatrixManipulator> m_animationModeManipulatorBackup;
00340 
00341 private:
00342 };
00343 
00344 #endif  // WGEVIEWER_H