fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Window / Base / OSGPerspectiveCamera.cpp
blobe8157678b97fab098e3d8eed30731d7adaa5ee25
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright(C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
45 #include <cmath>
47 #include "OSGConfig.h"
49 #include "OSGBaseTypes.h"
50 #include "OSGLog.h"
51 #include "OSGVector.h"
52 #include "OSGMatrix.h"
53 #include "OSGMatrixUtility.h"
55 #include "OSGWindow.h"
56 #include "OSGViewport.h"
57 #include "OSGCamera.h"
58 #include "OSGWindow.h"
59 #include "OSGBackground.h"
60 #include "OSGPerspectiveCamera.h"
62 OSG_USING_NAMESPACE
64 // Documentation for this class is emited in the
65 // OSGPerspectiveCameraBase.cpp file.
66 // To modify it, please change the .fcd file (OSGPerspectiveCamera.fcd) and
67 // regenerate the base file.
69 /***************************************************************************\
70 * Class methods *
71 \***************************************************************************/
73 /*-------------------------------------------------------------------------*\
74 - private -
75 \*-------------------------------------------------------------------------*/
77 void PerspectiveCamera::initMethod(InitPhase ePhase)
79 Inherited::initMethod(ePhase);
82 /***************************************************************************\
83 * Instance methods *
84 \***************************************************************************/
86 /*-------------------------------------------------------------------------*\
87 - public -
88 \*-------------------------------------------------------------------------*/
91 /*------------- constructors & destructors --------------------------------*/
93 PerspectiveCamera::PerspectiveCamera(void) :
94 Inherited()
98 PerspectiveCamera::PerspectiveCamera(const PerspectiveCamera &source) :
99 Inherited(source)
103 PerspectiveCamera::~PerspectiveCamera(void)
107 void PerspectiveCamera::changed(ConstFieldMaskArg whichField,
108 UInt32 origin,
109 BitVector details)
111 Inherited::changed(whichField, origin, details);
114 /*-------------------------- your_category---------------------------------*/
118 void PerspectiveCamera::getProjection(Matrix &result,
119 UInt32 width,
120 UInt32 height)
122 Real32 fov = getFov();
124 // catch some illegal cases
126 if(fov < 0 || width == 0 || height == 0)
128 result.setIdentity();
129 return;
132 // try to be nice to people giving degrees...
134 if(fov > Pi)
135 fov = osgDegree2Rad(fov);
138 Real32 rNear = getNear();
139 Real32 rFar = getFar ();
140 Real32 aspect = Real32(width) / Real32(height) * getAspect();
141 Real32 ct = osgTan(fov / 2.f);
143 if(rNear > rFar)
145 SWARNING << "MatrixPerspective: near " << rNear << " > far " << rFar
146 << "!\n" << std::endl;
147 result.setIdentity();
148 return;
151 if(fov <= TypeTraits<Real32>::getDefaultEps())
153 SWARNING << "MatrixPerspective: fov " << fov << " very small!\n"
154 << std::endl;
155 result.setIdentity();
156 return;
159 if(osgAbs(rNear - rFar) < TypeTraits<Real32>::getDefaultEps())
161 SWARNING << "MatrixPerspective: near " << rNear << " ~= far " << rFar
162 << "!\n" << std::endl;
163 result.setIdentity();
164 return;
167 if(aspect < TypeTraits<Real32>::getDefaultEps())
169 SWARNING << "MatrixPerspective: aspect ratio " << aspect
170 << " very small!\n" << std::endl;
171 result.setIdentity();
172 return;
175 Real32 x = ct * rNear;
176 Real32 y = ct * rNear;
178 UInt32 fovMode = getFovMode();
180 switch (fovMode)
182 case VerticalFoV:
183 x *= aspect;
184 break;
186 case HorizontalFoV:
187 y /= aspect;
188 break;
190 case SmallerFoV:
191 if(width * getAspect() >= height)
193 x *= aspect;
195 else
197 y /= aspect;
199 break;
201 default:
202 result.setIdentity();
203 return;
206 MatrixFrustum( result,
211 rNear,
212 rFar);
217 /*------------------------------- dump ----------------------------------*/
219 void PerspectiveCamera::dump( UInt32 OSG_CHECK_ARG(uiIndent),
220 const BitVector OSG_CHECK_ARG(bvFlags)) const
222 SLOG << "Dump PerspectiveCamera NI" << std::endl;