• Main Page
  • Packages
  • Classes
  • Files
  • File List

trunk/visLU/Camera.cs

Go to the documentation of this file.
00001 #region using statements
00002 using System;
00003 using System.Collections.Generic;
00004 using System.Linq;
00005 using System.Text;
00006 using Microsoft.Xna.Framework;
00007 using Microsoft.Xna.Framework.Input;
00008 using Microsoft.Xna.Framework.Graphics;
00009 #endregion
00010 
00011 namespace visLU2
00012 {
00016     class Camera : GameComponent
00017     {
00018         #region Variables
00019 
00020         //private Game game;
00021 
00022         //position
00023         protected Vector3 cameraPosition;
00024         protected Vector3 cameraTarget;
00025 
00026         //orientation vectors 
00027         //private Vector3 headingVector; //z axis
00028         //private Vector3 strafeVector; //x axis
00029         protected Vector3 upVector; //y axis
00030 
00031         //perspective projection parameters
00032         private float fieldOfView;
00033         private float aspectRatio;
00034         private float nearPlaneDistance;
00035         private float farPlaneDistance;
00036 
00037         // matrices
00038         protected Matrix viewMatrix;
00039         protected Matrix projectionMatrix;
00040         protected Matrix viewProjectionMatrix;
00041 
00042         //need update
00043         protected bool needUpdateView;
00044         protected bool needUpdateProjection;
00045         protected bool needUpdateViewProjection;
00046 
00047         private bool perspectiveProj = true;
00048 
00049         private float zoom;
00050         #endregion
00051 
00052         #region Properties
00053         //-------------------------------CAMERA POSITION------------------------------------------
00057         public Vector3 CameraPosition { get { return cameraPosition; } set { cameraPosition = value; needUpdateView = true; } }
00061         public Vector3 CameraTarget { get { return cameraTarget; } set { cameraTarget = value; needUpdateView = true; } }
00065         public Vector3 CameraUpVector { get { return upVector; } set { upVector = value; needUpdateView = true; } }
00066         //--------------------------------RENDER SCENE----------------------------------------------
00070         public Matrix View { get { return viewMatrix; } }
00074         public Matrix Projection { get { return projectionMatrix; } }
00078         public Matrix ViewProjection { get { return viewProjectionMatrix; } }
00079         //------------------------------------------------------------------------------------------
00080         public float Zoom
00081         {
00082             set { zoom = value; needUpdateProjection = true; }
00083             get { return zoom; }
00084         }
00085         #endregion
00086 
00087         #region Constructor
00088 
00089 
00090 
00091 
00092         public Camera(Game _game)
00093             : base(_game)
00094         {
00095             //game = _game;
00096 
00097             //default camera parameters
00098             cameraPosition = new Vector3(0.0f, 0.0f, 0.0001f);
00099             cameraTarget = Vector3.Zero;
00100             upVector = Vector3.Up;
00101 
00102             fieldOfView = MathHelper.Pi / 4;
00103             aspectRatio = 4 / 3;
00104             if (!GameProperties.Instance.fullscreen) aspectRatio = 4 / 3;
00105             else aspectRatio = (float)GameProperties.Instance.fullscreenWidth / (float)GameProperties.Instance.fullscreenHeight;
00106             zoom = 50;
00107             nearPlaneDistance = 0.0f;
00108             farPlaneDistance = 10000.0f;
00109 
00110             needUpdateView = true;
00111             needUpdateProjection = true;
00112             needUpdateViewProjection = true;
00113 
00114         }
00115         #endregion
00116 
00117         #region Update
00118 
00119 
00120 
00121 
00122         public override void Update(GameTime gameTime)
00123         {
00124             /*#region move camera
00125             MouseState currentMouseState = Mouse.GetState();
00126             MouseState lasttMouseState;
00127             if (currentMouseState.LeftButton = ButtonState.Pressed)
00128             {
00129                 float fX, fY;
00130                 fX = MathHelper.ToRadians(currentMouseState.X - lasttMouseState.X);
00131                 fY = MathHelper.ToRadians(currentMouseState.Y - lasttMouseState.Y);
00132 
00133                 viewMatrix *= Matrix.CreateRotationX(fX) * Matrix.CreateRotationY(-fY);
00134             }
00135             #endregion*/
00136 
00137             #region zoom
00138             if(zoom != GameProperties.Instance.cameraPosition.Z)
00139             {
00140                 if (GameProperties.Instance.cameraPosition.Z < 1)
00141                 {
00142                     GameProperties.Instance.cameraPosition.Z = 1;   
00143                 }
00144                 else if (GameProperties.Instance.cameraPosition.Z > Math.Max(Game.Window.ClientBounds.Width, Game.Window.ClientBounds.Height))
00145                 {
00146                     GameProperties.Instance.cameraPosition.Z = Math.Max(Game.Window.ClientBounds.Width, Game.Window.ClientBounds.Height);
00147                 }
00148                 zoom = GameProperties.Instance.cameraPosition.Z;
00149                 needUpdateProjection = true;
00150             }
00151             #endregion
00152 
00153             #region move
00154             if (cameraPosition.X != GameProperties.Instance.cameraPosition.X)
00155             {
00156                 MathHelper.Clamp(GameProperties.Instance.cameraPosition.X, 0, Game.Window.ClientBounds.Width);
00157                 cameraPosition.X = GameProperties.Instance.cameraPosition.X;
00158                 cameraTarget.X = GameProperties.Instance.cameraPosition.X;
00159                 needUpdateView = true;
00160             }
00161             if (cameraPosition.Y != GameProperties.Instance.cameraPosition.Y)
00162             {
00163                 MathHelper.Clamp(GameProperties.Instance.cameraPosition.Y, 0, Game.Window.ClientBounds.Height);
00164                 cameraPosition.Y = GameProperties.Instance.cameraPosition.Y;
00165                 cameraTarget.Y = GameProperties.Instance.cameraPosition.Y;
00166                 needUpdateView = true;
00167             }
00168             #endregion
00169 
00170             if (needUpdateView) updateView();
00171 
00172             if ((needUpdateProjection) || GameProperties.Instance.perspectiveProjection != perspectiveProj) 
00173                 updateProjection();
00174             
00175             if (needUpdateViewProjection)
00176             {
00177                 viewProjectionMatrix = viewMatrix * projectionMatrix;
00178                 needUpdateViewProjection = false;
00179             }
00180         }
00181 
00182         protected virtual void updateView()
00183         {
00184 
00185             viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraTarget, upVector);
00186 
00187             needUpdateView = false;
00188             needUpdateViewProjection = true;
00189         }
00190 
00191         protected void updateProjection()
00192         {
00193             if (!GameProperties.Instance.perspectiveProjection)
00194             {
00195                 projectionMatrix = Matrix.CreateOrthographic(Game.Window.ClientBounds.Width / zoom,
00196                     Game.Window.ClientBounds.Height / zoom, nearPlaneDistance, farPlaneDistance);
00197                 perspectiveProj = false;
00198             }
00199             else
00200             {
00201                 projectionMatrix = Matrix.CreatePerspectiveFieldOfView(fieldOfView, aspectRatio,
00202                     nearPlaneDistance, farPlaneDistance);
00203                 perspectiveProj = true;
00204             }
00205 
00206             needUpdateProjection = false;
00207             needUpdateViewProjection = true;
00208         }
00209 
00210 
00211         #endregion
00212     }
00213 }

Generated on Wed Jan 19 2011 21:59:17 for flowvis-2 by  doxygen 1.7.2