-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cs
More file actions
83 lines (68 loc) · 2.96 KB
/
Camera.cs
File metadata and controls
83 lines (68 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using Microsoft.Xna.Framework;
namespace LevelEditor.Engine
{
/// <summary>
/// Used to represent a camera in the engine.
/// </summary>
internal sealed class Camera
{
public Vector3 mLocation;
public Vector2 mRotation;
public float mFieldOfView;
public float mAspectRatio;
public float mNearPlane;
public float mFarPlane;
public Matrix mViewMatrix;
public Matrix mProjectionMatrix;
public Vector3 Direction { get; private set; }
public Vector3 Up { get; private set; }
public Vector3 Right { get; private set; }
/// <summary>
/// Constructs a <see cref="Camera"/>.
/// </summary>
/// <param name="location">The location of the camera.</param>
/// <param name="rotation">The rotation of the camera, where .X is the horizontal and .Y the vertical rotation.</param>
/// <param name="fieldOfView">The field of view in degrees.</param>
/// <param name="aspectRatio">The ratio of the image width to the image height.</param>
/// <param name="nearPlane">The plane where the camera starts to render.</param>
/// <param name="farPlane">The plane where the camera stops to render.</param>
public Camera(Vector3 location = default(Vector3),
Vector2 rotation = default(Vector2),
float fieldOfView = 45.0f,
float aspectRatio = 2.0f,
float nearPlane = 1.0f,
float farPlane = 100.0f)
{
mViewMatrix = new Matrix();
mProjectionMatrix = new Matrix();
mLocation = location;
mRotation = rotation;
mFieldOfView = fieldOfView;
mAspectRatio = aspectRatio;
mNearPlane = nearPlane;
mFarPlane = farPlane;
}
/// <summary>
/// Calculates the view matrix based on the location and rotation of the camera.
/// </summary>
public void UpdateView()
{
// TODO: We need to change this to a 3rd person camera
Direction = -Vector3.Normalize(new Vector3((float) (Math.Cos(mRotation.Y) * Math.Sin(mRotation.X)),
(float) Math.Sin(mRotation.Y),
(float) (Math.Cos(mRotation.Y) * Math.Cos(mRotation.X))));
Right = -Vector3.Normalize(new Vector3((float) Math.Sin(mRotation.X - 3.14 / 2.0), 0.0f, (float) Math.Cos(mRotation.X - 3.14 / 2.0)));
Up = Vector3.Cross(Right, Direction);
mViewMatrix = Matrix.CreateLookAt(mLocation, mLocation + Direction, Up);
}
/// <summary>
/// Calculates the perspective matrix based on the FoV, the aspect ratio and the near and far plane.
/// </summary>
public void UpdatePerspective()
{
mProjectionMatrix =
Matrix.CreatePerspectiveFieldOfView(mFieldOfView / 180.0f * (float)Math.PI, mAspectRatio, mNearPlane, mFarPlane);
}
}
}