OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WGECamera.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <iostream>
26 #include <string>
27 
28 #include "exceptions/WGEInitFailed.h"
29 #include "WGECamera.h"
30 
31 WGECamera::WGECamera( int width, int height, ProjectionMode projectionMode )
32  : osg::Camera(),
33  m_DefProjMode( projectionMode )
34 {
35  setViewport( 0, 0, width, height );
36  setClearColor( osg::Vec4( 0.9, 0.9, 0.9, 1.0 ) );
37  reset();
38 }
39 
41 {
42  // cleanup
43 }
44 
46 {
47  m_DefProjMode = mode;
48 }
49 
51 {
52  return m_DefProjMode;
53 }
54 
56 {
57  switch( m_DefProjMode )
58  {
59  case ORTHOGRAPHIC:
60  setProjectionMatrixAsOrtho( -120.0 * getViewport()->aspectRatio(), 120.0 * getViewport()->aspectRatio(),
61  -120.0, 120.0, -1000.0, +1000.0 );
62  setProjectionResizePolicy( HORIZONTAL );
63  break;
64  case PERSPECTIVE:
65  setProjectionMatrixAsPerspective( 30.0, getViewport()->aspectRatio(), 1.0, 1000.0 );
66  setProjectionResizePolicy( osg::Camera::HORIZONTAL );
67  break;
68  case TWO_D:
69  resize();
70  setProjectionResizePolicy( osg::Camera::FIXED );
71  break;
72  case TWO_D_UNIT:
73  resize();
74  setProjectionResizePolicy( osg::Camera::FIXED );
75  break;
76  default:
77  throw WGEInitFailed( std::string( "Unknown projection mode." ) );
78  }
79 }
80 
82 {
83  if( m_DefProjMode == TWO_D )
84  {
85  setProjectionMatrixAsOrtho2D( 0.0, getViewport()->width(), 0.0, getViewport()->height() );
86  }
87  else if( m_DefProjMode == TWO_D_UNIT )
88  {
89  double aspectWH = static_cast< double >( getViewport()->width() ) / static_cast< double >( getViewport()->height() );
90  double aspectHW = 1.0 / aspectWH;
91 
92  double w = aspectWH > aspectHW ? aspectWH : 1.0;
93  double h = aspectWH > aspectHW ? 1.0 : aspectHW;
94 
95  w *= 0.5;
96  h *= 0.5;
97  setProjectionMatrixAsOrtho( -w, w, -h, h, 0.0, 1.0 );
98  }
99 }