OSGPerspectiveCamera.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 <cstdlib>
00044 #include <cstdio>
00045 #include <cmath>
00046
00047 #include "OSGConfig.h"
00048
00049 #include "OSGBaseTypes.h"
00050 #include "OSGLog.h"
00051 #include "OSGVector.h"
00052 #include "OSGMatrix.h"
00053 #include "OSGMatrixUtility.h"
00054
00055 #include "OSGViewport.h"
00056 #include "OSGCamera.h"
00057 #include "OSGWindow.h"
00058 #include "OSGBackground.h"
00059 #include "OSGPerspectiveCamera.h"
00060
00061 OSG_USING_NAMESPACE
00062
00063 // Documentation for this class is emited in the
00064 // OSGPerspectiveCameraBase.cpp file.
00065 // To modify it, please change the .fcd file (OSGPerspectiveCamera.fcd) and
00066 // regenerate the base file.
00067
00068 /***************************************************************************\
00069  *                           Class methods                                 *
00070 \***************************************************************************/
00071
00072 /*-------------------------------------------------------------------------*\
00073  -  private                                                                -
00074 \*-------------------------------------------------------------------------*/
00075
00076 void PerspectiveCamera::initMethod(InitPhase ePhase)
00077 {
00078     Inherited::initMethod(ePhase);
00079 }
00080
00081 /***************************************************************************\
00082  *                           Instance methods                              *
00083 \***************************************************************************/
00084
00085 /*-------------------------------------------------------------------------*\
00086  -  public                                                                 -
00087 \*-------------------------------------------------------------------------*/
00088
00089
00090 /*------------- constructors & destructors --------------------------------*/
00091
00092 PerspectiveCamera::PerspectiveCamera(void) :
00093     Inherited()
00094 {
00095 }
00096
00097 PerspectiveCamera::PerspectiveCamera(const PerspectiveCamera &source) :
00098     Inherited(source)
00099 {
00100 }
00101
00102 PerspectiveCamera::~PerspectiveCamera(void)
00103 {
00104 }
00105
00106 void PerspectiveCamera::changed(ConstFieldMaskArg whichField,
00107                                 UInt32            origin,
00108                                 BitVector         details)
00109 {
00110     Inherited::changed(whichField, origin, details);
00111 }
00112
00113 /*-------------------------- your_category---------------------------------*/
00114
00115
00116
00117 void PerspectiveCamera::getProjection(Matrixr &result,
00118                                       UInt32   width,
00119                                       UInt32   height)
00120 {
00121     Real32 fov = getFov();
00122
00123     // catch some illegal cases
00124
00125     if(fov < 0 || width == 0 || height == 0)
00126     {
00127         result.setIdentity();
00128         return;
00129     }
00130
00131     // try to be nice to people giving degrees...
00132
00133     if(fov > Pi)
00134         fov = osgDegree2Rad(fov);
00135
00136
00137     Real32 rNear  = getNear();
00138     Real32 rFar   = getFar ();
00139     Real32 aspect = Real32(width) / Real32(height) * getAspect();
00140     Real32 ct     = osgTan(fov / 2.f);
00141
00142     if(rNear > rFar)
00143     {
00144         SWARNING << "MatrixPerspective: near " << rNear << " > far " << rFar
00145                  << "!\n" << std::endl;
00146         result.setIdentity();
00147         return;
00148     }
00149
00150     if(fov <= Eps)
00151     {
00152         SWARNING << "MatrixPerspective: fov " << fov << " very small!\n"
00153                  << std::endl;
00154         result.setIdentity();
00155         return;
00156     }
00157
00158     if(osgAbs(rNear - rFar) < Eps)
00159     {
00160         SWARNING << "MatrixPerspective: near " << rNear << " ~= far " << rFar
00161                  << "!\n" << std::endl;
00162         result.setIdentity();
00163         return;
00164     }
00165
00166     if(aspect < Eps)
00167     {
00168         SWARNING << "MatrixPerspective: aspect ratio " << aspect
00169                  << " very small!\n" << std::endl;
00170         result.setIdentity();
00171         return;
00172     }
00173
00174     Real32 x = ct * rNear;
00175     Real32 y = ct * rNear;
00176
00177     UInt32 fovMode = getFovMode();
00178
00179     switch (fovMode)
00180     {
00181         case VerticalFoV:
00182             x *= aspect;
00183             break;
00184
00185         case HorizontalFoV:
00186             y /= aspect;
00187             break;
00188
00189         case SmallerFoV:
00190             if(width * getAspect() >= height)
00191             {
00192                 x *= aspect;
00193             }
00194             else
00195             {
00196                 y /= aspect;
00197             }
00198             break;
00199
00200         default:
00201             result.setIdentity();
00202             return;
00203     }
00204
00205     MatrixFrustum( result,
00206                   -x,
00207                    x,
00208                   -y,
00209                    y,
00210                    rNear,
00211                    rFar);
00212
00213 }
00214
00215
00216 /*------------------------------- dump ----------------------------------*/
00217
00218 void PerspectiveCamera::dump(      UInt32    OSG_CHECK_ARG(uiIndent),
00219                              const BitVector OSG_CHECK_ARG(bvFlags)) const
00220 {
00221     SLOG << "Dump PerspectiveCamera NI" << std::endl;
00222 }
00223