WGEViewer.h 12.7 KB
Newer Older
ebaum's avatar
ebaum committed
1 2
//---------------------------------------------------------------------------
//
3
// Project: OpenWalnut ( http://www.openwalnut.org )
ebaum's avatar
ebaum committed
4
//
5 6
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
ebaum's avatar
ebaum committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#ifndef WGEVIEWER_H
#define WGEVIEWER_H

28 29 30 31 32 33 34 35
#include <string>

#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>

#include <osg/Node>
#include <osg/Version>
#include <osgViewer/View>
36
#include <osgViewer/Viewer>
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

// OSG interface changed in 2.9.7, to make it compile also with those versions we do this:
// OSG_MIN_VERSION_REQUIRED(2, 9, 8) macro is not available in e.g. OSG 2.8.1, hence we use the old way
#if ( ( OPENSCENEGRAPH_MAJOR_VERSION > 2 ) || ( OPENSCENEGRAPH_MAJOR_VERSION == 2 && ( OPENSCENEGRAPH_MINOR_VERSION > 9 || \
                            ( OPENSCENEGRAPH_MINOR_VERSION == 9 && OPENSCENEGRAPH_PATCH_VERSION >= 8 ) ) ) )
    #include <osgGA/CameraManipulator>
    namespace osgGA
    {
        typedef CameraManipulator MatrixManipulator;
    }
#else
    #include <osgGA/MatrixManipulator>
#endif

#include "../common/WColor.h"
52
#include "../common/WFlag.h"
53

54 55
#include "WGECamera.h"
#include "WGEGraphicsWindow.h"
56
#include "WGraphicsEngineMode.h"
57
class WGEGroupNode;
58
#include "WGEScreenCapture.h"
59
class WPickHandler;
60
#include "animation/WGEAnimationManipulator.h"
61

62 63 64 65
#include "WGEViewerEffectHorizon.h"
#include "WGEViewerEffectVignette.h"
#include "WGEViewerEffectImageOverlay.h"

66 67 68 69 70
/**
 * Class for managing one view to the scene. This includes viewport, camera and graphics context.
 * It is, besides WGraphicsEngine, the ONLY entry point for each widget for accessing the graphics engine.
 * \ingroup ge
 */
71
class WGEViewer: public WGEGraphicsWindow,
72
                 public boost::enable_shared_from_this< WGEViewer >
73 74
{
public:
75 76 77 78 79 80 81 82 83 84
    /**
     * Convenience typedef
     */
    typedef boost::shared_ptr< WGEViewer > SPtr;

    /**
     * Convenience typedef
     */
    typedef boost::shared_ptr< const WGEViewer > ConstSPtr;

85 86 87 88 89 90 91 92 93
    /**
     * Default constructor.
     *
     * \param name the name of the viewer
     * \param wdata the WindowData instance for the widget to use as render widget
     * \param x X coordinate of widget where to create the context.
     * \param y Y coordinate of widget where to create the context.
     * \param width Width of the widget.
     * \param height Height of the Widget.
94
     * \param projectionMode Projection mode of the viewer. Currently only the orthographic mode allows the use of a pick handler.
95 96 97 98 99 100 101 102 103
     * \exception WGEInitFailed thrown if initialization of graphics context or graphics window has failed.
     */
    WGEViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y, int width, int height,
        WGECamera::ProjectionMode projectionMode = WGECamera::ORTHOGRAPHIC );

    /**
     * Destructor.
     */
    virtual ~WGEViewer();
104

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    /**
     * Repaints the contents. Mac only.
     */
    virtual void paint();

    /**
     * Updates size information. Also updates camera.
     *
     * \param width new width.
     * \param height new height.
     */
    virtual void resize( int width, int height );

    /**
     * Close the viewer, but wait for the rendering thread to finish.
     */
    virtual void close();

    /**
     * Getter for OpenSceneGraph View instance.
     *
     * \return the OSG Viewer instance.
     */
128
#ifdef WGEMODE_SINGLETHREADED
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    osg::ref_ptr<osgViewer::Viewer> getView();
#else
    osg::ref_ptr<osgViewer::View> getView();
#endif

    /**
     * Resets the view using the installed manipulator.
     */
    void reset();

    /**
     * Sets the camera manipulator to use.
     *
     * \param manipulator the manipulator to use.
     */
    void setCameraManipulator( osg::ref_ptr<osgGA::MatrixManipulator> manipulator );

    /**
     * Returns current active camera manipulator
     *
     * \return the active camera manipulator.
     */
    osg::ref_ptr<osgGA::MatrixManipulator> getCameraManipulator();

    /**
     * Sets the current camera.
     *
     * \param camera the OSG camera instance.
     */
