Desktop/Dev/Cameras/Cameras/Projection.cs

Go to the documentation of this file.
00001 using System;
00002 using Microsoft.Xna.Framework;
00003 
00004 namespace Mindshifter
00005 {
00009     public enum ProjectionType
00010     {
00011         Perspective,
00012         Orthographic,
00013     }
00014 
00021     public class Projection
00022     {
00023         private Matrix          mMatrix;                // Projection matrix calculated from parameterization
00024 
00025         private ProjectionType  mType;                  // Type of projection, perspective or orthographic
00026 
00027         private float           mFieldOfView;           // Field of view for perspective projection
00028         private float           mAspectRatio;           // Aspect ratio for perspective projection
00029 
00030         private Rectangle       mRectangle;             // Rectangle for ortho projection
00031 
00032         private float           mNearPlaneDistance;     // Near frustum plane distance
00033         private float           mFarPlaneDistance;      // Far frustum plane distance
00034 
00035         private bool            mRequiresUpdate;        // Set when the projection matrix requires an update
00036 
00040         public Matrix Matrix
00041         {
00042             get 
00043             {
00044                 if (mRequiresUpdate)
00045                     BuildMatrix();
00046 
00047                 return mMatrix; 
00048             }
00049         }
00050 
00054         public ProjectionType Type
00055         {
00056             get { return mType; }
00057             set
00058             {
00059                 mType = value;
00060                 mRequiresUpdate = true;
00061             }
00062         }
00063 
00067         public float FieldOfView
00068         {
00069             get { return mFieldOfView; }
00070             set 
00071             { 
00072                 mFieldOfView = value;
00073                 mRequiresUpdate = true;
00074             }
00075         }
00076 
00080         public float AspectRatio
00081         {
00082             get { return mAspectRatio; }
00083             set
00084             {
00085                 mAspectRatio = value;
00086                 mRequiresUpdate = true;
00087             }
00088         }
00089 
00093         public Rectangle Rectangle
00094         {
00095             get { return mRectangle; }
00096             set 
00097             { 
00098                 mRectangle = value;
00099                 mRequiresUpdate = true;
00100             }
00101         }
00102 
00106         public int OffsetX
00107         {
00108             get { return mRectangle.X; }
00109             set 
00110             { 
00111                 mRectangle.X = value;
00112                 mRequiresUpdate = true;
00113             }
00114         }
00115 
00119         public int OffsetY
00120         {
00121             get { return mRectangle.Y; }
00122             set 
00123             { 
00124                 mRectangle.Y = value;
00125                 mRequiresUpdate = true;
00126             }
00127         }
00128 
00132         public int Width
00133         {
00134             get { return mRectangle.Width; }
00135             set
00136             {
00137                 mRectangle.Width = value;
00138                 mRequiresUpdate = true;
00139             }
00140         }
00141 
00145         public int Height
00146         {
00147             get { return mRectangle.Height; }
00148             set
00149             {
00150                 mRectangle.Height = value;
00151                 mRequiresUpdate = true;
00152             }
00153         }
00154 
00158         public float NearPlaneDistance
00159         {
00160             get { return mNearPlaneDistance; }
00161             set
00162             {
00163                 mNearPlaneDistance = value;
00164                 mRequiresUpdate = true;
00165             }
00166         }
00167 
00171         public float FarPlaneDistance
00172         {
00173             get { return mFarPlaneDistance; }
00174             set
00175             {
00176                 mFarPlaneDistance = value;
00177                 mRequiresUpdate = true;
00178             }
00179         }
00180 
00189         public Projection(float fov, float aspect, float near, float far)
00190         {
00191             mFieldOfView = fov;
00192             mAspectRatio = aspect;
00193             mNearPlaneDistance = near;
00194             mFarPlaneDistance = far;
00195 
00196             mType = ProjectionType.Perspective;
00197 
00198             mRequiresUpdate = true;
00199         }
00200 
00209         public Projection(int width, int height, float near, float far)
00210             : this(0, 0, width, height, near, far)
00211         {
00212 
00213         }
00214 
00225         public Projection(int x, int y, int width, int height, float near, float far)
00226         {
00227             mRectangle = new Rectangle(x, y, width, height);
00228             mNearPlaneDistance = near;
00229             mFarPlaneDistance = far;
00230 
00231             mType = ProjectionType.Orthographic;
00232 
00233             mRequiresUpdate = true;
00234         }
00235 
00243         public Projection(Rectangle rect, float near, float far)
00244         {
00245             mRectangle = rect;
00246             mNearPlaneDistance = near;
00247             mFarPlaneDistance = far;
00248 
00249             mType = ProjectionType.Orthographic;
00250 
00251             mRequiresUpdate = true;
00252         }
00253 
00254         private void BuildMatrix()
00255         {
00256             switch (mType)
00257             {
00258                 case ProjectionType.Perspective:
00259                     {
00260                         Matrix.CreatePerspectiveFieldOfView(mFieldOfView, mAspectRatio, mNearPlaneDistance,
00261                             mFarPlaneDistance, out mMatrix);
00262                     }
00263                     break;
00264 
00265                 case ProjectionType.Orthographic:
00266                     {
00267                         Matrix.CreateOrthographicOffCenter(mRectangle.Left, mRectangle.Right, mRectangle.Bottom,
00268                             mRectangle.Top, mNearPlaneDistance, mFarPlaneDistance, out mMatrix);
00269                     }
00270                     break;
00271             }
00272 
00273             mRequiresUpdate = false;
00274         }
00275 
00283         public void SetPerspective(float fov, float aspect, float near, float far)
00284         {
00285             mType = ProjectionType.Perspective;
00286 
00287             mFieldOfView = fov;
00288             mAspectRatio = aspect;
00289             mNearPlaneDistance = near;
00290             mFarPlaneDistance = far;
00291 
00292             mRequiresUpdate = true;
00293         }
00294 
00301         public void SetOrthographic(Rectangle rect, float near, float far)
00302         {
00303             mType = ProjectionType.Orthographic;
00304 
00305             mRectangle = rect;
00306 
00307             mRequiresUpdate = true;
00308         }
00309     }
00310 }

Generated on Mon Mar 31 03:29:21 2008 for Cameras by  doxygen 1.5.1-p1