WGEViewer.cpp 10.6 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

#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>
41
#include <osg/Depth>
42 43 44 45 46

#include <osgDB/ReadFile>

#include "exceptions/WGEInitFailed.h"
#include "WGE2DManipulator.h"
47
#include "WGEGroupNode.h"
48
#include "WGENoOpManipulator.h"
49 50
#include "WGEZoomTrackballManipulator.h"
#include "WPickHandler.h"
51 52 53
#include "WGEGeodeUtils.h"
#include "shaders/WGEShader.h"

54
#include "../common/WConditionOneShot.h"
55

56 57
#include "../common/WThreadedRunner.h"

ebaum's avatar
ebaum committed
58
#include "WGEViewer.h"
59 60 61 62 63

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 >(),
64
      m_name( name ),
65 66 67
      m_rendered( WBoolFlag::SPtr( new WBoolFlag( new WConditionOneShot(), false ) ) ),
      m_screenCapture( new WGEScreenCapture() ),
      m_inAnimationMode( false )
68 69 70
{
    try
    {
71
#ifdef WGEMODE_MULTITHREADED
72 73 74 75 76 77 78
        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 ) );
79 80
        m_queryCallback = new QueryCallback( m_View->getCamera(), m_rendered );
        m_View->getCamera()->setInitialDrawCallback( m_queryCallback );
81
        m_View->getCamera()->setFinalDrawCallback( m_screenCapture );
82

83
#ifdef WGEMODE_MULTITHREADED
84 85 86 87 88 89 90 91 92 93
        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 );
94
                if( name != std::string( "Main View" ) )
95 96 97 98 99 100 101 102 103 104 105
                    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;
106 107 108 109
            case( WGECamera::TWO_D_UNIT ):
                // use no-op handler by default
                m_View->setCameraManipulator( new WGENoOpManipulator() );
                break;
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
            default:
                throw WGEInitFailed( std::string( "Unknown projection mode" ) );
        }

        // add the stats handler
        m_View->addEventHandler( new osgViewer::StatsHandler );
    }
    catch( ... )
    {
        throw WGEInitFailed( std::string( "Initialization of WGEViewer failed" ) );
    }
}

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

129
#ifdef WGEMODE_SINGLETHREADED
130 131 132 133 134 135 136 137 138 139 140
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 )
{
141
    if( !m_inAnimationMode )
142 143 144
    {
        m_View->setCameraManipulator( manipulator );
    }
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
}

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 )
{
    m_View->setSceneData( node );
    m_scene = node;
167

168 169
    // vignetting cam
    {
170
        /*
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
        osg::ref_ptr< osg::Camera > bkCam = new osg::Camera();
        bkCam->setClearMask( GL_DEPTH_BUFFER_BIT );
        bkCam->setRenderOrder( osg::Camera::POST_RENDER );
        bkCam->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
        bkCam->setProjectionMatrixAsOrtho2D( 0.0, 1.0, 0.0, 1.0 );
        bkCam->setViewMatrix( osg::Matrixd::identity() );
        osg::StateSet* state = bkCam->getOrCreateStateSet();
        state->setMode( GL_DEPTH_TEST, osg::StateAttribute::OFF );
        state->setMode( GL_LIGHTING, osg::StateAttribute::PROTECTED );
        state->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
        state->setMode( GL_BLEND, osg::StateAttribute::PROTECTED );
        state->setMode( GL_BLEND, osg::StateAttribute::ON );

        osg::ref_ptr< osg::Geode > geode = wge::genFinitePlane( osg::Vec3( 0.0, 0.0, 0.0 ),
                                                                osg::Vec3( 1.0, 0.0, 0.0 ),
                                                                osg::Vec3( 0.0, 1.0, 0.0 ) );

        osg::ref_ptr< WGEShader > vignetteShader = new WGEShader( "WGECameraVignette" );
        vignetteShader->apply( geode );

        // add the slice to the geode
        bkCam->addChild( geode );
        node->insert( bkCam );
194
        */
195
    }
196

197 198
    // horizon background cam
    {
199
        /*
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
        osg::ref_ptr< osg::Camera > bkCam = new osg::Camera();
        bkCam->setClearMask( GL_DEPTH_BUFFER_BIT );
        bkCam->setRenderOrder( osg::Camera::NESTED_RENDER, 0 );
        bkCam->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
        bkCam->setProjectionMatrixAsOrtho2D( 0.0, 1.0, 0.0, 1.0 );
        bkCam->setViewMatrix( osg::Matrixd::identity() );
        osg::StateSet* state = bkCam->getOrCreateStateSet();
        state->setMode( GL_DEPTH_TEST, osg::StateAttribute::OFF );
        state->setMode( GL_LIGHTING, osg::StateAttribute::PROTECTED );
        state->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
        state->setMode( GL_BLEND, osg::StateAttribute::PROTECTED );
        state->setMode( GL_BLEND, osg::StateAttribute::ON );
        osg::Depth* depth = new osg::Depth;
        depth->setWriteMask( false );
        state->setAttributeAndModes( depth, osg::StateAttribute::ON );

        osg::ref_ptr< osg::Geode > geode = wge::genFinitePlane( osg::Vec3( 0.0, 0.0, 0.0 ),
                                                                osg::Vec3( 1.0, 0.0, 0.0 ),
                                                                osg::Vec3( 0.0, 1.0, 0.0 ) );

        osg::ref_ptr< WGEShader > horizonShader = new WGEShader( "WGECameraHorizon" );
        horizonShader->apply( geode );

        // add the slice to the geode
        bkCam->addChild( geode );
        node->insert( bkCam );
226
        */
227
    }
228 229 230 231 232 233 234 235 236 237 238 239 240 241
}

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

void WGEViewer::setBgColor( const WColor& bgColor )
{
    m_View->getCamera()->setClearColor( bgColor );
}

void WGEViewer::paint()
{
242
#ifdef WGEMODE_SINGLETHREADED
243 244 245 246 247 248 249 250 251 252 253 254 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
    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();
}

283 284 285 286 287
WGEScreenCapture::RefPtr WGEViewer::getScreenCapture() const
{
    return m_screenCapture;
}

288 289 290 291 292 293 294 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 324 325
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;
}

326 327
WGEAnimationManipulator::RefPtr WGEViewer::animationMode( bool on )
{
328
    if( m_inAnimationMode && !on ) // turn off mode
329 330 331 332 333 334 335
    {
        m_inAnimationMode = false;

        // restore old manipulator
        m_View->setCameraManipulator( m_animationModeManipulatorBackup );
        return NULL;
    }
336
    else if( !m_inAnimationMode && on ) // turn on
337 338 339 340 341 342 343 344 345 346 347
    {
        m_inAnimationMode = true;

        // backup
        m_animationModeManipulatorBackup = getCameraManipulator();

        // create animation manipulator
        WGEAnimationManipulator::RefPtr anim = new WGEAnimationManipulator();
        m_View->setCameraManipulator( anim );
        return anim;
    }
348
    else if( m_inAnimationMode ) // already on
349 350 351 352 353 354 355 356 357 358 359 360 361
    {
        return dynamic_cast< WGEAnimationManipulator* >( getCameraManipulator().get() );
    }

    // else: do nothing
    return NULL;
}

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