158
    void setCamera( osg::ref_ptr< WGECamera > camera );
159 160 161 162 163 164

    /**
     * Returns the camera currently in use.
     *
     * \return the camera currently in use.
     */
165
    osg::ref_ptr< WGECamera > getCamera();
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

    /**
     * Sets the scene graph node to be used for rendering.
     *
     * \param node part of the scene graph
     */
    void setScene( osg::ref_ptr< WGEGroupNode > node );

    /**
     * Returns the currently set OSG node.
     *
     * \return the node.
     */
    osg::ref_ptr< WGEGroupNode > getScene();

    /**
     * Returns the name of the viewer.
     *
     * \return the name
     */
    std::string getName() const;

    /**
     * Determine the color of the viewer's background.
190 191 192
     *
     * \note This is only useful when the background effect is disabled.
     *
193 194 195 196
     * \param bgColor the new background color
     */
    void setBgColor( const WColor& bgColor );

197 198 199 200 201 202 203
    /**
     * Returns the current default background color. This color is only visible if no camera effect overrides it.
     *
     * \return The color.
     */
    WColor getBgColor() const;

204 205
    /**
     * Getter for the pick handler
206
     * Warning: At the moment only the orthographic projection mode supports a pick handler.
207 208 209 210 211
     *
     * \return the pick handler
     */
    osg::ref_ptr< WPickHandler > getPickHandler();

212 213 214 215 216 217 218 219 220 221 222 223 224 225
    /**
     * Queries the OpenGL vendor info.
     *
     * \return Vendor string.
     */
    std::string getOpenGLVendor() const;

    /**
     * Returns the flag which denotes whether a frame was rendered.
     *
     * \return the flag.
     */
    WBoolFlag::SPtr isFrameRendered() const;

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    /**
     * Returns the main cameras screen capture callback.
     *
     * \return the screen capture callback.
     */
    WGEScreenCapture::RefPtr getScreenCapture() const;

    /**
     * The (de-)activates the animation mode. In animation mode, a special camera manipulator is used instead of the currently set. This
     * manipulator can then play some animation path in realtime, frame-rate independent or in frame-per-frame mode which is useful if combined
     * with the getScreenCapture() record function.
     *
     * If animation mode is turned off again, the previously set manipulator / camera setting is restored.
     *
     * \note do not modify camera or camera manipulator manually while in animation mode.
     *
     * \param on true to turn on.
     *
     * \return the animation manipulator. This, and only this should be used to provide the animation.
     */
    WGEAnimationManipulator::RefPtr animationMode( bool on = true );

    /**
     * Checks if the viewer is in animation mode.
     *
     * \return true if in animation mode
     */
    bool isAnimationMode() const;

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    /**
     * Return the background render effect for modification.
     *
     * \return the effect
     */
    WGEViewerEffectHorizon::SPtr getBackground();

    /**
     * Return the overlay render effect for modification.
     *
     * \return the effect
     */
    WGEViewerEffectImageOverlay::SPtr getImageOverlay();

    /**
     * Return the vignette render effect for modification.
     *
     * \return the effect
     */
    WGEViewerEffectVignette::SPtr getVignette();

    /**
     * Return the background render effect for modification.
     *
     * \return the effect
     */
    WGEViewerEffectHorizon::ConstSPtr getBackground() const;

    /**
     * Return the overlay render effect for modification.
     *
     * \return the effect
     */
    WGEViewerEffectImageOverlay::ConstSPtr getImageOverlay() const;

    /**
     * Return the vignette render effect for modification.
     *
     * \return the effect
     */
    WGEViewerEffectVignette::ConstSPtr getVignette() const;

297 298 299 300 301 302 303
    /**
     * Activate viewer effects by default. If the user has deactivated them, this method does not cause any change.
     *
     * \param activeByDefault if true all effects are active by default
     */
    void setEffectsActiveDefault( bool activeByDefault = true );

304 305 306 307 308 309 310
    /**
     * Return a pointer to the properties object of the view.
     *
     * \return the properties.
     */
    WProperties::SPtr getProperties() const;

