CameraBase.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // CameraBase.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using Microsoft.Xna.Framework;
  13. using System.Text;
  14. using RobotGameData.GameInterface;
  15. #endregion
  16. namespace RobotGameData.Camera
  17. {
  18. /// <summary>
  19. /// This is the base class of every camera.
  20. /// It can configure View matrix and Projection matrix.
  21. /// </summary>
  22. public class CameraBase : GameNode
  23. {
  24. #region Fields
  25. /// <summary>
  26. /// Field of View
  27. /// </summary>
  28. float fov = MathHelper.PiOver4;
  29. float screenWidth = 0;
  30. float screenHeight = 0;
  31. float aspectRatio = 0;
  32. /// <summary>
  33. /// The Camera's near distance
  34. /// </summary>
  35. float near = 1.0f;
  36. /// <summary>
  37. /// The Camera's far distance
  38. /// </summary>
  39. float far = 1000.0f;
  40. /// <summary>
  41. /// The Camera position
  42. /// </summary>
  43. Vector3 position = Vector3.Zero;
  44. /// <summary>
  45. /// The Camera old position
  46. /// </summary>
  47. Vector3 oldPosition = Vector3.Zero;
  48. /// <summary>
  49. /// The Camera up vector
  50. /// </summary>
  51. Vector3 up = Vector3.Up;
  52. /// <summary>
  53. /// The Camera right vector
  54. /// </summary>
  55. Vector3 right = Vector3.Right;
  56. /// <summary>
  57. /// The Camera direction
  58. /// </summary>
  59. Vector3 direction = Vector3.Forward;
  60. /// <summary>
  61. /// The camera looks at the target position.
  62. /// </summary>
  63. Vector3 target = Vector3.Forward;
  64. /// <summary>
  65. /// The Camera velocity
  66. /// </summary>
  67. Vector3 velocity = Vector3.Zero;
  68. /// <summary>
  69. /// The projection matrix
  70. /// </summary>
  71. Matrix projectionMatrix = Matrix.Identity;
  72. /// <summary>
  73. /// The view matrix
  74. /// </summary>
  75. Matrix viewMatrix = Matrix.Identity;
  76. #endregion
  77. #region Properties
  78. public Matrix ProjectionMatrix
  79. {
  80. get { return projectionMatrix; }
  81. protected set { projectionMatrix = value; }
  82. }
  83. public Matrix ViewMatrix
  84. {
  85. get { return viewMatrix; }
  86. protected set { viewMatrix = value; }
  87. }
  88. public Vector3 Position
  89. {
  90. get { return position; }
  91. }
  92. public Vector3 Direction
  93. {
  94. get { return direction; }
  95. }
  96. public Vector3 Target
  97. {
  98. get { return target; }
  99. }
  100. public Vector3 Velocity
  101. {
  102. get { return velocity; }
  103. set { velocity = value; }
  104. }
  105. public Vector3 Up
  106. {
  107. get { return up; }
  108. }
  109. public Vector3 Right
  110. {
  111. get { return right; }
  112. }
  113. public float AspectRatio
  114. {
  115. get { return aspectRatio; }
  116. }
  117. public float FieldOfView
  118. {
  119. get { return fov; }
  120. }
  121. public float Near
  122. {
  123. get { return near; }
  124. }
  125. public float Far
  126. {
  127. get { return far; }
  128. }
  129. public float Width
  130. {
  131. get { return screenWidth; }
  132. }
  133. public float Height
  134. {
  135. get { return screenHeight; }
  136. }
  137. #endregion
  138. /// <summary>
  139. /// Constructor.
  140. /// </summary>
  141. public CameraBase()
  142. : base()
  143. {
  144. near = 1.0f;
  145. far = 1000.0f;
  146. position = oldPosition = Vector3.Zero;
  147. up = Vector3.Up;
  148. right = Vector3.Right;
  149. direction = Vector3.Forward;
  150. target = Vector3.Forward;
  151. velocity = Vector3.Zero;
  152. projectionMatrix = Matrix.Identity;
  153. viewMatrix = Matrix.Identity;
  154. }
  155. /// <summary>
  156. /// Update a velocity
  157. /// </summary>
  158. protected override void OnUpdate(GameTime gameTime)
  159. {
  160. if (oldPosition == this.Position)
  161. {
  162. this.velocity = Vector3.Zero;
  163. }
  164. else
  165. {
  166. this.velocity = this.position - oldPosition;
  167. oldPosition = this.position;
  168. }
  169. base.OnUpdate(gameTime);
  170. }
  171. /// <summary>
  172. /// Set the view matrix of the camera
  173. /// </summary>
  174. public Matrix SetView(Vector3 position, Vector3 target, Vector3 up)
  175. {
  176. // Make a camera's direction
  177. this.direction = target - position;
  178. this.direction.Normalize();
  179. this.position = position;
  180. this.target = target;
  181. this.up = up;
  182. // Make a camera's right vector
  183. this.right = Vector3.Cross(direction, up);
  184. this.right.Normalize();
  185. // Make a camera's view matrix
  186. ViewMatrix = Matrix.CreateLookAt(this.Position, this.target, this.Up);
  187. return ViewMatrix;
  188. }
  189. /// <summary>
  190. /// Set the projection matrix of the camera
  191. /// </summary>
  192. public Matrix SetPespective( float fov, float width, float height,
  193. float near, float far)
  194. {
  195. this.screenWidth = width;
  196. this.screenHeight = height;
  197. this.fov = fov;
  198. this.near = near;
  199. this.far = far;
  200. aspectRatio = screenWidth / screenHeight;
  201. // Make a camera's projection matrix
  202. projectionMatrix =
  203. Matrix.CreatePerspectiveFieldOfView(fov, aspectRatio, near, far);
  204. return projectionMatrix;
  205. }
  206. /// <summary>
  207. /// It changes the width and height of screen.
  208. /// </summary>
  209. public void Resize(float width, float height)
  210. {
  211. SetPespective(this.fov, width, height, this.Near, this.Far);
  212. }
  213. /// <summary>
  214. /// Reset the camera
  215. /// </summary>
  216. public void Reset()
  217. {
  218. this.position = Vector3.Zero;
  219. this.up = Vector3.Up;
  220. this.right = Vector3.Right;
  221. this.direction = Vector3.Forward;
  222. this.target = position + direction;
  223. this.ViewMatrix = Matrix.Identity;
  224. }
  225. }
  226. }