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 visLU
00012 {
00016 class Camera : GameComponent
00017 {
00018 #region Variables
00019
00020
00021
00022
00023 protected Vector3 cameraPosition;
00024 protected Vector3 cameraTarget;
00025
00026
00027
00028
00029 protected Vector3 upVector;
00030
00031
00032 private float fieldOfView;
00033 private float aspectRatio;
00034 private float nearPlaneDistance;
00035 private float farPlaneDistance;
00036
00037
00038 protected Matrix viewMatrix;
00039 protected Matrix projectionMatrix;
00040 protected Matrix viewProjectionMatrix;
00041
00042
00043 protected bool needUpdateView;
00044 protected bool needUpdateProjection;
00045 protected bool needUpdateViewProjection;
00046
00047 private bool perspectiveProj = true;
00048
00049 #endregion
00050
00051 #region Properties
00052
00056 public Vector3 CameraPosition { get { return cameraPosition; } set { cameraPosition = value; needUpdateView = true; } }
00060 public Vector3 CameraTarget { get { return cameraTarget; } set { cameraTarget = value; needUpdateView = true; } }
00064 public Vector3 CameraUpVector { get { return upVector; } set { upVector = value; needUpdateView = true; } }
00065
00069 public Matrix View { get { return viewMatrix; } }
00073 public Matrix Projection { get { return projectionMatrix; } }
00077 public Matrix ViewProjection { get { return viewProjectionMatrix; } }
00078 #endregion
00079
00080 #region Constructor
00081
00082
00083
00084
00085 public Camera(Game _game)
00086 : base(_game)
00087 {
00088
00089
00090
00091 cameraPosition = new Vector3(0.0f, 0.0f, 10.0f);
00092 cameraTarget = Vector3.Zero;
00093 upVector = Vector3.Up;
00094
00095 fieldOfView = MathHelper.Pi / 4;
00096 aspectRatio = 4 / 3;
00097 if (!GameProperties.Instance.fullscreen) aspectRatio = 4 / 3;
00098 else aspectRatio = (float)GameProperties.Instance.fullscreenWidth / (float)GameProperties.Instance.fullscreenHeight;
00099 nearPlaneDistance = 0.1f;
00100 farPlaneDistance = 10000.0f;
00101
00102 needUpdateView = true;
00103 needUpdateProjection = true;
00104 needUpdateViewProjection = true;
00105
00106 }
00107 #endregion
00108
00109 #region Update
00110
00111
00112
00113
00114 public override void Update(GameTime gameTime)
00115 {
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 if (needUpdateView) updateView();
00130
00131 if ((needUpdateProjection) || GameProperties.Instance.perspectiveProjection != perspectiveProj)
00132 updateProjection();
00133
00134 if (needUpdateViewProjection)
00135 {
00136 viewProjectionMatrix = viewMatrix * projectionMatrix;
00137 needUpdateViewProjection = false;
00138 }
00139 }
00140
00141 protected virtual void updateView()
00142 {
00143
00144
00145
00146
00147
00148 viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraTarget, upVector);
00149
00150 needUpdateView = false;
00151 needUpdateViewProjection = true;
00152 }
00153
00154 protected void updateProjection()
00155 {
00156 if (GameProperties.Instance.perspectiveProjection == false)
00157 {
00158 projectionMatrix = Matrix.CreateOrthographic(Game.Window.ClientBounds.Width / 235,
00159 Game.Window.ClientBounds.Height / 235, nearPlaneDistance, farPlaneDistance);
00160 perspectiveProj = false;
00161 }
00162 else
00163 {
00164 projectionMatrix = Matrix.CreatePerspectiveFieldOfView(fieldOfView, aspectRatio,
00165 nearPlaneDistance, farPlaneDistance);
00166 perspectiveProj = true;
00167 }
00168
00169 needUpdateProjection = false;
00170 needUpdateViewProjection = true;
00171 }
00172
00173
00174 #endregion
00175 }
00176 }