WGEViewer.cpp 7.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// 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/>.
//
//---------------------------------------------------------------------------

#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

#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>

44 45
#include "exceptions/WGEInitFailed.h"
#include "WGE2DManipulator.h"
46
#include "WGEGroupNode.h"
47
#include "WGENoOpManipulator.h"
48 49
#include "WGEZoomTrackballManipulator.h"
#include "WPickHandler.h"
50
#include "../common/WConditionOneShot.h"
51

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

ebaum's avatar
ebaum committed
54
#include "WGEViewer.h"
55

56
WGEViewer::WGEViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y,
57
    int width, int height, WGECamera::ProjectionMode projectionMode )
58
    : WGEGraphicsWindow( wdata, x, y, width, height ),
59
      boost::enable_shared_from_this< WGEViewer >(),
60
      m_name( name ),
61
      m_rendered( WBoolFlag::SPtr( new WBoolFlag( new WConditionOneShot(), false ) ) ),
62 63
      m_screenCapture( new WGEScreenCapture() ),
      m_inAnimationMode( false )
64 65 66
{
    try
    {
67
#ifndef __APPLE__
68
        m_View = osg::ref_ptr<osgViewer::View>( new osgViewer::View );
69 70 71 72 73
#else
        // on mac, this is a viewer!
        m_View = osg::ref_ptr<osgViewer::Viewer>( new osgViewer::Viewer );
#endif

74
        m_View->setCamera( new WGECamera( width, height, projectionMode ) );
75 76
        m_queryCallback = new QueryCallback( m_View->getCamera(), m_rendered );
        m_View->getCamera()->setInitialDrawCallback( m_queryCallback );
77
        m_View->getCamera()->setFinalDrawCallback( m_screenCapture );
78

79 80 81 82 83 84
#ifndef __APPLE__
        m_View->getCamera()->setGraphicsContext( m_GraphicsContext.get() );
#else
        m_View->getCamera()->setGraphicsContext( m_GraphicsWindow.get() );
#endif

85 86 87 88 89
        switch( projectionMode )
        {
            case( WGECamera::ORTHOGRAPHIC ):
                m_pickHandler = new WPickHandler( name );
                m_View->addEventHandler( m_pickHandler );
90
                if( name != std::string( "main" ) )
91 92 93 94 95 96 97 98 99 100 101
                    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;
102 103 104 105
            case( WGECamera::TWO_D_UNIT ):
                // use no-op handler by default
                m_View->setCameraManipulator( new WGENoOpManipulator() );
                break;
106 107 108 109 110 111 112 113 114
            default:
                throw WGEInitFailed( std::string( "Unknown projection mode" ) );
        }

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

119
WGEViewer::~WGEViewer()
120 121 122 123 124
{
    // cleanup
    close();
}

125 126 127 128 129 130
#ifdef __APPLE__
osg::ref_ptr<osgViewer::Viewer>
#else
osg::ref_ptr<osgViewer::View>
#endif
WGEViewer::getView()
131 132 133 134
{
    return m_View;
}

135
void WGEViewer::setCameraManipulator( osg::ref_ptr<osgGA::MatrixManipulator> manipulator )
136
{
137 138 139 140
    if ( !m_inAnimationMode )
    {
        m_View->setCameraManipulator( manipulator );
    }
141 142
}

143
osg::ref_ptr<osgGA::MatrixManipulator> WGEViewer::getCameraManipulator()
144 145 146 147
{
    return m_View->getCameraManipulator();
}

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

154
osg::ref_ptr<osg::Camera> WGEViewer::getCamera()
155 156 157 158
{
    return m_View->getCamera();
}

159
void WGEViewer::setScene( osg::ref_ptr< WGEGroupNode > node )
160 161 162 163 164
{
    m_View->setSceneData( node );
    m_scene = node;
}

165
osg::ref_ptr< WGEGroupNode > WGEViewer::getScene()
166 167 168 169
{
    return m_scene;
}

170
void WGEViewer::setBgColor( const WColor& bgColor )
171
{
172
    m_View->getCamera()->setClearColor( bgColor );
173 174
}

175 176 177 178 179 180 181 182
void WGEViewer::paint()
{
#ifdef __APPLE__
    m_View->frame();
#endif
}

void WGEViewer::resize( int width, int height )
183 184 185 186 187 188 189 190 191 192 193 194 195 196
{
    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();
    }
}

197
void WGEViewer::close()
198 199 200 201 202
{
    // forward close event
    WGEGraphicsWindow::close();
}

203
std::string WGEViewer::getName() const
204 205 206 207
{
    return m_name;
}

208
osg::ref_ptr< WPickHandler > WGEViewer::getPickHandler()
209 210 211 212
{
    return m_pickHandler;
}

213
void WGEViewer::reset()
214 215 216 217
{
    m_View->home();
}

218
WGEScreenCapture::RefPtr WGEViewer::getScreenCapture() const
219 220 221 222
{
    return m_screenCapture;
}

223 224 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
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;
}
260

261
WGEAnimationManipulator::RefPtr WGEViewer::animationMode( bool on )
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
{
    if ( m_inAnimationMode && !on ) // turn off mode
    {
        m_inAnimationMode = false;

        // restore old manipulator
        m_View->setCameraManipulator( m_animationModeManipulatorBackup );
        return NULL;
    }
    else if ( !m_inAnimationMode && on ) // turn on
    {
        m_inAnimationMode = true;

        // backup
        m_animationModeManipulatorBackup = getCameraManipulator();

        // create animation manipulator
        WGEAnimationManipulator::RefPtr anim = new WGEAnimationManipulator();
        m_View->setCameraManipulator( anim );
        return anim;
    }
    else if ( m_inAnimationMode ) // already on
    {
        return dynamic_cast< WGEAnimationManipulator* >( getCameraManipulator().get() );
    }

    // else: do nothing
    return NULL;
}

292
bool WGEViewer::isAnimationMode() const
293 294 295 296
{
    return m_inAnimationMode;
}