Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Camera.cs
Go to the documentation of this file.
1 // Copyright (c) 2011 ReShader - Alexandre Mutel
2 
3 using System;
4 
5 using SiliconStudio.Paradox.Games.Mathematics;
6 
7 namespace ScriptTest2
8 {
9  /// <summary>
10  /// Camera mode.
11  /// </summary>
12  public enum CameraMode
13  {
14  Free,
15  Target
16  }
17 
18  /// <summary>
19  /// Camera component.
20  /// </summary>
21  public class Camera
22  {
23  private float orthographicSize;
24  private bool isOrthographic;
25  private float nearClipPlane;
26  private float farClipPlane;
27  private float aspect;
28  private float fieldOfView;
29  private CameraMode mode;
30  private Matrix projection;
31  private Vector3 position;
32  private Matrix worldToCamera;
33  private Vector3 target;
34 
35  /// <summary>
36  /// Initializes a new instance of the <see cref="Camera"/> class.
37  /// </summary>
38  public Camera()
39  {
40  }
41 
42  /// <summary>
43  /// Initializes a new instance of the <see cref="Camera"/> class.
44  /// </summary>
45  /// <param name="position">The position.</param>
46  /// <param name="target">The target.</param>
47  /// <param name="fov">The fov.</param>
48  /// <param name="aspect">The aspect.</param>
49  /// <param name="nearClipPlane">The near clip plane.</param>
50  /// <param name="farClipPlane">The far clip plane.</param>
51  public Camera(Vector3 position, Vector3 target, float fov, float width, float height, float aspect, float nearClipPlane, float farClipPlane)
52  {
53  this.orthographicSize = 1; // default value for orthographic
54  this.mode = CameraMode.Target;
55  this.position = position;
56  this.target = target;
57  this.fieldOfView = fov;
58  this.Width = width;
59  this.Height = height;
60  this.aspect = aspect;
61  this.nearClipPlane = nearClipPlane;
62  this.farClipPlane = farClipPlane;
63  UpdateWorldToCamera();
64  UpdateProjection();
65  }
66 
67  /// <summary>
68  /// Initializes a new instance of the <see cref="Camera"/> class.
69  /// </summary>
70  /// <param name="position">The position.</param>
71  /// <param name="yaw">The yaw.</param>
72  /// <param name="pitch">The pitch.</param>
73  /// <param name="roll">The roll.</param>
74  /// <param name="fov">The fov.</param>
75  /// <param name="aspect">The aspect.</param>
76  /// <param name="nearClipPlane">The near clip plane.</param>
77  /// <param name="farClipPlane">The far clip plane.</param>
78  public Camera(Vector3 position, float yaw, float pitch, float roll, float fov, float width, float height, float aspect, float nearClipPlane, float farClipPlane)
79  {
80  Matrix.RotationYawPitchRoll(yaw, pitch, roll, out worldToCamera);
81  this.position = position;
82  this.orthographicSize = 1; // default value for orthographic
83  this.mode = CameraMode.Free;
84  this.fieldOfView = fov;
85  this.Width = width;
86  this.Height = height;
87  this.aspect = aspect;
88  this.nearClipPlane = nearClipPlane;
89  this.farClipPlane = farClipPlane;
90  UpdateWorldToCamera();
91  UpdateProjection();
92  }
93 
94  /// <summary>
95  /// Gets the width of the camera.
96  /// </summary>
97  public float Width { get; private set; }
98 
99  /// <summary>
100  /// Gets the height of the camera.
101  /// </summary>
102  public float Height { get; private set; }
103 
104  /// <summary>
105  /// Gets or sets the near clip plane.
106  /// </summary>
107  /// <value>
108  /// The near clip plane.
109  /// </value>
110  public float NearClipPlane
111  {
112  get
113  {
114  return nearClipPlane;
115  }
116  set
117  {
118  nearClipPlane = value;
119  UpdateProjection();
120  }
121  }
122 
123  /// <summary>
124  /// Gets or sets the far clip plane.
125  /// </summary>
126  /// <value>
127  /// The far clip plane.
128  /// </value>
129  public float FarClipPlane
130  {
131  get
132  {
133  return farClipPlane;
134  }
135  set
136  {
137  farClipPlane = value;
138  UpdateProjection();
139  }
140  }
141 
142  /// <summary>
143  /// Gets or sets the vertical field of view.
144  /// </summary>
145  /// <value>
146  /// The field of view.
147  /// </value>
148  /// <remarks>
149  /// The horizontal field of view is determined
150  /// </remarks>
151  public float FieldOfView
152  {
153  get
154  {
155  return fieldOfView;
156  }
157  set
158  {
159  fieldOfView = value;
160  UpdateProjection();
161  }
162  }
163 
164  /// <summary>
165  /// Gets or sets the aspect.
166  /// </summary>
167  /// <value>
168  /// The aspect.
169  /// </value>
170  public float Aspect
171  {
172  get
173  {
174  return aspect;
175  }
176  set
177  {
178  aspect = value;
179  UpdateProjection();
180  }
181  }
182 
183  /// <summary>
184  /// Gets or sets the camera half vertical size in orthographic mode.
185  /// </summary>
186  /// <value>
187  /// The camera half vertical size .
188  /// </value>
189  /// <remarks>
190  /// This value is valid only when <see cref="IsOrthographic"/> is set to true.
191  /// </remarks>
192  public float OrthographicSize
193  {
194  get
195  {
196  return orthographicSize;
197  }
198  set
199  {
200  orthographicSize = value;
201  UpdateProjection();
202  }
203  }
204 
205  /// <summary>
206  /// Gets or sets a value indicating whether this instance is orthographic.
207  /// </summary>
208  /// <value>
209  /// <c>true</c> if this instance is orthographic; otherwise, <c>false</c>.
210  /// </value>
211  public bool IsOrthographic
212  {
213  get
214  {
215  return isOrthographic;
216  }
217  set
218  {
219  isOrthographic = value;
220  UpdateProjection();
221  }
222  }
223 
224  /// <summary>
225  /// Gets or sets the mode of this camera.
226  /// </summary>
227  /// <value>
228  /// The camera mode.
229  /// </value>
230  public CameraMode Mode
231  {
232  get
233  {
234  return mode;
235  }
236  set
237  {
238  mode = value;
239  }
240  }
241 
242  /// <summary>
243  /// Gets or sets the world to camera matrix.
244  /// </summary>
245  /// <value>
246  /// The world to camera matrix.
247  /// </value>
248  public Matrix WorldToCamera
249  {
250  get
251  {
252  return worldToCamera;
253  }
254  set
255  {
256  // If WorldToCamera matrix is setup manually, then force to free camera mode
257  mode = CameraMode.Free;
258 
259  worldToCamera = value;
260  }
261  }
262 
263  /// <summary>
264  /// Gets or sets the projection matrix.
265  /// </summary>
266  /// <value>
267  /// The projection matrix.
268  /// </value>
269  public Matrix Projection
270  {
271  get
272  {
273  return projection;
274  }
275  set
276  {
277  projection = value;
278  }
279  }
280 
281  /// <summary>
282  /// Gets or sets the position.
283  /// </summary>
284  /// <value>
285  /// The position.
286  /// </value>
287  public Vector3 Position
288  {
289  get
290  {
291  return position;
292  }
293 
294  set
295  {
296  position = value;
297  UpdateWorldToCamera();
298  }
299  }
300 
301  /// <summary>
302  /// Gets or sets the target.
303  /// </summary>
304  /// <value>
305  /// The target.
306  /// </value>
307  /// <remarks>>Only available for camera mode <see cref="CameraMode.Target"/></remarks>
308  public Vector3 Target
309  {
310  get
311  {
312  return target;
313  }
314  set
315  {
316  target = value;
317  UpdateWorldToCamera();
318  }
319  }
320 
321  public override string ToString()
322  {
323  return string.Format("Mode: {0}, NearClipPlane: {1}, FarClipPlane: {2}, FieldOfView: {3}, Aspect: {4}", mode, nearClipPlane, farClipPlane, fieldOfView, aspect);
324  }
325 
326  public static Matrix YawPitchRoll(Vector3 position, Matrix matrix, float yaw, float pitch, float roll)
327  {
328  var tempMatrix = Matrix.Identity;
329  var rotateZ = Matrix.RotationZ(yaw) * Matrix.RotationX(roll);
330  tempMatrix.Column1 = Vector3.Transform((Vector3)matrix.Column1, rotateZ);
331  tempMatrix.Column3 = Vector3.Transform((Vector3)matrix.Column3, rotateZ);
332  tempMatrix.Column2 = (Vector4)Vector3.Cross((Vector3)tempMatrix.Column3, (Vector3)tempMatrix.Column1);
333  tempMatrix.M41 = 0;
334  tempMatrix.M42 = 0;
335  tempMatrix.M43 = 0;
336  return Matrix.Translation(-position) * tempMatrix * Matrix.RotationX(pitch);
337  }
338 
339  private void UpdateWorldToCamera()
340  {
341  if (mode == CameraMode.Target)
342  {
343  worldToCamera = Matrix.LookAtLH(Position, Target, Vector3.UnitZ);
344  }
345  else
346  {
347  var defaultCamera = worldToCamera;
348  defaultCamera.M41 = 0;
349  defaultCamera.M42 = 0;
350  defaultCamera.M43 = 0;
351  worldToCamera = Matrix.Translation(-Position) * defaultCamera;
352  }
353  }
354 
355  private void UpdateProjection()
356  {
357  if (IsOrthographic)
358  {
359  Matrix.OrthoLH(OrthographicSize * 2 * Aspect, OrthographicSize * 2, NearClipPlane, FarClipPlane, out projection);
360  }
361  else
362  {
363  Matrix.PerspectiveFovLH(FieldOfView, Aspect, NearClipPlane, FarClipPlane, out projection);
364  }
365  }
366  }
367 }
float Aspect
Gets or sets the aspect.
Definition: Camera.cs:171
Camera component.
Definition: Camera.cs:21
float Height
Gets the height of the camera.
Definition: Camera.cs:102
Matrix WorldToCamera
Gets or sets the world to camera matrix.
Definition: Camera.cs:249
float Width
Gets the width of the camera.
Definition: Camera.cs:97
Vector3 Target
Gets or sets the target.
Definition: Camera.cs:309
float OrthographicSize
Gets or sets the camera half vertical size in orthographic mode.
Definition: Camera.cs:193
bool IsOrthographic
Gets or sets a value indicating whether this instance is orthographic.
Definition: Camera.cs:212
Camera(Vector3 position, float yaw, float pitch, float roll, float fov, float width, float height, float aspect, float nearClipPlane, float farClipPlane)
Initializes a new instance of the Camera class.
Definition: Camera.cs:78
static Matrix YawPitchRoll(Vector3 position, Matrix matrix, float yaw, float pitch, float roll)
Definition: Camera.cs:326
Matrix Projection
Gets or sets the projection matrix.
Definition: Camera.cs:270
Camera(Vector3 position, Vector3 target, float fov, float width, float height, float aspect, float nearClipPlane, float farClipPlane)
Initializes a new instance of the Camera class.
Definition: Camera.cs:51
float FieldOfView
Gets or sets the vertical field of view.
Definition: Camera.cs:152
override string ToString()
Definition: Camera.cs:321
CameraMode Mode
Gets or sets the mode of this camera.
Definition: Camera.cs:231
Vector3 Position
Gets or sets the position.
Definition: Camera.cs:288
float NearClipPlane
Gets or sets the near clip plane.
Definition: Camera.cs:111
SiliconStudio.Core.Mathematics.Vector3 Vector3
CameraMode
Camera mode.
Definition: Camera.cs:12
Camera()
Initializes a new instance of the Camera class.
Definition: Camera.cs:38
float FarClipPlane
Gets or sets the far clip plane.
Definition: Camera.cs:130