WGEViewer.h 9.44 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <string>

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

#include <osg/Node>
#include <osg/Version>
#include <osgGA/DriveManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/TerrainManipulator>
#include <osgGA/UFOManipulator>
#include <osgViewer/View>

// 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"
55
#include "../common/WFlag.h"
56 57 58 59 60
#include "../common/WThreadedRunner.h"
#include "WExportWGE.h"
#include "WGECamera.h"
#include "WGEGraphicsWindow.h"
#include "WGEGroupNode.h"
61
#include "WGEScreenCapture.h"
62
#include "WPickHandler.h"
63
#include "animation/WGEAnimationManipulator.h"
64 65 66 67 68 69 70 71 72 73

/**
 * 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
 */
class WGE_EXPORT WGEViewer: public WGEGraphicsWindow,
                            public boost::enable_shared_from_this< WGEViewer >
{
public:
74 75 76 77 78 79 80 81 82 83
    /**
     * Convenience typedef
     */
    typedef boost::shared_ptr< WGEViewer > SPtr;

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

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    /**
     * 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.
     * \param projectionMode Projection mode of the viewer.
     * \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();
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    /**
     * 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.
     */
#ifdef __APPLE__
    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.
     */
    void setCamera( osg::ref_ptr<osg::Camera> camera );

    /**
     * Returns the camera currently in use.
     *
     * \return the camera currently in use.
     */
    osg::ref_ptr<osg::Camera> getCamera();

    /**
     * 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.
     * \param bgColor the new background color
     */
    void setBgColor( const WColor& bgColor );

    /**
     * Getter for the pick handler
     *
     * \return the pick handler
     */
    osg::ref_ptr< WPickHandler > getPickHandler();

200 201 202 203 204 205 206 207 208 209 210 211 212 213
    /**
     * 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;

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    /**
     * 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;

243 244 245 246 247 248
protected:
    /**
     * The OpenSceneGraph view used in this (Composite)Viewer.
     */
#ifdef __APPLE__
    osg::ref_ptr< osgViewer::Viewer > m_View;
249
#else
250
    osg::ref_ptr< osgViewer::View > m_View;
251
#endif
ebaum's avatar
ebaum committed
252

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    /**
     * 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;

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
    /**
     * This flag is true and notifies after the first rendered frame.
     */
    WBoolFlag::SPtr m_rendered;

    /**
     * Small class used for querying glGet info during rendering.
     */
    class QueryCallback: public osg::Camera::DrawCallback
    {
    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.
         */
        QueryCallback( osg::ref_ptr<osg::Camera> camera, WBoolFlag::SPtr run );

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

        /**
         * Query operator.
         *
         * \param renderInfo render info object
         */
297
        virtual void operator()( osg::RenderInfo& renderInfo ) const;   // NOLINT - this is OSG API
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

        /**
         * 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.
         */
        osg::ref_ptr<osg::Camera> m_camera;
    };

    /**
     * The callback used for querying OpenGL features
     */
    osg::ref_ptr< QueryCallback > m_queryCallback;
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343


    /**
     * 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;

344 345 346
private:
};

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