WGEViewer.cpp 10.3 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
//
// 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/>.
//
//---------------------------------------------------------------------------

25 26 27 28 29
#include <string>
#include <iostream>

#include <osg/ShapeDrawable>
#include <osg/Geode>
30
#include <osg/Camera>
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/UFOManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include <osgViewer/ViewerEventHandlers>
#include <osgViewer/View>

#include <osgDB/ReadFile>

#include "exceptions/WGEInitFailed.h"
#include "WGE2DManipulator.h"
46
#include "WGEGroupNode.h"
47
#include "WGENoOpManipulator.h"
48 49
#include "WGEZoomTrackballManipulator.h"
#include "WPickHandler.h"
50

51
#include "../common/WConditionOneShot.h"
52 53
#include "../common/WThreadedRunner.h"

ebaum's avatar
ebaum committed
54
#include "WGEViewer.h"
55 56 57 58 59

WGEViewer::WGEViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y,
    int width, int height, WGECamera::ProjectionMode projectionMode )
    : WGEGraphicsWindow( wdata, x, y, width, height ),
      boost::enable_shared_from_this< WGEViewer >(),
60
      m_name( name ),
61 62
      m_rendered( WBoolFlag::SPtr( new WBoolFlag( new WConditionOneShot(), false ) ) ),
      m_screenCapture( new WGEScreenCapture() ),
63 64 65 66
      m_inAnimationMode( false ),
      m_effectHorizon( new WGEViewerEffectHorizon() ),
      m_effectVignette( new WGEViewerEffectVignette() ),
      m_effectImageOverlay( new WGEViewerEffectImageOverlay() )
67 68 69
{
    try
    {
70
#ifdef WGEMODE_MULTITHREADED
71 72 73 74 75 76 77
        m_View = osg::ref_ptr<osgViewer::View>( new osgViewer::View );
#else
        // on mac, this is a viewer!
        m_View = osg::ref_ptr<osgViewer::Viewer>( new osgViewer::Viewer );
#endif

        m_View->setCamera( new WGECamera( width, height, projectionMode ) );
78 79
        m_queryCallback = new QueryCallback( m_View->getCamera(), m_rendered );
        m_View->getCamera()->setInitialDrawCallback( m_queryCallback );
80
        m_View->getCamera()->setFinalDrawCallback( m_screenCapture );
81

82
#ifdef WGEMODE_MULTITHREADED
83 84 85 86 87 88 89 90 91 92
        m_View->getCamera()->setGraphicsContext( m_GraphicsContext.get() );
#else
        m_View->getCamera()->setGraphicsContext( m_GraphicsWindow.get() );
#endif

        switch( projectionMode )
        {
            case( WGECamera::ORTHOGRAPHIC ):
                m_pickHandler = new WPickHandler( name );
                m_View->addEventHandler( m_pickHandler );
93
                if( name != std::string( "Main View" ) )
94 95 96 97 98 99 100 101 102 103 104
                    break;
            case( WGECamera::PERSPECTIVE ):
                // camera manipulator
                m_View->setCameraManipulator( new WGEZoomTrackballManipulator() );

                m_View->setLightingMode( osg::View::HEADLIGHT ); // this is the default anyway

                break;
            case( WGECamera::TWO_D ):
                // no manipulators nor gui handlers
                break;
105 106 107 108
            case( WGECamera::TWO_D_UNIT ):
                // use no-op handler by default
                m_View->setCameraManipulator( new WGENoOpManipulator() );
                break;
109 110 111 112 113 114
            default:
                throw WGEInitFailed( std::string( "Unknown projection mode" ) );
        }

        // add the stats handler
        m_View->addEventHandler( new osgViewer::StatsHandler );
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

        // Properties of the view. Collects props of the effects and similar
        m_properties = boost::shared_ptr< WProperties >( new WProperties( "Properties", "The view's properties" ) );
        m_bgColor = m_properties->addProperty( "Background Color", "Default background color if not overwritten by a camera effect.",
                                               defaultColor::WHITE,
                                               boost::bind( &WGEViewer::updateBgColor, this ) );
        m_throwing = m_properties->addProperty( "Throwing", "If checked, you can grab the scene and throw it. It will keep the rotation impulse.",
                                                false,
                                                boost::bind( &WGEViewer::updateThrowing, this ) );

        WPropGroup effects = m_properties->addPropertyGroup( "Camera Effects", "Several effects that to not depend on any scene content." );
        effects->addProperty( m_effectHorizon->getProperties() );
        effects->addProperty( m_effectVignette->getProperties() );
        effects->addProperty( m_effectImageOverlay->getProperties() );

        // apply the above default
        updateThrowing();
        updateBgColor();
133 134 135 136 137 138 139 140 141 142 143 144 145
    }
    catch( ... )
    {
        throw WGEInitFailed( std::string( "Initialization of WGEViewer failed" ) );
    }
}

WGEViewer::~WGEViewer()
{
    // cleanup
    close();
}

146
#ifdef WGEMODE_SINGLETHREADED
147 148 149 150 151 152 153 154 155 156 157
osg::ref_ptr<osgViewer::Viewer>
#else
osg::ref_ptr<osgViewer::View>
#endif
WGEViewer::getView()
{
    return m_View;
}

void WGEViewer::setCameraManipulator( osg::ref_ptr<osgGA::MatrixManipulator> manipulator )
{
158
    if( !m_inAnimationMode )
159 160 161
    {
        m_View->setCameraManipulator( manipulator );
    }
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
}

osg::ref_ptr<osgGA::MatrixManipulator> WGEViewer::getCameraManipulator()
{
    return m_View->getCameraManipulator();
}

