WGEViewer.h 9.34 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 "WExportWGE.h"
#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 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
 */
class WGE_EXPORT WGEViewer: public WGEGraphicsWindow,
                            public boost::enable_shared_from_this< WGEViewer >
{
public:
71 72 73 74 75 76 77 78 79 80
    /**
     * Convenience typedef
     */
    typedef boost::shared_ptr< WGEViewer > SPtr;

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

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    /**
     * 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();
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    /**
     * 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.
     */
124
#ifdef WGEMODE_SINGLETHREADED
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
    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();

197 198 199 200 201 202 203 204 205 206 207 208 209 210
    /**
     * 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;

211 212 213 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
    /**
     * 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;

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

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    /**
     * 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;

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
    /**
     * 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
         */
294
        virtual void operator()( osg::RenderInfo& renderInfo ) const;   // NOLINT - this is OSG API
295 296 297 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

        /**
         * 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;
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340


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

341 342 343
private:
};

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