OpenWalnut 1.2.5
|
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 <iostream> 00026 #include <string> 00027 00028 #include "exceptions/WGEInitFailed.h" 00029 #include "WGECamera.h" 00030 00031 WGECamera::WGECamera( int width, int height, ProjectionMode projectionMode ) 00032 : osg::Camera(), 00033 m_DefProjMode( projectionMode ) 00034 { 00035 setViewport( 0, 0, width, height ); 00036 setClearColor( osg::Vec4( 0.9, 0.9, 0.9, 1.0 ) ); 00037 reset(); 00038 } 00039 00040 WGECamera::~WGECamera() 00041 { 00042 // cleanup 00043 } 00044 00045 void WGECamera::setDefaultProjectionMode( WGECamera::ProjectionMode mode ) 00046 { 00047 m_DefProjMode = mode; 00048 } 00049 00050 WGECamera::ProjectionMode WGECamera::getDefaultProjectionMode() 00051 { 00052 return m_DefProjMode; 00053 } 00054 00055 void WGECamera::reset() 00056 { 00057 switch( m_DefProjMode ) 00058 { 00059 case ORTHOGRAPHIC: 00060 setProjectionMatrixAsOrtho( -120.0 * getViewport()->aspectRatio(), 120.0 * getViewport()->aspectRatio(), 00061 -120.0, 120.0, -1000.0, +1000.0 ); 00062 setProjectionResizePolicy( HORIZONTAL ); 00063 break; 00064 case PERSPECTIVE: 00065 setProjectionMatrixAsPerspective( 30.0, getViewport()->aspectRatio(), 1.0, 1000.0 ); 00066 setProjectionResizePolicy( osg::Camera::HORIZONTAL ); 00067 break; 00068 case TWO_D: 00069 resize(); 00070 setProjectionResizePolicy( osg::Camera::FIXED ); 00071 break; 00072 case TWO_D_UNIT: 00073 resize(); 00074 setProjectionResizePolicy( osg::Camera::FIXED ); 00075 break; 00076 default: 00077 throw WGEInitFailed( std::string( "Unknown projection mode." ) ); 00078 } 00079 } 00080 00081 void WGECamera::resize() 00082 { 00083 if( m_DefProjMode == TWO_D ) 00084 { 00085 setProjectionMatrixAsOrtho2D( 0.0, getViewport()->width(), 0.0, getViewport()->height() ); 00086 } 00087 else if( m_DefProjMode == TWO_D_UNIT ) 00088 { 00089 double aspectWH = static_cast< double >( getViewport()->width() ) / static_cast< double >( getViewport()->height() ); 00090 double aspectHW = 1.0 / aspectWH; 00091 00092 double w = aspectWH > aspectHW ? aspectWH : 1.0; 00093 double h = aspectWH > aspectHW ? 1.0 : aspectHW; 00094 00095 w *= 0.5; 00096 h *= 0.5; 00097 setProjectionMatrixAsOrtho( -w, w, -h, h, 0.0, 1.0 ); 00098 } 00099 }