Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __SP_SCENE_PROJECTION_H__
00009 #define __SP_SCENE_PROJECTION_H__
00010
00011
00012 #include "Base/spStandard.hpp"
00013 #include "Base/spMathCore.hpp"
00014 #include "Base/spDimensionRect2D.hpp"
00015 #include "Base/spDimensionMatrix4.hpp"
00016
00017
00018 namespace sp
00019 {
00020 namespace scene
00021 {
00022
00023
00029 template <typename T> class Projection3D
00030 {
00031
00032 public:
00033
00034
00035
00036 static const dim::rect2di DEFAULT_VIEWPORT;
00037 static const T DEFAULT_NEAR;
00038 static const T DEFAULT_FAR;
00039 static const T DEFAULT_FOV;
00040
00041
00042
00043 Projection3D() :
00044 Viewport_ (Projection3D<T>::DEFAULT_VIEWPORT ),
00045 NearPlane_ (Projection3D<T>::DEFAULT_NEAR ),
00046 FarPlane_ (Projection3D<T>::DEFAULT_FAR ),
00047 FieldOfView_(Projection3D<T>::DEFAULT_FOV ),
00048 IsOrtho_ (false ),
00049 HasChanged_ (true )
00050 {
00051 }
00052 Projection3D(
00053 const dim::rect2di &Viewport,
00054 const T &NearPlane = Projection3D<T>::DEFAULT_NEAR,
00055 const T &FarPlane = Projection3D<T>::DEFAULT_FAR,
00056 const T &FieldOfView = Projection3D<T>::DEFAULT_FOV,
00057 bool IsOrtho = false) :
00058 Viewport_ (Viewport ),
00059 NearPlane_ (NearPlane ),
00060 FarPlane_ (FarPlane ),
00061 FieldOfView_(FieldOfView),
00062 IsOrtho_ (IsOrtho ),
00063 HasChanged_ (true )
00064 {
00065 }
00066 Projection3D(const Projection3D<T> &Other) :
00067 Viewport_ (Other.Viewport_ ),
00068 NearPlane_ (Other.NearPlane_ ),
00069 FarPlane_ (Other.FarPlane_ ),
00070 FieldOfView_ (Other.FieldOfView_ ),
00071 IsOrtho_ (Other.IsOrtho_ ),
00072 ProjMatrixLH_ (Other.ProjMatrixLH_),
00073 ProjMatrixRH_ (Other.ProjMatrixRH_),
00074 HasChanged_ (Other.HasChanged_ )
00075 {
00076 }
00077 ~Projection3D()
00078 {
00079 }
00080
00081
00082
00083 Projection3D<T>& operator = (const Projection3D<T> &Other)
00084 {
00085 Viewport_ = Other.Viewport_;
00086 NearPlane_ = Other.NearPlane_;
00087 FarPlane_ = Other.FarPlane_;
00088 FieldOfView_ = Other.FieldOfView_;
00089 IsOrtho_ = Other.IsOrtho_;
00090 ProjMatrixLH_ = Other.ProjMatrixLH_;
00091 ProjMatrixRH_ = Other.ProjMatrixRH_;
00092 HasChanged_ = Other.HasChanged_;
00093 return *this;
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 void update() const
00105 {
00106
00107 if (IsOrtho_)
00108 {
00109 ProjMatrixLH_.setOrthoLH(
00110 static_cast<f32>(Viewport_.Left) / FieldOfView_,
00111 static_cast<f32>(Viewport_.Left + Viewport_.Right) / FieldOfView_,
00112 static_cast<f32>(Viewport_.Top) / FieldOfView_,
00113 static_cast<f32>(Viewport_.Top + Viewport_.Bottom) / FieldOfView_,
00114 NearPlane_, FarPlane_
00115 );
00116 ProjMatrixRH_.setOrthoRH(
00117 static_cast<f32>(Viewport_.Left) / FieldOfView_,
00118 static_cast<f32>(Viewport_.Left + Viewport_.Right) / FieldOfView_,
00119 static_cast<f32>(Viewport_.Top) / FieldOfView_,
00120 static_cast<f32>(Viewport_.Top + Viewport_.Bottom) / FieldOfView_,
00121 NearPlane_, FarPlane_
00122 );
00123 }
00124 else
00125 {
00126
00127 const f32 AspectRatio = static_cast<f32>(Viewport_.Right) / Viewport_.Bottom;
00128
00129
00130 ProjMatrixLH_.setPerspectiveLH(FieldOfView_, AspectRatio, NearPlane_, FarPlane_);
00131 ProjMatrixRH_.setPerspectiveRH(FieldOfView_, AspectRatio, NearPlane_, FarPlane_);
00132 }
00133
00134 HasChanged_ = false;
00135 }
00136
00142 const dim::matrix4<T>& getMatrixLH() const
00143 {
00144 if (HasChanged_)
00145 update();
00146 return ProjMatrixLH_;
00147 }
00148
00153 const dim::matrix4<T>& getMatrixRH() const
00154 {
00155 if (HasChanged_)
00156 update();
00157 return ProjMatrixRH_;
00158 }
00159
00160
00161
00163 inline void setViewport(const dim::rect2di &Viewport)
00164 {
00165 Viewport_ = Viewport;
00166 HasChanged_ = true;
00167 }
00169 inline const dim::rect2di& getViewport() const
00170 {
00171 return Viewport_;
00172 }
00173
00175 inline void setNearPlane(const T &NearPlane)
00176 {
00177 NearPlane_ = NearPlane;
00178 HasChanged_ = true;
00179 }
00181 inline const T& getNearPlane() const
00182 {
00183 return NearPlane_;
00184 }
00185
00187 inline void setFarPlane(const T &FarPlane)
00188 {
00189 FarPlane_ = FarPlane;
00190 HasChanged_ = true;
00191 }
00193 inline const T& getFarPlane() const
00194 {
00195 return FarPlane_;
00196 }
00197
00199 inline void setFOV(const T &FieldOfView)
00200 {
00201 FieldOfView_ = FieldOfView;
00202 HasChanged_ = true;
00203 }
00205 inline const T& getFOV() const
00206 {
00207 return FieldOfView_;
00208 }
00209
00211 inline void setZoom(const T &Zoom)
00212 {
00213 setFOV(math::ATan(T(2) / Zoom));
00214 }
00215 inline T getZoom() const
00216 {
00217 return T(1) / math::Tan(getFOV() / T(2));
00218 }
00219
00221 inline void setOrtho(bool Enable)
00222 {
00223 IsOrtho_ = Enable;
00224 HasChanged_ = true;
00225 }
00227 inline bool getOrtho() const
00228 {
00229 return IsOrtho_;
00230 }
00231
00232 private:
00233
00234
00235
00236 dim::rect2di Viewport_;
00237 T NearPlane_;
00238 T FarPlane_;
00239 T FieldOfView_;
00240 bool IsOrtho_;
00241
00242 mutable dim::matrix4<T> ProjMatrixLH_;
00243 mutable dim::matrix4<T> ProjMatrixRH_;
00244 mutable bool HasChanged_;
00245
00246 };
00247
00248 template <typename T> const dim::rect2di Projection3D<T>::DEFAULT_VIEWPORT = dim::rect2di(0, 0, 100, 100);
00249 template <typename T> const T Projection3D<T>::DEFAULT_NEAR = T(0.25);
00250 template <typename T> const T Projection3D<T>::DEFAULT_FAR = T(1000);
00251 template <typename T> const T Projection3D<T>::DEFAULT_FOV = T(74);
00252
00253
00254 typedef Projection3D<f32> Projection;
00255
00256
00257 }
00258
00259 }
00260
00261
00262 #endif
00263
00264
00265
00266