1 #region Using Statements
3 using System.Collections.Generic;
6 using Microsoft.Xna.Framework;
7 using Microsoft.Xna.Framework.Input;
8 using Microsoft.Xna.Framework.Graphics;
20 protected Vector3 cameraPosition;
21 protected Vector3 cameraTarget;
24 protected Vector3 upVector;
27 private float fieldOfView;
28 private float aspectRatio;
29 private float nearPlaneDistance;
30 private float farPlaneDistance;
33 protected Matrix worldMatrix;
34 protected Matrix viewMatrix;
35 protected Matrix projectionMatrix;
36 protected Matrix viewProjectionMatrix;
39 protected bool needUpdateView;
40 protected bool needUpdateProjection;
41 protected bool needUpdateViewProjection;
44 protected Vector3 cameraInput;
45 private bool perspectiveProj;
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; } }
74 public bool SetPerspectiveProjecton { set {
if (value != perspectiveProj) { perspectiveProj = value; needUpdateProjection =
true; } } }
78 public int[]
SetClientSize { set { clientSize = value; aspectRatio = (float)clientSize[0] / (
float)clientSize[1]; needUpdateProjection =
true; } }
80 public Viewport Viewport {
get {
return viewport; } set { viewport = value; } }
86 public Matrix
World {
get {
return worldMatrix; } set { worldMatrix = value; } }
90 public Matrix
View {
get {
return viewMatrix; } }
94 public Matrix
Projection {
get {
return projectionMatrix; } }
108 clientSize =
new int[2];
113 cameraPosition =
new Vector3(0.0f, 0.0f, 1.0f);
114 cameraTarget = Vector3.Zero;
115 upVector = Vector3.Up;
117 fieldOfView = MathHelper.PiOver4;
119 aspectRatio = (float)clientSize[0] / (
float)clientSize[1];
120 nearPlaneDistance = 0.1f;
121 farPlaneDistance = 1000.0f;
137 needUpdateView =
true;
138 needUpdateProjection =
true;
139 needUpdateViewProjection =
true;
146 public virtual void Update()
155 if (needUpdateProjection)
158 if (needUpdateViewProjection)
160 viewProjectionMatrix = viewMatrix * projectionMatrix;
161 needUpdateViewProjection =
false;
171 viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraTarget, upVector);
173 needUpdateView =
false;
174 needUpdateViewProjection =
true;
182 if (!perspectiveProj)
184 projectionMatrix = Matrix.CreateOrthographic(clientSize[0]/ 235,
185 clientSize[1]/ 235, nearPlaneDistance, farPlaneDistance);
190 float yScale = 1.0f / (float)Math.Tan(fieldOfView / 2.0f);
191 float xScale = yScale / aspectRatio;
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;
203 needUpdateProjection =
false;
204 needUpdateViewProjection =
true;