-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.cs
294 lines (251 loc) · 6.99 KB
/
Camera.cs
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
namespace SXE.Engine
{
public interface ICamera
{
Vector3 Position { get; set;}
Vector3 Forward { get; set; }
Vector3 Up { get; set; }
Vector3 Right { get; }
Vector3 ViewOffset { get; set; }
Matrix View { get; }
Matrix Projection { get; }
BoundingFrustum Frustrum { get; }
BoundingFrustum ShortFrustrum { get; }
float FOV { get; set; }
float AspectRatio { get; set; }
float NearPlane { get; set; }
float FarPlane { get; set; }
float ShortPlane { get; set; }
void RotateHorizontally(float amt);
void RotateVertically(float amt);
void Update(GameTime time);
}
/// <summary>
/// Encapsulates camera functionality
/// Takes in various parameters, like position, forward vector, up,and view offset
/// Outputs a view matrix, projection matrix, and some frustrums
/// Make sure that position, forward, up, viewoffset, and aspectRatio are set before
/// calling SetupMatrices()
/// </summary>
public class Camera : ICamera
{
Vector3 position;
Vector3 forward;
Vector3 up;
Vector3 viewOffset;
Matrix view;
Matrix projection;
Matrix shortProjection;
BoundingFrustum frustrum;
BoundingFrustum shortFrustrum;
float fov = (float)MathHelper.PiOver4;
float aspectRatio = 1.0f;
float nearPlane = 1.0f;
float shortPlane = 250.0f;
float farPlane = 5000.0f;
bool hasChanged = true; //flag to mark if camera data has changed
public Camera(Vector3 position, Vector3 forward)
{
this.position = position;
this.forward = forward;
}
public Camera()
: this(Vector3.Zero, Vector3.Forward)
{
this.up = Vector3.Up;
}
#region Public Get/Set Functions
public Vector3 Position
{
get
{
return position;
}
set
{
hasChanged = true;
position = value;
}
}
public Vector3 Forward
{
get
{
return forward;
}
set
{
hasChanged = true;
forward = value;
forward.Normalize();
}
}
public Vector3 Up
{
get
{
return up;
}
set
{
hasChanged = true;
up = value;
}
}
public Vector3 Right
{
get
{
return Vector3.Normalize(Vector3.Cross(Forward, Up));
}
}
public Vector3 ViewOffset
{
get
{
return viewOffset;
}
set
{
hasChanged = true;
viewOffset = value;
}
}
public float FOV
{
get
{
return fov;
}
set
{
hasChanged = true;
fov = value;
}
}
public float AspectRatio
{
get
{
return aspectRatio;
}
set
{
hasChanged = true;
aspectRatio = value;
}
}
public float NearPlane
{
get
{
return nearPlane;
}
set
{
hasChanged = true;
nearPlane = value;
}
}
public float FarPlane
{
get
{
return farPlane;
}
set
{
hasChanged = true;
farPlane = value;
}
}
public float ShortPlane
{
get
{
return shortPlane;
}
set
{
hasChanged = true;
shortPlane = value;
}
}
#endregion
public void RotateHorizontally(float amount)
{
//First, rotate the forward vector
Forward = Vector3.Transform(forward, Matrix.CreateFromAxisAngle(Vector3.Up, amount));
Forward.Normalize();
//TODO: Fix to make more robust
//This is not very robust, because if you are looking exactly straight up, it will create problems..
}
public void RotateVertically(float amount)
{
Vector3 cross = Vector3.Cross(forward, Vector3.Up);
cross.Normalize();
Matrix rotMatrix = Matrix.CreateFromAxisAngle(cross, amount);
//Rotate the forward vector
Forward = Vector3.Transform(forward, rotMatrix);
Forward.Normalize();
//TODO:
//Fix this function, make it way better
}
public void Update(GameTime gameTime)
{
SetupMatrices();
}
/// <summary>
/// Private function that creates the camera matrices based on our data
/// </summary>
void SetupMatrices()
{
view = Matrix.CreateLookAt(this.position + viewOffset, this.position + viewOffset + forward, this.up);
projection = Matrix.CreatePerspectiveFieldOfView(fov, aspectRatio, nearPlane, farPlane);
shortProjection = Matrix.CreatePerspectiveFieldOfView(fov, aspectRatio, nearPlane, shortPlane);
if (hasChanged)
{
frustrum = new BoundingFrustum(view * projection);
shortFrustrum = new BoundingFrustum(view * shortProjection);
hasChanged = false;
}
}
public Matrix View
{
get
{
return view;
}
}
public Matrix Projection
{
get
{
return projection;
}
}
public BoundingFrustum Frustrum
{
get
{
return frustrum;
}
}
public BoundingFrustum ShortFrustrum
{
get
{
return shortFrustrum;
}
}
}
}