OpenWalnut  1.4.0
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 #include "WGEViewerEffectHorizon.h"
00063 #include "WGEViewerEffectVignette.h"
00064 #include "WGEViewerEffectImageOverlay.h"
00065 
00066 /**
00067  * Class for managing one view to the scene. This includes viewport, camera and graphics context.
00068  * It is, besides WGraphicsEngine, the ONLY entry point for each widget for accessing the graphics engine.
00069  * \ingroup ge
00070  */
00071 class WGEViewer: public WGEGraphicsWindow,
00072                  public boost::enable_shared_from_this< WGEViewer >
00073 {
00074 public:
00075     /**
00076      * Convenience typedef
00077      */
00078     typedef boost::shared_ptr< WGEViewer > SPtr;
00079 
00080     /**
00081      * Convenience typedef
00082      */
00083     typedef boost::shared_ptr< const WGEViewer > ConstSPtr;
00084 
00085     /**
00086      * Default constructor.
00087      *
00088      * \param name the name of the viewer
00089      * \param wdata the WindowData instance for the widget to use as render widget
00090      * \param x X coordinate of widget where to create the context.
00091      * \param y Y coordinate of widget where to create the context.
00092      * \param width Width of the widget.
00093      * \param height Height of the Widget.
00094      * \param projectionMode Projection mode of the viewer.
00095      * \exception WGEInitFailed thrown if initialization of graphics context or graphics window has failed.
00096      */
00097     WGEViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y, int width, int height,
00098         WGECamera::ProjectionMode projectionMode = WGECamera::ORTHOGRAPHIC );
00099 
00100     /**
00101      * Destructor.
00102      */
00103     virtual ~WGEViewer();
00104 
00105     /**
00106      * Repaints the contents. Mac only.
00107      */
00108     virtual void paint();
00109 
00110     /**
00111      * Updates size information. Also updates camera.
00112      *
00113      * \param width new width.
00114      * \param height new height.
00115      */
00116     virtual void resize( int width, int height );
00117 
00118     /**
00119      * Close the viewer, but wait for the rendering thread to finish.
00120      */
00121     virtual void close();
00122 
00123     /**
00124      * Getter for OpenSceneGraph View instance.
00125      *
00126      * \return the OSG Viewer instance.
00127      */
00128 #ifdef WGEMODE_SINGLETHREADED
00129     osg::ref_ptr<osgViewer::Viewer> getView();
00130 #else
00131     osg::ref_ptr<osgViewer::View> getView();
00132 #endif
00133 
00134     /**
00135      * Resets the view using the installed manipulator.
00136      */
00137     void reset();
00138 
00139     /**
00140      * Sets the camera manipulator to use.
00141      *
00142      * \param manipulator the manipulator to use.
00143      */
00144     void setCameraManipulator( osg::ref_ptr<osgGA::MatrixManipulator> manipulator );
00145 
00146     /**
00147      * Returns current active camera manipulator
00148      *
00149      * \return the active camera manipulator.
00150      */
00151     osg::ref_ptr<osgGA::MatrixManipulator> getCameraManipulator();
00152 
00153     /**
00154      * Sets the current camera.
00155      *
00156      * \param camera the OSG camera instance.
00157      */
00158     void setCamera( osg::ref_ptr< WGECamera > camera );
00159 
00160     /**
00161      * Returns the camera currently in use.
00162      *
00163      * \return the camera currently in use.
00164      */
00165     osg::ref_ptr< WGECamera > getCamera();
00166 
00167     /**
00168      * Sets the scene graph node to be used for rendering.
00169      *
00170      * \param node part of the scene graph
00171      */
00172     void setScene( osg::ref_ptr< WGEGroupNode > node );
00173 
00174     /**
00175      * Returns the currently set OSG node.
00176      *
00177      * \return the node.
00178      */
00179     osg::ref_ptr< WGEGroupNode > getScene();
00180 
00181     /**
00182      * Returns the name of the viewer.
00183      *
00184      * \return the name
00185      */
00186     std::string getName() const;
00187 
00188     /**
00189      * Determine the color of the viewer's background.
00190      *
00191      * \note This is only useful when the background effect is disabled.
00192      *
00193      * \param bgColor the new background color
00194      */
00195     void setBgColor( const WColor& bgColor );
00196 
00197     /**
00198      * Returns the current default background color. This color is only visible if no camera effect overrides it.
00199      *
00200      * \return The color.
00201      */
00202     WColor getBgColor() const;
00203 
00204     /**
00205      * Getter for the pick handler
00206      *
00207      * \return the pick handler
00208      */
00209     osg::ref_ptr< WPickHandler > getPickHandler();
00210 
00211     /**
00212      * Queries the OpenGL vendor info.
00213      *
00214      * \return Vendor string.
00215      */
00216     std::string getOpenGLVendor() const;
00217 
00218     /**
00219      * Returns the flag which denotes whether a frame was rendered.
00220      *
00221      * \return the flag.
00222      */
00223     WBoolFlag::SPtr isFrameRendered() const;
00224 
00225     /**
00226      * Returns the main cameras screen capture callback.
00227      *
00228      * \return the screen capture callback.
00229      */
00230     WGEScreenCapture::RefPtr getScreenCapture() const;
00231 
00232     /**
00233      * The (de-)activates the animation mode. In animation mode, a special camera manipulator is used instead of the currently set. This
00234      * manipulator can then play some animation path in realtime, frame-rate independent or in frame-per-frame mode which is useful if combined
00235      * with the getScreenCapture() record function.
00236      *
00237      * If animation mode is turned off again, the previously set manipulator / camera setting is restored.
00238      *
00239      * \note do not modify camera or camera manipulator manually while in animation mode.
00240      *
00241      * \param on true to turn on.
00242      *
00243      * \return the animation manipulator. This, and only this should be used to provide the animation.
00244      */
00245     WGEAnimationManipulator::RefPtr animationMode( bool on = true );
00246 
00247     /**
00248      * Checks if the viewer is in animation mode.
00249      *
00250      * \return true if in animation mode
00251      */
00252     bool isAnimationMode() const;
00253 
00254     /**
00255      * Return the background render effect for modification.
00256      *
00257      * \return the effect
00258      */
00259     WGEViewerEffectHorizon::SPtr getBackground();
00260 
00261     /**
00262      * Return the overlay render effect for modification.
00263      *
00264      * \return the effect
00265      */
00266     WGEViewerEffectImageOverlay::SPtr getImageOverlay();
00267 
00268     /**
00269      * Return the vignette render effect for modification.
00270      *
00271      * \return the effect
00272      */
00273     WGEViewerEffectVignette::SPtr getVignette();
00274 
00275     /**
00276      * Return the background render effect for modification.
00277      *
00278      * \return the effect
00279      */
00280     WGEViewerEffectHorizon::ConstSPtr getBackground() const;
00281 
00282     /**
00283      * Return the overlay render effect for modification.
00284      *
00285      * \return the effect
00286      */
00287     WGEViewerEffectImageOverlay::ConstSPtr getImageOverlay() const;
00288 
00289     /**
00290      * Return the vignette render effect for modification.
00291      *
00292      * \return the effect
00293      */
00294     WGEViewerEffectVignette::ConstSPtr getVignette() const;
00295 
00296     /**
00297      * Activate viewer effects by default. If the user has deactivated them, this method does not cause any change.
00298      *
00299      * \param activeByDefault if true all effects are active by default
00300      */
00301     void setEffectsActiveDefault( bool activeByDefault = true );
00302 
00303     /**
00304      * Return a pointer to the properties object of the view.
00305      *
00306      * \return the properties.
00307      */
00308     WProperties::SPtr getProperties() const;
00309 
00310 protected:
00311     /**
00312      * The OpenSceneGraph view used in this (Composite)Viewer.
00313      */
00314 #ifdef WGEMODE_SINGLETHREADED
00315     osg::ref_ptr< osgViewer::Viewer > m_View;
00316 #else
00317     osg::ref_ptr< osgViewer::View > m_View;
00318 #endif
00319 
00320     /**
00321      * The name of the viewer.
00322      */
00323     std::string m_name;
00324 
00325     /**
00326      * Pointer to the pick handler of the viewer.
00327      */
00328     osg::ref_ptr<WPickHandler> m_pickHandler;
00329 
00330     /**
00331      * reference to the scene which is displayed by viewer
00332      */
00333     osg::ref_ptr< WGEGroupNode > m_scene;
00334 
00335     /**
00336      * Keep the currently set scene node. Unlike m_scene, it is the user set node.
00337      */
00338     osg::ref_ptr< WGEGroupNode > m_sceneMainNode;
00339 
00340     /**
00341      * This flag is true and notifies after the first rendered frame.
00342      */
00343     WBoolFlag::SPtr m_rendered;
00344 
00345     /**
00346      * Small class used for querying glGet info during rendering.
00347      */
00348     class QueryCallback: public WGECamera::DrawCallback
00349     {
00350     public:
00351         /**
00352          * Constructor. Automatically de-registers from camera after one run.
00353          *
00354          * \param camera the cam to which this was registered
00355          * \param run notifies the flag when run.
00356          */
00357         QueryCallback( osg::ref_ptr< WGECamera> camera, WBoolFlag::SPtr run );
00358 
00359         /**
00360          * Destructor.
00361          */
00362         virtual ~QueryCallback();
00363 
00364         /**
00365          * Query operator.
00366          *
00367          * \param renderInfo render info object
00368          */
00369         virtual void operator()( osg::RenderInfo& renderInfo ) const;   // NOLINT - this is OSG API
00370 
00371         /**
00372          * Returns the queried vendor string.
00373          *
00374          * \return the vendor
00375          */
00376         std::string getVendor() const;
00377 
00378     protected:
00379         /**
00380          * The vendor string.
00381          */
00382         mutable std::string m_vendor;
00383 
00384         /**
00385          * True if callback was run once.
00386          */
00387         WBoolFlag::SPtr m_run;
00388 
00389         /**
00390          * The camera to which this was connected.
00391          */
00392         osg::ref_ptr< WGECamera > m_camera;
00393     };
00394 
00395     /**
00396      * The callback used for querying OpenGL features
00397      */
00398     osg::ref_ptr< QueryCallback > m_queryCallback;
00399 
00400 
00401     /**
00402      * The screen capture callback.
00403      */
00404     WGEScreenCapture::RefPtr m_screenCapture;
00405 
00406     /**
00407      * True -> animation mode on.
00408      */
00409     bool m_inAnimationMode;
00410 
00411     /**
00412      * The manipulator that was set before entering animation mode. Null if not in animation mode.
00413      */
00414     osg::ref_ptr<osgGA::MatrixManipulator> m_animationModeManipulatorBackup;
00415 
00416     /**
00417      * Horizon effect.
00418      */
00419     WGEViewerEffectHorizon::SPtr m_effectHorizon;
00420 
00421     /**
00422     * Vignette effect.
00423      */
00424     WGEViewerEffectVignette::SPtr m_effectVignette;
00425 
00426     /**
00427      * Image overlay effect.
00428      */
00429     WGEViewerEffectImageOverlay::SPtr m_effectImageOverlay;
00430 
00431     /**
00432      * The property object for the view.
00433      */
00434     WProperties::SPtr m_properties;
00435 private:
00436     /**
00437      * The default clear color (bg color).
00438      */
00439     WPropColor m_bgColor;
00440 
00441     /**
00442      * The switch to enable the throw- functionality of some OSG manipulators.
00443      */
00444     WPropBool m_throwing;
00445 
00446     /**
00447      * Update the default clear color (bg color). Called by the m_bgColor property.
00448      */
00449     void updateBgColor();
00450 
00451     /**
00452      * Update throw setting of the manipulator (if supported).
00453      */
00454     void updateThrowing();
00455 };
00456 
00457 #endif  // WGEVIEWER_H