changed: gcc8 base update
[opensg.git] / Source / System / Window / Camera / OSGOffCenterPerspectiveCamera.cpp
blob345b225cb838778248b6ab1c01b1a748a656ebde
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 <stdlib.h>
44 #include <stdio.h>
45 #include <math.h>
47 #include "OSGMatrixUtility.h"
48 #include "OSGOffCenterPerspectiveCamera.h"
50 OSG_USING_NAMESPACE
53 /***************************************************************************\
54 * Description *
55 \***************************************************************************/
57 /*! \class OSG::OffCenterPerspectiveCamera
58 \ingroup GrpSystemWindowCameras
60 The OffCenter Perspective Camera class.
62 The only new parameter is the _sfPrincipalPoint field.
64 The OffCenter Perspective Camera provides a more general camera than the
65 Perspective Camera.
66 The viewing frustrum is by default the same as given by the Perspective
67 Camera.
68 Iff principal points params are unequal zero the viewing frustrums parameters
69 (left,right,top,bottom) are simple shifted in the camera's image plane.
70 The principal point parameters are relative to left--right (or respectively
71 bottom--top).
72 A value of x=2 means the left value is equal to default
73 right value.
74 A value of x=-2 means the right value is equal to default
75 left value.
76 Marginal note: Iff the principal point is unequal zero the field of view
77 value is unequal to the real field of view of the camera.
78 (mail to: dherzog (at) mip.informatik.uni-kiel.de)
81 /***************************************************************************\
82 * Class methods *
83 \***************************************************************************/
85 /*-------------------------------------------------------------------------*\
86 - private -
87 \*-------------------------------------------------------------------------*/
89 void OffCenterPerspectiveCamera::initMethod(InitPhase ePhase)
91 Inherited::initMethod(ePhase);
94 /***************************************************************************\
95 * Instance methods *
96 \***************************************************************************/
98 /*-------------------------------------------------------------------------*\
99 - public -
100 \*-------------------------------------------------------------------------*/
103 /*------------- constructors & destructors --------------------------------*/
105 OffCenterPerspectiveCamera::OffCenterPerspectiveCamera(void) :
106 Inherited()
110 OffCenterPerspectiveCamera::OffCenterPerspectiveCamera(
111 const OffCenterPerspectiveCamera &source) :
113 Inherited(source)
117 OffCenterPerspectiveCamera::~OffCenterPerspectiveCamera(void)
121 void OffCenterPerspectiveCamera::changed(ConstFieldMaskArg whichField,
122 UInt32 origin,
123 BitVector details)
125 Inherited::changed(whichField, origin, details);
128 /*-------------------------- your_category---------------------------------*/
131 void OffCenterPerspectiveCamera::getProjection(Matrix &result,
132 UInt32 width,
133 UInt32 height)
135 Real32 fov = getFov();
137 // catch some illegal cases
138 if(fov < 0 || width == 0 || height == 0)
140 result.setIdentity();
141 return;
144 // try to be nice to people giving degrees...
145 if(fov > Pi)
146 fov = osgDegree2Rad(fov);
149 Real32 rNear = getNear();
150 Real32 rFar = getFar();
151 Real32 aspect = Real32(width) / Real32(height) * getAspect();
152 Real32 ct = osgTan(fov / 2.f);
154 if(rNear > rFar)
156 SWARNING << "MatrixPerspective: near " << rNear << " > far " << rFar
157 << "!\n" << std::endl;
158 result.setIdentity();
159 return;
162 if(fov <= TypeTraits<Real32>::getDefaultEps())
164 SWARNING << "MatrixPerspective: fov " << fov << " very small!\n"
165 << std::endl;
166 result.setIdentity();
167 return;
170 if(osgAbs(rNear - rFar) < TypeTraits<Real32>::getDefaultEps())
172 SWARNING << "MatrixPerspective: near " << rNear << " ~= far " << rFar
173 << "!\n" << std::endl;
174 result.setIdentity();
175 return;
178 if(aspect < TypeTraits<Real32>::getDefaultEps())
180 SWARNING << "MatrixPerspective: aspect ratio " << aspect
181 << " very small!\n" << std::endl;
182 result.setIdentity();
183 return;
186 Real32 x = ct * rNear;
187 Real32 y = ct * rNear;
188 UInt32 fovMode = getFovMode();
190 switch (fovMode)
192 case VerticalFoV:
193 x *= aspect;
194 break;
196 case HorizontalFoV:
197 y /= aspect;
198 break;
200 case SmallerFoV:
201 if(width * getAspect() >= height)
203 x *= aspect;
205 else
207 y /= aspect;
209 break;
211 default:
212 result.setIdentity();
213 return;
216 Real32 principalPointX = getPrincipalPoint()[0];
217 Real32 principalPointY = getPrincipalPoint()[1];
219 // if principal point (x,y) is default (==(0,0)) everything works
220 // like before or rather for an symmetical camera
221 if ((principalPointX == 0.f) && (principalPointY == 0.f))
223 MatrixFrustum( result,
228 rNear,
229 rFar);
231 else
233 MatrixFrustum( result,
234 -x * (1.f + principalPointX),
235 x * (1.f - principalPointX),
236 -y * (1.f + principalPointY),
237 y * (1.f - principalPointY),
238 rNear,
239 rFar);
244 /*------------------------------- dump ----------------------------------*/
246 void OffCenterPerspectiveCamera::dump(
247 UInt32 OSG_CHECK_ARG(uiIndent),
248 const BitVector OSG_CHECK_ARG(bvFlags)) const
250 SLOG << "Dump OffCenterPerspectiveCamera NI" << std::endl;