WGEViewer.cpp 4.38 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
#include <string>
ebaum's avatar
ebaum committed
26
#include <iostream>
ebaum's avatar
ebaum committed
27 28 29 30 31 32 33 34 35 36 37

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

#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>
schurade's avatar
schurade committed
39
#include <osgViewer/View>
ebaum's avatar
ebaum committed
40 41 42 43

#include <osgDB/ReadFile>

#include "exceptions/WGEInitFailed.h"
44
#include "WPickHandler.h"
45
#include "../kernel/WKernel.h"
46

ebaum's avatar
ebaum committed
47
#include "WGEViewer.h"
ebaum's avatar
ebaum committed
48

49 50 51 52
WGEViewer::WGEViewer( std::string name, osg::ref_ptr<WindowData> wdata, int x, int y,
    int width, int height, WGECamera::ProjectionMode projectionMode )
    : WGEGraphicsWindow( wdata, x, y, width, height ),
      boost::enable_shared_from_this< WGEViewer >(),
53 54
      m_name( name ),
      m_projectionMode( projectionMode )
ebaum's avatar
ebaum committed
55 56 57
{
    try
    {
Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
58
        m_View = osg::ref_ptr<osgViewer::Viewer>( new osgViewer::Viewer() );
59

60 61 62
        m_View->setCamera( new WGECamera( width, height, projectionMode ) );
        m_View->getCamera()->setGraphicsContext( m_GraphicsContext );
        if( projectionMode == WGECamera::ORTHOGRAPHIC || projectionMode == WGECamera::PERSPECTIVE )
63
        {
64 65
            // camera manipulator
            m_View->setCameraManipulator( new WGEZoomTrackballManipulator() );
66 67
        }

ebaum's avatar
ebaum committed
68 69 70
        // add the stats handler
        m_View->addEventHandler( new osgViewer::StatsHandler );

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

73 74
        m_pickHandler = new WPickHandler();
        m_View->addEventHandler( m_pickHandler );
ebaum's avatar
ebaum committed
75 76 77 78 79 80 81 82 83 84
    }
    catch( ... )
    {
        throw WGEInitFailed( "Initialization of WGEViewer failed" );
    }
}

WGEViewer::~WGEViewer()
{
    // cleanup
85
    close();
ebaum's avatar
ebaum committed
86 87
}

88
osg::ref_ptr<osgViewer::Viewer> WGEViewer::getViewer()
ebaum's avatar
ebaum committed
89
{
90
    return m_View;
ebaum's avatar
ebaum committed
91 92
}

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

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

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

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

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

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
120
osg::ref_ptr<osg::Node> WGEViewer::getNode()
ebaum's avatar
ebaum committed
121 122
{
    return m_View->getSceneData();
ebaum's avatar
ebaum committed
123 124
}

125 126 127 128 129
void WGEViewer::setBgColor( WColor bgColor )
{
    m_View->getCamera()->setClearColor( osg::Vec4( bgColor.getRed(), bgColor.getGreen(), bgColor.getBlue(), 1. ) );
}

ebaum's avatar
ebaum committed
130 131 132 133 134
void WGEViewer::resize( int width, int height )
{
    WGEGraphicsWindow::resize( width, height );

    // also update the camera
135
    m_View->getCamera()->setViewport( 0, 0, width, height );
136 137 138 139 140

    if( m_projectionMode == WGECamera::TWO_D )
    {
        m_View->getCamera()->setProjectionMatrixAsOrtho2D( 0.0, width, 0.0, height );
    }
ebaum's avatar
ebaum committed
141 142
}

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
143 144 145 146 147
void WGEViewer::close()
{
    // forward close event
    WGEGraphicsWindow::close();
}
148 149 150 151 152 153

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

154
osg::ref_ptr< WPickHandler > WGEViewer::getPickHandler()
schurade's avatar
schurade committed
155 156 157
{
    return m_pickHandler;
}