OSGOffCenterPerspectiveCamera.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright(C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038
00039 //---------------------------------------------------------------------------
00040 //  Includes
00041 //---------------------------------------------------------------------------
00042
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045 #include <math.h>
00046
00047 #include "OSGMatrixUtility.h"
00048 #include "OSGOffCenterPerspectiveCamera.h"
00049
00050 OSG_USING_NAMESPACE
00051
00052
00053 /***************************************************************************\
00054  *                            Description                                  *
00055 \***************************************************************************/
00056
00081 /***************************************************************************\
00082  *                           Class methods                                 *
00083 \***************************************************************************/
00084
00085 /*-------------------------------------------------------------------------*\
00086  -  private                                                                -
00087 \*-------------------------------------------------------------------------*/
00088
00089 void OffCenterPerspectiveCamera::initMethod(InitPhase ePhase)
00090 {
00091     Inherited::initMethod(ePhase);
00092 }
00093
00094 /***************************************************************************\
00095  *                           Instance methods                              *
00096 \***************************************************************************/
00097
00098 /*-------------------------------------------------------------------------*\
00099  -  public                                                                 -
00100 \*-------------------------------------------------------------------------*/
00101
00102
00103 /*------------- constructors & destructors --------------------------------*/
00104
00105 OffCenterPerspectiveCamera::OffCenterPerspectiveCamera(void) :
00106     Inherited()
00107 {
00108 }
00109
00110 OffCenterPerspectiveCamera::OffCenterPerspectiveCamera(
00111     const OffCenterPerspectiveCamera &source) :
00112
00113     Inherited(source)
00114 {
00115 }
00116
00117 OffCenterPerspectiveCamera::~OffCenterPerspectiveCamera(void)
00118 {
00119 }
00120
00121 void OffCenterPerspectiveCamera::changed(ConstFieldMaskArg whichField,
00122                                          UInt32            origin,
00123                                          BitVector         details)
00124 {
00125     Inherited::changed(whichField, origin, details);
00126 }
00127
00128 /*-------------------------- your_category---------------------------------*/
00129
00130
00131 void OffCenterPerspectiveCamera::getProjection(Matrix &result,
00132                                                UInt32  width,
00133                                                UInt32  height)
00134 {
00135     Real32 fov = getFov();
00136
00137     // catch some illegal cases
00138     if(fov < 0 || width == 0 || height == 0)
00139     {
00140         result.setIdentity();
00141         return;
00142     }
00143
00144     // try to be nice to people giving degrees...
00145     if(fov > Pi)
00146         fov = osgDegree2Rad(fov);
00147
00148
00149     Real32 rNear  = getNear();
00150     Real32 rFar   = getFar();
00151     Real32 aspect = Real32(width) / Real32(height) * getAspect();
00152     Real32 ct     = osgTan(fov / 2.f);
00153
00154     if(rNear > rFar)
00155     {
00156         SWARNING << "MatrixPerspective: near " << rNear << " > far " << rFar
00157                  << "!\n" << std::endl;
00158         result.setIdentity();
00159         return;
00160     }
00161
00162     if(fov <= Eps)
00163     {
00164         SWARNING << "MatrixPerspective: fov " << fov << " very small!\n"
00165                  << std::endl;
00166         result.setIdentity();
00167         return;
00168     }
00169
00170     if(osgAbs(rNear - rFar) < Eps)
00171     {
00172         SWARNING << "MatrixPerspective: near " << rNear << " ~= far " << rFar
00173                  << "!\n" << std::endl;
00174         result.setIdentity();
00175         return;
00176     }
00177
00178     if(aspect < Eps)
00179     {
00180         SWARNING << "MatrixPerspective: aspect ratio " << aspect
00181                  << " very small!\n" << std::endl;
00182         result.setIdentity();
00183         return;
00184     }
00185
00186     Real32 x       = ct * rNear;
00187     Real32 y       = ct * rNear;
00188     UInt32 fovMode = getFovMode();
00189
00190     switch (fovMode)
00191     {
00192         case VerticalFoV:
00193             x *= aspect;
00194             break;
00195
00196         case HorizontalFoV:
00197             y /= aspect;
00198             break;
00199
00200         case SmallerFoV:
00201             if(width * getAspect() >= height)
00202             {
00203                 x *= aspect;
00204             }
00205             else
00206             {
00207                 y /= aspect;
00208             }
00209             break;
00210
00211         default:
00212             result.setIdentity();
00213             return;
00214     }
00215
00216     Real32 principalPointX = getPrincipalPoint()[0];
00217     Real32 principalPointY = getPrincipalPoint()[1];
00218
00219     // if principal point (x,y) is default (==(0,0)) everything works
00220     // like before or rather for an symmetical camera
00221     if ((principalPointX == 0.f) && (principalPointY == 0.f))
00222     {
00223         MatrixFrustum( result,
00224                       -x,
00225                        x,
00226                       -y,
00227                        y,
00228                        rNear,
00229                        rFar);
00230     }
00231     else
00232     {
00233         MatrixFrustum( result,
00234                       -x * (1.f + principalPointX),
00235                        x * (1.f - principalPointX),
00236                       -y * (1.f + principalPointY),
00237                        y * (1.f - principalPointY),
00238                        rNear,
00239                        rFar);
00240     }
00241 }
00242
00243
00244 /*------------------------------- dump ----------------------------------*/
00245
00246 void OffCenterPerspectiveCamera::dump(
00247           UInt32    OSG_CHECK_ARG(uiIndent),
00248     const BitVector OSG_CHECK_ARG(bvFlags)) const
00249 {
00250     SLOG << "Dump OffCenterPerspectiveCamera NI" << std::endl;
00251 }
00252
00253