OSGPerspectiveCamera.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
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
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 void PerspectiveCamera::initMethod(InitPhase ePhase)
00077 {
00078 Inherited::initMethod(ePhase);
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
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
00114
00115
00116
00117 void PerspectiveCamera::getProjection(Matrixr &result,
00118 UInt32 width,
00119 UInt32 height)
00120 {
00121 Real32 fov = getFov();
00122
00123
00124
00125 if(fov < 0 || width == 0 || height == 0)
00126 {
00127 result.setIdentity();
00128 return;
00129 }
00130
00131
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
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