void WGEViewer::setCamera( osg::ref_ptr<osg::Camera> camera )
{
    m_View->setCamera( camera );
    // redraw request?? No since it redraws permanently and uses the new settings
}

osg::ref_ptr<osg::Camera> WGEViewer::getCamera()
{
    return m_View->getCamera();
}

void WGEViewer::setScene( osg::ref_ptr< WGEGroupNode > node )
{
182 183
    m_effectImageOverlay->setReferenceViewer( shared_from_this() );

184 185
    m_View->setSceneData( node );
    m_scene = node;
186

187 188 189 190
    // add effects:
    node->insert( m_effectVignette );
    node->insert( m_effectImageOverlay );
    node->insert( m_effectHorizon );
191 192 193 194 195 196 197
}

osg::ref_ptr< WGEGroupNode > WGEViewer::getScene()
{
    return m_scene;
}

198 199 200 201 202 203 204 205 206 207 208 209 210 211
void WGEViewer::updateThrowing()
{
    WGEZoomTrackballManipulator* manipulator = dynamic_cast< WGEZoomTrackballManipulator* >( getCameraManipulator().get() );
    if( manipulator )
    {
        manipulator->setThrow( m_throwing->get() );
    }
}

void WGEViewer::updateBgColor()
{
    m_View->getCamera()->setClearColor( m_bgColor->get() );
}

212 213
void WGEViewer::setBgColor( const WColor& bgColor )
{
214 215 216 217 218 219
    m_bgColor->set( bgColor );
}

WColor WGEViewer::getBgColor() const
{
    return m_bgColor->get();
220 221 222 223
}

void WGEViewer::paint()
{
224
#ifdef WGEMODE_SINGLETHREADED
225 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 255 256 257 258 259 260 261 262 263 264
    m_View->frame();
#endif
}

void WGEViewer::resize( int width, int height )
{
    m_View->getEventQueue()->windowResize( 0, 0, width, height );

    WGEGraphicsWindow::resize( width, height );

    // also update the camera
    m_View->getCamera()->setViewport( 0, 0, width, height );
    WGECamera* camera = dynamic_cast< WGECamera* >( m_View->getCamera() );
    if( camera )
    {
        camera->resize();
    }
}

void WGEViewer::close()
{
    // forward close event
    WGEGraphicsWindow::close();
}

std::string WGEViewer::getName() const
{
    return m_name;
}

osg::ref_ptr< WPickHandler > WGEViewer::getPickHandler()
{
    return m_pickHandler;
}

void WGEViewer::reset()
{
    m_View->home();
}

265 266 267 268 269
WGEScreenCapture::RefPtr WGEViewer::getScreenCapture() const
{
    return m_screenCapture;
}

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 297 298 299 300 301 302 303 304 305 306 307
std::string WGEViewer::getOpenGLVendor() const
{
    return m_queryCallback->getVendor();
}

WBoolFlag::SPtr WGEViewer::isFrameRendered() const
{
    return m_rendered;
}

WGEViewer::QueryCallback::QueryCallback( osg::ref_ptr<osg::Camera> camera, WBoolFlag::SPtr run ):
    m_vendor( "" ),
    m_run( run ),
    m_camera( camera )
{
    // init
}

WGEViewer::QueryCallback::~QueryCallback()
{
    // cleanup
}

void WGEViewer::QueryCallback::operator()( osg::RenderInfo& /* renderInfo */ ) const
{
    const GLubyte* vendor = glGetString( GL_VENDOR );
    m_vendor = reinterpret_cast< const char* >( vendor );

    // job done. De-register.
    m_camera->setInitialDrawCallback( NULL );
    m_run->set( true );
}

std::string WGEViewer::QueryCallback::getVendor() const
{
    return m_vendor;
}

308 309
WGEAnimationManipulator::RefPtr WGEViewer::animationMode( bool on )
{
310
    if( m_inAnimationMode && !on ) // turn off mode
311 312 313 314 315 316 317
    {
        m_inAnimationMode = false;

        // restore old manipulator
        m_View->setCameraManipulator( m_animationModeManipulatorBackup );
        return NULL;
    }
318
    else if( !m_inAnimationMode && on ) // turn on
319 320 321 322 323 324 325 326 327 328 329
    {
        m_inAnimationMode = true;

        // backup
        m_animationModeManipulatorBackup = getCameraManipulator();

        // create animation manipulator
        WGEAnimationManipulator::RefPtr anim = new WGEAnimationManipulator();
        m_View->setCameraManipulator( anim );
        return anim;
    }
330
    else if( m_inAnimationMode ) // already on
331 332 333 334 335 336 337 338 339 340 341 342 343
    {
        return dynamic_cast< WGEAnimationManipulator* >( getCameraManipulator().get() );
    }

    // else: do nothing
    return NULL;
}

bool WGEViewer::isAnimationMode() const
{
    return m_inAnimationMode;
}

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
WGEViewerEffectHorizon::SPtr WGEViewer::getBackground()
{
    return m_effectHorizon;
}

WGEViewerEffectImageOverlay::SPtr WGEViewer::getImageOverlay()
{
    return m_effectImageOverlay;
}

WGEViewerEffectVignette::SPtr WGEViewer::getVignette()
{
    return m_effectVignette;
}

WGEViewerEffectHorizon::ConstSPtr WGEViewer::getBackground() const
{
    return m_effectHorizon;
}

WGEViewerEffectImageOverlay::ConstSPtr WGEViewer::getImageOverlay() const
{
    return m_effectImageOverlay;
}

WGEViewerEffectVignette::ConstSPtr WGEViewer::getVignette() const
{
    return m_effectVignette;
}
373 374 375 376 377

WProperties::SPtr WGEViewer::getProperties() const
{
    return m_properties;
}