311
    /**
Sebastian Eichelbaum's avatar
[DOC]  
Sebastian Eichelbaum committed
312 313
     * Pause rendering. This does not free the viewer. It simply pauses rendering. Be warned. This does not necessarily improve performance with
     * multiple views. This depends on the osgViewer components used and the underlying GUI (i.e. Qt).
314 315 316 317 318 319 320 321 322 323 324
     *
     * \param pause true to pause.
     */
    void setPaused( bool pause = true );

    /**
     * Query whether the view is paused or not.
     *
     * \return true if paused
     */
    bool getPaused() const;
325 326 327 328
protected:
    /**
     * The OpenSceneGraph view used in this (Composite)Viewer.
     */
329
#ifdef WGEMODE_SINGLETHREADED
330
    osg::ref_ptr< osgViewer::Viewer > m_View;
331
#else
332
    osg::ref_ptr< osgViewer::View > m_View;
333
#endif
ebaum's avatar
ebaum committed
334

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
    /**
     * The name of the viewer.
     */
    std::string m_name;

    /**
     * Pointer to the pick handler of the viewer.
     */
    osg::ref_ptr<WPickHandler> m_pickHandler;

    /**
     * reference to the scene which is displayed by viewer
     */
    osg::ref_ptr< WGEGroupNode > m_scene;

350 351 352 353 354
    /**
     * Keep the currently set scene node. Unlike m_scene, it is the user set node.
     */
    osg::ref_ptr< WGEGroupNode > m_sceneMainNode;

355 356 357 358 359 360 361 362
    /**
     * This flag is true and notifies after the first rendered frame.
     */
    WBoolFlag::SPtr m_rendered;

    /**
     * Small class used for querying glGet info during rendering.
     */
363
    class QueryCallback: public WGECamera::DrawCallback
364 365 366 367 368 369 370 371
    {
    public:
        /**
         * Constructor. Automatically de-registers from camera after one run.
         *
         * \param camera the cam to which this was registered
         * \param run notifies the flag when run.
         */
372
        QueryCallback( osg::ref_ptr< WGECamera> camera, WBoolFlag::SPtr run );
373 374 375 376 377 378 379 380 381 382 383

        /**
         * Destructor.
         */
        virtual ~QueryCallback();

        /**
         * Query operator.
         *
         * \param renderInfo render info object
         */
384
        virtual void operator()( osg::RenderInfo& renderInfo ) const;   // NOLINT - this is OSG API
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406

        /**
         * Returns the queried vendor string.
         *
         * \return the vendor
         */
        std::string getVendor() const;

    protected:
        /**
         * The vendor string.
         */
        mutable std::string m_vendor;

        /**
         * True if callback was run once.
         */
        WBoolFlag::SPtr m_run;

        /**
         * The camera to which this was connected.
         */
407
        osg::ref_ptr< WGECamera > m_camera;
408 409 410 411 412 413
    };

    /**
     * The callback used for querying OpenGL features
     */
    osg::ref_ptr< QueryCallback > m_queryCallback;
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430


    /**
     * The screen capture callback.
     */
    WGEScreenCapture::RefPtr m_screenCapture;

    /**
     * True -> animation mode on.
     */
    bool m_inAnimationMode;

    /**
     * The manipulator that was set before entering animation mode. Null if not in animation mode.
     */
    osg::ref_ptr<osgGA::MatrixManipulator> m_animationModeManipulatorBackup;

431 432 433 434 435 436 437 438 439 440 441 442 443 444
    /**
     * Horizon effect.
     */
    WGEViewerEffectHorizon::SPtr m_effectHorizon;

    /**
    * Vignette effect.
     */
    WGEViewerEffectVignette::SPtr m_effectVignette;

    /**
     * Image overlay effect.
     */
    WGEViewerEffectImageOverlay::SPtr m_effectImageOverlay;
445 446 447 448 449

    /**
     * The property object for the view.
     */
    WProperties::SPtr m_properties;
450
private:
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    /**
     * The default clear color (bg color).
     */
    WPropColor m_bgColor;

    /**
     * The switch to enable the throw- functionality of some OSG manipulators.
     */
    WPropBool m_throwing;

    /**
     * Update the default clear color (bg color). Called by the m_bgColor property.
     */
    void updateBgColor();

    /**
     * Update throw setting of the manipulator (if supported).
     */
    void updateThrowing();
470 471 472 473 474

    /**
     * Flag denoting whether the view is paused or not
     */
    bool m_paused;
475 476
};

ebaum's avatar
ebaum committed
477
#endif  // WGEVIEWER_H