VIS2 SS2013 CVD DVR
 All Classes Namespaces Functions Enumerations Properties
Camera.cs
1 #region Using Statements
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6 using Microsoft.Xna.Framework;
7 using Microsoft.Xna.Framework.Input;
8 using Microsoft.Xna.Framework.Graphics;
9 #endregion
10 
11 namespace visLU
12 {
16  class Camera
17  {
18  #region Variables
19  //position
20  protected Vector3 cameraPosition;
21  protected Vector3 cameraTarget;
22 
23  //orientation vector
24  protected Vector3 upVector;
25 
26  //perspective projection parameters
27  private float fieldOfView;
28  private float aspectRatio;
29  private float nearPlaneDistance;
30  private float farPlaneDistance;
31 
32  // matrices
33  protected Matrix worldMatrix;
34  protected Matrix viewMatrix;
35  protected Matrix projectionMatrix;
36  protected Matrix viewProjectionMatrix;
37 
38  //need update
39  protected bool needUpdateView;
40  protected bool needUpdateProjection;
41  protected bool needUpdateViewProjection;
42 
43  //user input
44  protected Vector3 cameraInput;
45  private bool perspectiveProj;
46 
47  //window dimensions
48  int[] clientSize;
49  Viewport viewport;
50  #endregion
51 
52  #region Properties
53  //-------------------------------CAMERA POSITION------------------------------------------
57  public Vector3 CameraPosition { get { return cameraPosition; } set { cameraPosition = value; needUpdateView = true; } }
61  public Vector3 CameraTarget { get { return cameraTarget; } set { cameraTarget = value; needUpdateView = true; } }
65  public Vector3 CameraUpVector { get { return upVector; } set { upVector = value; needUpdateView = true; } }
69  public Vector3 CameraInput { set { cameraInput = value; needUpdateView = true; } }
70 
74  public bool SetPerspectiveProjecton { set { if (value != perspectiveProj) { perspectiveProj = value; needUpdateProjection = true; } } } //todo
78  public int[] SetClientSize { set { clientSize = value; aspectRatio = (float)clientSize[0] / (float)clientSize[1]; needUpdateProjection = true; } }
79 
80  public Viewport Viewport { get { return viewport; } set { viewport = value; } }
81 
82  //--------------------------------RENDER SCENE----------------------------------------------
86  public Matrix World { get { return worldMatrix; } set { worldMatrix = value; } }
90  public Matrix View { get { return viewMatrix; } }
94  public Matrix Projection { get { return projectionMatrix; } }
98  public Matrix ViewProjection { get { return viewProjectionMatrix; } }
99  #endregion
100 
101  #region Constructor
102  public Camera()
106  {
107  //get control dimensions
108  clientSize = new int[2];
109  clientSize[0] = GameProperties.Instance.XNADrawSurfaceWidth;//initial dimensions
110  clientSize[1] = GameProperties.Instance.XNADrawSurfaceHeight;
111 
112  //set default camera parameters
113  cameraPosition = new Vector3(0.0f, 0.0f, 1.0f);
114  cameraTarget = Vector3.Zero;
115  upVector = Vector3.Up;
116 
117  fieldOfView = MathHelper.PiOver4;
118  //fieldOfView = MathHelper.Pi / 3.0f;
119  aspectRatio = (float)clientSize[0] / (float)clientSize[1]; //4/3
120  nearPlaneDistance = 0.1f;
121  farPlaneDistance = 1000.0f;
122 
123  //user input
124  cameraInput = GameProperties.Instance.cameraInput;
125  perspectiveProj = GameProperties.Instance.perspectiveProjection;
126 
127  }
128  #endregion
129 
130  #region Initialize
131  public virtual void Initialize()
135  {
136  //init update
137  needUpdateView = true;
138  needUpdateProjection = true;
139  needUpdateViewProjection = true;
140 
141  Update();
142  }
143  #endregion
144 
145  #region Update
146  public virtual void Update()
151  {
152 
153  if (needUpdateView) updateView();
154 
155  if (needUpdateProjection)
157 
158  if (needUpdateViewProjection)
159  {
160  viewProjectionMatrix = viewMatrix * projectionMatrix;
161  needUpdateViewProjection = false;
162  }
163  }
164 
165 
169  protected virtual void updateView()
170  {
171  viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraTarget, upVector);
172 
173  needUpdateView = false;
174  needUpdateViewProjection = true;
175  }
176 
180  protected void updateProjection()
181  {
182  if (!perspectiveProj)
183  {
184  projectionMatrix = Matrix.CreateOrthographic(clientSize[0]/ 235,
185  clientSize[1]/ 235, nearPlaneDistance, farPlaneDistance);
186  }
187  else
188  {
189 
190  float yScale = 1.0f / (float)Math.Tan(fieldOfView / 2.0f);
191  float xScale = yScale / aspectRatio;
192  Matrix projTmp;
193 
194  projTmp.M11 = xScale; projTmp.M12 = 0.0f; projTmp.M13 = 0.0f; projTmp.M14 = 0.0f;
195  projTmp.M21 = 0.0f; projTmp.M22 = yScale; projTmp.M23 = 0.0f; projTmp.M24 = 0.0f;
196  projTmp.M31 = 0.0f; projTmp.M32 = 0.0f; projTmp.M33 = farPlaneDistance / (nearPlaneDistance - farPlaneDistance); projTmp.M34 = -1.0f;
197  projTmp.M41 = 0.0f; projTmp.M42 = 0.0f; projTmp.M43 = nearPlaneDistance * farPlaneDistance / (nearPlaneDistance - farPlaneDistance); projTmp.M44 = 0.0f;
198  projectionMatrix = projTmp;
199 
200  //projectionMatrix = Matrix.CreatePerspectiveFieldOfView(fieldOfView, aspectRatio, nearPlaneDistance, farPlaneDistance);
201  }
202 
203  needUpdateProjection = false;
204  needUpdateViewProjection = true;
205  }
206 
207 
208 
209  #endregion
210  }
211 }