1 /*---------------------------------------------------------------------------*\
5 * Copyright(C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
47 #include "OSGMatrixUtility.h"
48 #include "OSGOffCenterPerspectiveCamera.h"
53 /***************************************************************************\
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
66 The viewing frustrum is by default the same as given by the Perspective
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
72 A value of x=2 means the left value is equal to default
74 A value of x=-2 means the right value is equal to default
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 /***************************************************************************\
83 \***************************************************************************/
85 /*-------------------------------------------------------------------------*\
87 \*-------------------------------------------------------------------------*/
89 void OffCenterPerspectiveCamera::initMethod(InitPhase ePhase
)
91 Inherited::initMethod(ePhase
);
94 /***************************************************************************\
96 \***************************************************************************/
98 /*-------------------------------------------------------------------------*\
100 \*-------------------------------------------------------------------------*/
103 /*------------- constructors & destructors --------------------------------*/
105 OffCenterPerspectiveCamera::OffCenterPerspectiveCamera(void) :
110 OffCenterPerspectiveCamera::OffCenterPerspectiveCamera(
111 const OffCenterPerspectiveCamera
&source
) :
117 OffCenterPerspectiveCamera::~OffCenterPerspectiveCamera(void)
121 void OffCenterPerspectiveCamera::changed(ConstFieldMaskArg whichField
,
125 Inherited::changed(whichField
, origin
, details
);
128 /*-------------------------- your_category---------------------------------*/
131 void OffCenterPerspectiveCamera::getProjection(Matrix
&result
,
135 Real32 fov
= getFov();
137 // catch some illegal cases
138 if(fov
< 0 || width
== 0 || height
== 0)
140 result
.setIdentity();
144 // try to be nice to people giving degrees...
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
);
156 SWARNING
<< "MatrixPerspective: near " << rNear
<< " > far " << rFar
157 << "!\n" << std::endl
;
158 result
.setIdentity();
162 if(fov
<= TypeTraits
<Real32
>::getDefaultEps())
164 SWARNING
<< "MatrixPerspective: fov " << fov
<< " very small!\n"
166 result
.setIdentity();
170 if(osgAbs(rNear
- rFar
) < TypeTraits
<Real32
>::getDefaultEps())
172 SWARNING
<< "MatrixPerspective: near " << rNear
<< " ~= far " << rFar
173 << "!\n" << std::endl
;
174 result
.setIdentity();
178 if(aspect
< TypeTraits
<Real32
>::getDefaultEps())
180 SWARNING
<< "MatrixPerspective: aspect ratio " << aspect
181 << " very small!\n" << std::endl
;
182 result
.setIdentity();
186 Real32 x
= ct
* rNear
;
187 Real32 y
= ct
* rNear
;
188 UInt32 fovMode
= getFovMode();
201 if(width
* getAspect() >= height
)
212 result
.setIdentity();
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
,
233 MatrixFrustum( result
,
234 -x
* (1.f
+ principalPointX
),
235 x
* (1.f
- principalPointX
),
236 -y
* (1.f
+ principalPointY
),
237 y
* (1.f
- principalPointY
),
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
;