OpenWalnut
1.4.0
|
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 // needed since OSG 3.2 to ensure a properly initialized stateset. Also works with OSG 3.0 00036 getOrCreateStateSet()->setGlobalDefaults(); 00037 00038 setViewport( 0, 0, width, height ); 00039 setClearColor( osg::Vec4( 0.9, 0.9, 0.9, 1.0 ) ); 00040 00041 // disable all culling 00042 setCullingActive( false ); 00043 setCullingMode( osg::CullSettings::NO_CULLING ); 00044 00045 // near-far computation is done using the bounding volumes 00046 setComputeNearFarMode( 00047 osg::CullSettings::COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES 00048 // osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR 00049 // osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES 00050 ); 00051 reset(); 00052 } 00053 00054 WGECamera::WGECamera() 00055 : osg::Camera(), 00056 m_DefProjMode( ORTHOGRAPHIC ) 00057 { 00058 // needed since OSG 3.2 to ensure a properly initialized stateset. Also works with OSG 3.0 00059 getOrCreateStateSet()->setGlobalDefaults(); 00060 00061 // disable all culling 00062 setCullingActive( false ); 00063 setCullingMode( osg::CullSettings::NO_CULLING ); 00064 00065 // near-far computation is done using the bounding volumes 00066 setComputeNearFarMode( 00067 osg::CullSettings::COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES 00068 // osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR 00069 // osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES 00070 ); 00071 } 00072 00073 WGECamera::~WGECamera() 00074 { 00075 // cleanup 00076 } 00077 00078 void WGECamera::setDefaultProjectionMode( WGECamera::ProjectionMode mode ) 00079 { 00080 m_DefProjMode = mode; 00081 } 00082 00083 WGECamera::ProjectionMode WGECamera::getDefaultProjectionMode() 00084 { 00085 return m_DefProjMode; 00086 } 00087 00088 void WGECamera::reset() 00089 { 00090 switch( m_DefProjMode ) 00091 { 00092 case ORTHOGRAPHIC: 00093 setProjectionMatrixAsOrtho( -120.0 * getViewport()->aspectRatio(), 120.0 * getViewport()->aspectRatio(), 00094 -120.0, 120.0, -1000.0, +1000.0 ); 00095 setProjectionResizePolicy( HORIZONTAL ); 00096 break; 00097 case PERSPECTIVE: 00098 setProjectionMatrixAsPerspective( 30.0, getViewport()->aspectRatio(), 1.0, 1000.0 ); 00099 setProjectionResizePolicy( WGECamera::HORIZONTAL ); 00100 break; 00101 case TWO_D: 00102 resize(); 00103 setProjectionResizePolicy( WGECamera::FIXED ); 00104 break; 00105 case TWO_D_UNIT: 00106 resize(); 00107 setProjectionResizePolicy( WGECamera::FIXED ); 00108 break; 00109 default: 00110 throw WGEInitFailed( std::string( "Unknown projection mode." ) ); 00111 } 00112 } 00113 00114 void WGECamera::resize() 00115 { 00116 if( m_DefProjMode == TWO_D ) 00117 { 00118 setProjectionMatrixAsOrtho2D( 0.0, getViewport()->width(), 0.0, getViewport()->height() ); 00119 } 00120 else if( m_DefProjMode == TWO_D_UNIT ) 00121 { 00122 double aspectWH = static_cast< double >( getViewport()->width() ) / static_cast< double >( getViewport()->height() ); 00123 double aspectHW = 1.0 / aspectWH; 00124 00125 double w = aspectWH > aspectHW ? aspectWH : 1.0; 00126 double h = aspectWH > aspectHW ? 1.0 : aspectHW; 00127 00128 w *= 0.5; 00129 h *= 0.5; 00130 setProjectionMatrixAsOrtho( -w, w, -h, h, 0.0, 1.0 ); 00131 } 00132 }