OpenWalnut  1.5.0dev
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  // needed since OSG 3.2 to ensure a properly initialized stateset. Also works with OSG 3.0
36  getOrCreateStateSet()->setGlobalDefaults();
37 
38  setViewport( 0, 0, width, height );
39  setClearColor( osg::Vec4( 0.9, 0.9, 0.9, 1.0 ) );
40 
41  // disable all culling
42  setCullingActive( false );
43  setCullingMode( osg::CullSettings::NO_CULLING );
44 
45  // near-far computation is done using the bounding volumes
46  setComputeNearFarMode(
47  osg::CullSettings::COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES
48  // osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR
49  // osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES
50  );
51  reset();
52 }
53 
55  : osg::Camera(),
56  m_DefProjMode( ORTHOGRAPHIC )
57 {
58  // needed since OSG 3.2 to ensure a properly initialized stateset. Also works with OSG 3.0
59  getOrCreateStateSet()->setGlobalDefaults();
60 
61  // disable all culling
62  setCullingActive( false );
63  setCullingMode( osg::CullSettings::NO_CULLING );
64 
65  // near-far computation is done using the bounding volumes
66  setComputeNearFarMode(
67  osg::CullSettings::COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES
68  // osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR
69  // osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES
70  );
71 }
72 
74 {
75  // cleanup
76 }
77 
79 {
80  m_DefProjMode = mode;
81 }
82 
84 {
85  return m_DefProjMode;
86 }
87 
89 {
90  switch( m_DefProjMode )
91  {
92  case ORTHOGRAPHIC:
93  setProjectionMatrixAsOrtho( -120.0 * getViewport()->aspectRatio(), 120.0 * getViewport()->aspectRatio(),
94  -120.0, 120.0, -1000.0, +1000.0 );
95  setProjectionResizePolicy( HORIZONTAL );
96  break;
97  case PERSPECTIVE:
98  setProjectionMatrixAsPerspective( 30.0, getViewport()->aspectRatio(), 1.0, 1000.0 );
99  setProjectionResizePolicy( WGECamera::HORIZONTAL );
100  break;
101  case TWO_D:
102  resize();
103  setProjectionResizePolicy( WGECamera::FIXED );
104  break;
105  case TWO_D_UNIT:
106  resize();
107  setProjectionResizePolicy( WGECamera::FIXED );
108  break;
109  default:
110  throw WGEInitFailed( std::string( "Unknown projection mode." ) );
111  }
112 }
113 
115 {
116  if( m_DefProjMode == TWO_D )
117  {
118  setProjectionMatrixAsOrtho2D( 0.0, getViewport()->width(), 0.0, getViewport()->height() );
119  }
120  else if( m_DefProjMode == TWO_D_UNIT )
121  {
122  double aspectWH = static_cast< double >( getViewport()->width() ) / static_cast< double >( getViewport()->height() );
123  double aspectHW = 1.0 / aspectWH;
124 
125  double w = aspectWH > aspectHW ? aspectWH : 1.0;
126  double h = aspectWH > aspectHW ? 1.0 : aspectHW;
127 
128  w *= 0.5;
129  h *= 0.5;
130  setProjectionMatrixAsOrtho( -w, w, -h, h, 0.0, 1.0 );
131  }
132 }
void reset()
Resets the camera and activates the prior set defaults.
Definition: WGECamera.cpp:88
virtual ~WGECamera()
Destructor.
Definition: WGECamera.cpp:73
ProjectionMode m_DefProjMode
The projection mode used as default.
Definition: WGECamera.h:103
void setDefaultProjectionMode(ProjectionMode mode)
Sets the default projection mode used for cameras getting reset.
Definition: WGECamera.cpp:78
ProjectionMode
List of possible camera modes.
Definition: WGECamera.h:44
void resize()
Change camera parameters which should be changed on a resize.
Definition: WGECamera.cpp:114
WGECamera()
Constructor which sets defaults.
Definition: WGECamera.cpp:54
ProjectionMode getDefaultProjectionMode()
Returns the current default projection mode.
Definition: WGECamera.cpp:83
Exception thrown if initialization of the graphics engine fails.
Definition: WGEInitFailed.h:38