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

ebaum's avatar
ebaum committed
25
#include <iostream>
ebaum's avatar
ebaum committed
26 27 28 29 30 31 32 33 34 35 36 37

#include <osg/ShapeDrawable>
#include <osg/Geode>

#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/UFOManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
ebaum's avatar
ebaum committed
38
#include <osgViewer/ViewerEventHandlers>
ebaum's avatar
ebaum committed
39 40 41 42

#include <osgDB/ReadFile>

#include "exceptions/WGEInitFailed.h"
ebaum's avatar
ebaum committed
43
#include "WGEViewer.h"
ebaum's avatar
ebaum committed
44

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
45
WGEViewer::WGEViewer( osg::ref_ptr<WindowData> wdata, int x, int y, int width, int height ):
ebaum's avatar
ebaum committed
46 47 48 49 50
    WGEGraphicsWindow( wdata, x, y, width, height )
{
    try
    {
        // initialize OSG render window
Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
51
        m_Viewer = osg::ref_ptr<osgViewer::CompositeViewer>( new osgViewer::CompositeViewer() );
ebaum's avatar
ebaum committed
52 53 54
        m_Viewer->setThreadingModel( osgViewer::Viewer::DrawThreadPerContext );
        // m_Viewer->setThreadingModel( osgViewer::Viewer::SingleThreaded );

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
55
        m_View = osg::ref_ptr<osgViewer::Viewer>( new osgViewer::Viewer() );
ebaum's avatar
ebaum committed
56
        m_View->getCamera()->setGraphicsContext( m_GraphicsContext );
57 58 59 60 61
        // m_View->getCamera()->setProjectionMatrixAsPerspective(
        //     30.0, static_cast< double >( width ) / static_cast< double >( height ), 1.0, 1000.0 );
        m_View->getCamera()->setProjectionMatrixAsOrtho( -120.0 * width / height, 120.0 * width / height, -120.0, 120.0, -1000.0, +1000.0 );
        m_View->getCamera()->setViewport( 0, 0, width, height );
        m_View->getCamera()->setProjectionResizePolicy( osg::Camera::HORIZONTAL );
ebaum's avatar
ebaum committed
62 63 64 65 66

        // add the stats handler
        m_View->addEventHandler( new osgViewer::StatsHandler );

        // camera manipulator
ebaum's avatar
ebaum committed
67
        m_View->setCameraManipulator( new osgGA::TrackballManipulator() );
ebaum's avatar
ebaum committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

        // finally add view
        // there is the possibility to use ONE single composite viewer instance for every view, but
        // currently this possibility is not used.
        m_Viewer->addView( m_View.get() );
    }
    catch( ... )
    {
        throw WGEInitFailed( "Initialization of WGEViewer failed" );
    }
}

WGEViewer::~WGEViewer()
{
    // cleanup
Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
83
    wait( true );
ebaum's avatar
ebaum committed
84 85
}

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
86
osg::ref_ptr<osgViewer::CompositeViewer> WGEViewer::getViewer()
ebaum's avatar
ebaum committed
87 88 89 90
{
    return m_Viewer;
}

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
91
void WGEViewer::setCameraManipulator( osg::ref_ptr<osgGA::MatrixManipulator> manipulator )
ebaum's avatar
ebaum committed
92
{
ebaum's avatar
ebaum committed
93 94
    m_View->setCameraManipulator( manipulator );
    // redraw request?? no since it redraws permanently and uses the new settings
ebaum's avatar
ebaum committed
95 96
}

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
97
osg::ref_ptr<osgGA::MatrixManipulator> WGEViewer::getCameraManipulator()
ebaum's avatar
ebaum committed
98
{
ebaum's avatar
ebaum committed
99 100 101
    return m_View->getCameraManipulator();
}

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
102
void WGEViewer::setCamera( osg::ref_ptr<osg::Camera> camera )
ebaum's avatar
ebaum committed
103 104 105 106 107
{
    m_View->setCamera( camera );
    // redraw request?? no since it redraws permanently and uses the new settings
}

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
108
osg::ref_ptr<osg::Camera> WGEViewer::getCamera()
ebaum's avatar
ebaum committed
109 110 111 112
{
    return m_View->getCamera();
}

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
113
void WGEViewer::setScene( osg::ref_ptr<osg::Node> node )
ebaum's avatar
ebaum committed
114 115 116 117
{
    m_View->setSceneData( node );
}

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
118
osg::ref_ptr<osg::Node> WGEViewer::getNode()
ebaum's avatar
ebaum committed
119 120
{
    return m_View->getSceneData();
ebaum's avatar
ebaum committed
121 122 123 124 125 126 127 128 129 130 131 132
}

void WGEViewer::paint()
{
    m_Viewer->frame();
}

void WGEViewer::resize( int width, int height )
{
    WGEGraphicsWindow::resize( width, height );

    // also update the camera
133
    m_View->getCamera()->setViewport( 0, 0, width, height );
ebaum's avatar
ebaum committed
134 135
}

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
136 137 138 139 140 141 142 143 144 145 146
void WGEViewer::close()
{
    // wait for thread to finish
    wait( true );

    // forward close event
    WGEGraphicsWindow::close();
}

void WGEViewer::threadMain()
{
147 148 149
    // TODO(ebaum): Ticket #125 (synchronize GUI and GraphicsEngine)
    sleep( 1 );

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
150 151 152 153 154
    while( !m_FinishRequested )
    {
        paint();
    }
}