OpenWalnut 1.2.5

WGEViewer.cpp

00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <string>
00026 #include <iostream>
00027 
00028 #include <osg/ShapeDrawable>
00029 #include <osg/Geode>
00030 #include <osg/Camera>
00031 
00032 #include <osgGA/FlightManipulator>
00033 #include <osgGA/DriveManipulator>
00034 #include <osgGA/UFOManipulator>
00035 #include <osgGA/KeySwitchMatrixManipulator>
00036 #include <osgGA/StateSetManipulator>
00037 #include <osgGA/AnimationPathManipulator>
00038 #include <osgGA/TerrainManipulator>
00039 #include <osgViewer/ViewerEventHandlers>
00040 #include <osgViewer/View>
00041 
00042 #include <osgDB/ReadFile>
00043 
00044 #include "exceptions/WGEInitFailed.h"
00045 #include "WGE2DManipulator.h"
00046 #include "WGENoOpManipulator.h"
00047 #include "WGEZoomTrackballManipulator.h"
00048 #include "WPickHandler.h"
00049 #include "../common/WConditionOneShot.h"
00050 
00051 #include "WGEViewer.h"
00052 
00053 WGEViewer::WGEViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y,
00054     int width, int height, WGECamera::ProjectionMode projectionMode )
00055     : WGEGraphicsWindow( wdata, x, y, width, height ),
00056       boost::enable_shared_from_this< WGEViewer >(),
00057       m_name( name ),
00058       m_rendered( WBoolFlag::SPtr( new WBoolFlag( new WConditionOneShot(), false ) ) )
00059 {
00060     try
00061     {
00062 #ifndef __APPLE__
00063         m_View = osg::ref_ptr<osgViewer::View>( new osgViewer::View );
00064 #else
00065         // on mac, this is a viewer!
00066         m_View = osg::ref_ptr<osgViewer::Viewer>( new osgViewer::Viewer );
00067 #endif
00068 
00069         m_View->setCamera( new WGECamera( width, height, projectionMode ) );
00070         m_queryCallback = new QueryCallback( m_View->getCamera(), m_rendered );
00071         m_View->getCamera()->setInitialDrawCallback( m_queryCallback );
00072 
00073 #ifndef __APPLE__
00074         m_View->getCamera()->setGraphicsContext( m_GraphicsContext.get() );
00075 #else
00076         m_View->getCamera()->setGraphicsContext( m_GraphicsWindow.get() );
00077 #endif
00078 
00079         switch( projectionMode )
00080         {
00081             case( WGECamera::ORTHOGRAPHIC ):
00082                 m_pickHandler = new WPickHandler( name );
00083                 m_View->addEventHandler( m_pickHandler );
00084                 if( name != std::string( "main" ) )
00085                     break;
00086             case( WGECamera::PERSPECTIVE ):
00087                 // camera manipulator
00088                 m_View->setCameraManipulator( new WGEZoomTrackballManipulator() );
00089 
00090                 m_View->setLightingMode( osg::View::HEADLIGHT ); // this is the default anyway
00091 
00092                 break;
00093             case( WGECamera::TWO_D ):
00094                 // no manipulators nor gui handlers
00095                 break;
00096             case( WGECamera::TWO_D_UNIT ):
00097                 // use no-op handler by default
00098                 m_View->setCameraManipulator( new WGENoOpManipulator() );
00099                 break;
00100             default:
00101                 throw WGEInitFailed( std::string( "Unknown projection mode" ) );
00102         }
00103 
00104         // add the stats handler
00105         m_View->addEventHandler( new osgViewer::StatsHandler );
00106     }
00107     catch( ... )
00108     {
00109         throw WGEInitFailed( std::string( "Initialization of WGEViewer failed" ) );
00110     }
00111 }
00112 
00113 WGEViewer::~WGEViewer()
00114 {
00115     // cleanup
00116     close();
00117 }
00118 
00119 #ifdef __APPLE__
00120 osg::ref_ptr<osgViewer::Viewer>
00121 #else
00122 osg::ref_ptr<osgViewer::View>
00123 #endif
00124 WGEViewer::getView()
00125 {
00126     return m_View;
00127 }
00128 
00129 void WGEViewer::setCameraManipulator( osg::ref_ptr<osgGA::MatrixManipulator> manipulator )
00130 {
00131     m_View->setCameraManipulator( manipulator );
00132     // redraw request?? no since it redraws permanently and uses the new settings
00133 }
00134 
00135 osg::ref_ptr<osgGA::MatrixManipulator> WGEViewer::getCameraManipulator()
00136 {
00137     return m_View->getCameraManipulator();
00138 }
00139 
00140 void WGEViewer::setCamera( osg::ref_ptr<osg::Camera> camera )
00141 {
00142     m_View->setCamera( camera );
00143     // redraw request?? No since it redraws permanently and uses the new settings
00144 }
00145 
00146 osg::ref_ptr<osg::Camera> WGEViewer::getCamera()
00147 {
00148     return m_View->getCamera();
00149 }
00150 
00151 void WGEViewer::setScene( osg::ref_ptr< WGEGroupNode > node )
00152 {
00153     m_View->setSceneData( node );
00154     m_scene = node;
00155 }
00156 
00157 osg::ref_ptr< WGEGroupNode > WGEViewer::getScene()
00158 {
00159     return m_scene;
00160 }
00161 
00162 void WGEViewer::setBgColor( const WColor& bgColor )
00163 {
00164     m_View->getCamera()->setClearColor( bgColor );
00165 }
00166 
00167 void WGEViewer::paint()
00168 {
00169 #ifdef __APPLE__
00170     m_View->frame();
00171 #endif
00172 }
00173 
00174 void WGEViewer::resize( int width, int height )
00175 {
00176     m_View->getEventQueue()->windowResize( 0, 0, width, height );
00177 
00178     WGEGraphicsWindow::resize( width, height );
00179 
00180     // also update the camera
00181     m_View->getCamera()->setViewport( 0, 0, width, height );
00182     WGECamera* camera = dynamic_cast< WGECamera* >( m_View->getCamera() );
00183     if( camera )
00184     {
00185         camera->resize();
00186     }
00187 }
00188 
00189 void WGEViewer::close()
00190 {
00191     // forward close event
00192     WGEGraphicsWindow::close();
00193 }
00194 
00195 std::string WGEViewer::getName() const
00196 {
00197     return m_name;
00198 }
00199 
00200 osg::ref_ptr< WPickHandler > WGEViewer::getPickHandler()
00201 {
00202     return m_pickHandler;
00203 }
00204 
00205 void WGEViewer::reset()
00206 {
00207     m_View->home();
00208 }
00209 
00210 std::string WGEViewer::getOpenGLVendor() const
00211 {
00212     return m_queryCallback->getVendor();
00213 }
00214 
00215 WBoolFlag::SPtr WGEViewer::isFrameRendered() const
00216 {
00217     return m_rendered;
00218 }
00219 
00220 WGEViewer::QueryCallback::QueryCallback( osg::ref_ptr<osg::Camera> camera, WBoolFlag::SPtr run ):
00221     m_vendor( "" ),
00222     m_run( run ),
00223     m_camera( camera )
00224 {
00225     // init
00226 }
00227 
00228 WGEViewer::QueryCallback::~QueryCallback()
00229 {
00230     // cleanup
00231 }
00232 
00233 void WGEViewer::QueryCallback::operator()( osg::RenderInfo& /* renderInfo */ ) const
00234 {
00235     const GLubyte* vendor = glGetString( GL_VENDOR );
00236     m_vendor = reinterpret_cast< const char* >( vendor );
00237 
00238     // job done. De-register.
00239     m_camera->setInitialDrawCallback( NULL );
00240     m_run->set( true );
00241 }
00242 
00243 std::string WGEViewer::QueryCallback::getVendor() const
00244 {
00245     return m_vendor;
00246 }
00247 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends