-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.cs
363 lines (329 loc) · 8.89 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// ***********************************************************************
// Assembly : SharpDXWrapper
// Author : Andrew
// Created : 07-21-2017
//
// Last Modified By : Andrew
// Last Modified On : 07-21-2017
// ***********************************************************************
using SharpDX;
namespace SharpDXWrapper
{
/// <summary>
/// Defines a method that allows you to obtain a projection matrix.
/// </summary>
public interface IProjectionCamera
{
/// <summary>
/// Gets the projection matrix.
/// </summary>
/// <value>The projection.</value>
Matrix Projection { get; }
}
/// <summary>
/// Create a camera for obtaining view matrix.
/// </summary>
public abstract class Camera
{
/// <summary>
/// Gets or sets the position.
/// </summary>
/// <value>The position.</value>
public Vector3 Position { get; set; }
/// <summary>
/// Gets or sets the target.
/// </summary>
/// <value>The target.</value>
public Vector3 Target { get; set; }
/// <summary>
/// Gets the view matrix.
/// </summary>
/// <value>The view.</value>
public Matrix View
{
get
{
return Matrix.LookAtLH(Position, Target, Vector3.Up);
}
}
/// <summary>
/// Gets or sets the rotation.
/// </summary>
/// <value>The rotation.</value>
public abstract Vector3 Rotation { get; set; }
/// <summary>
/// Gets or sets the length.
/// </summary>
/// <value>The length.</value>
public abstract float Length { get; set; }
/// <summary>
/// Rotates the specified angle.
/// </summary>
/// <param name="yawAngle">The yaw angle.</param>
/// <param name="pitchAngle">The pitch angle.</param>
/// <param name="rollAngle">The roll angle.</param>
public abstract void Rotate(float yawAngle, float pitchAngle, float rollAngle);
/// <summary>
/// Moves the specified direction.
/// </summary>
/// <param name="direction">The direction.</param>
public void Move(Vector3 direction)
{
Position += direction;
Target += direction;
}
}
/// <summary>
/// Class FirstPersonCamera. Create a first person camera for obtaining view matrix.
/// </summary>
/// <seealso cref="SharpDXWrapper.Camera" />
public class FirstPersonCamera : Camera
{
/// <summary>
/// Initializes a new instance of the <see cref="FirstPersonCamera"/> class.
/// </summary>
/// <param name="position">The position.</param>
/// <param name="target">The target.</param>
public FirstPersonCamera(Vector3 position = default(Vector3), Vector3 target = default(Vector3))
{
Position = position;
Target = target;
Length = (Position - Target).Length();
Rotation = MathHelper.YawPitchRollFromVector3(Target - Position);
}
/// <summary>
/// The length
/// </summary>
private float length;
/// <summary>
/// The rotation
/// </summary>
private Vector3 rotation;
/// <summary>
/// Gets or sets the length.
/// </summary>
/// <value>The length.</value>
public override float Length
{
get
{
return length;
}
set
{
length = MathUtil.Clamp(value, 0, float.MaxValue);
Vector3 vector = Vector3.Normalize(Target - Position);
Target = Position + vector * length;
}
}
/// <summary>
/// Gets or sets the rotation.
/// </summary>
/// <value>The rotation.</value>
public override Vector3 Rotation
{
get
{
return rotation;
}
set
{
Vector3 vectorRotate = MathHelper.Vector3FromYawPitch(value.X, value.Y);
vectorRotate.Normalize();
float length = (Position - Target).Length();
Target = Position + vectorRotate * length;
rotation = value;
}
}
/// <summary>
/// Rotates the specified yaw angle.
/// </summary>
/// <param name="yawAngle">The yaw angle.</param>
/// <param name="pitchAngle">The pitch angle.</param>
/// <param name="rollAngle">The roll angle.</param>
public override void Rotate(float yawAngle, float pitchAngle, float rollAngle)
{
Rotation += new Vector3(yawAngle, pitchAngle, rollAngle);
}
}
/// <summary>
/// Class ThirdPersonCamera. Create a third person camera for obtaining view matrix.
/// </summary>
/// <seealso cref="SharpDXWrapper.Camera" />
public class ThirdPersonCamera : Camera
{
/// <summary>
/// The length
/// </summary>
private float length;
/// <summary>
/// The rotation
/// </summary>
private Vector3 rotation;
/// <summary>
/// Initializes a new instance of the <see cref="ThirdPersonCamera"/> class.
/// </summary>
/// <param name="position">The position.</param>
/// <param name="target">The target.</param>
public ThirdPersonCamera(Vector3 position = default(Vector3), Vector3 target = default(Vector3))
{
Position = position;
Target = target;
length = (Position - Target).Length();
rotation = MathHelper.YawPitchRollFromVector3(position - target);
}
/// <summary>
/// Gets or sets the length.
/// </summary>
/// <value>The length.</value>
public override float Length
{
get
{
return length;
}
set
{
length = MathUtil.Clamp(value, 0, float.MaxValue);
Vector3 vector = MathHelper.Vector3FromYawPitch(Rotation.X, Rotation.Y);
vector.Normalize();
Position = Target + vector * length;
}
}
/// <summary>
/// Gets or sets the rotation.
/// </summary>
/// <value>The rotation.</value>
public override Vector3 Rotation
{
get
{
return rotation;
}
set
{
value.Y = MathUtil.Clamp(value.Y, -MathUtil.PiOverTwo + MathUtil.DegreesToRadians(1),
MathUtil.PiOverTwo - MathUtil.DegreesToRadians(1));
Vector3 vectorRotate = MathHelper.Vector3FromYawPitch(value.X, value.Y);
vectorRotate.Normalize();
float length = (Position - Target).Length();
Position = Target + vectorRotate * length;
rotation = value;
}
}
/// <summary>
/// Rotates the specified yaw angle.
/// </summary>
/// <param name="yawAngle">The yaw angle.</param>
/// <param name="pitchAngle">The pitch angle.</param>
/// <param name="rollAngle">The roll angle.</param>
public override void Rotate(float yawAngle, float pitchAngle, float rollAngle)
{
Rotation += new Vector3(yawAngle, pitchAngle, rollAngle);
}
}
/// <summary>
/// Create a perspective camera for obtaining projection matrix.
/// </summary>
/// <seealso cref="SharpDXWrapper.IProjectionCamera" />
public class PerspectiveCamera : IProjectionCamera
{
/// <summary>
/// Gets or sets the fov.
/// </summary>
/// <value>The fov.</value>
public float FOV { get; set; } //Degrees
/// <summary>
/// Gets or sets the near z.
/// </summary>
/// <value>The near z.</value>
public float NearZ { get; set; }
/// <summary>
/// Gets or sets the far z.
/// </summary>
/// <value>The far z.</value>
public float FarZ { get; set; }
/// <summary>
/// Gets or sets the aspect ratio.
/// </summary>
/// <value>The aspect ratio.</value>
public float AspectRatio { get; set; }
/// <summary>
/// Gets the projection.
/// </summary>
/// <value>The projection.</value>
public Matrix Projection
{
get
{
return Matrix.PerspectiveFovLH(MathUtil.DegreesToRadians(FOV), AspectRatio, NearZ, FarZ);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="PerspectiveCamera"/> class.
/// </summary>
/// <param name="fov">The fov.</param>
/// <param name="nearZ">The near z.</param>
/// <param name="farZ">The far z.</param>
/// <param name="aspectRation">The aspect ration.</param>
public PerspectiveCamera(float fov, float nearZ, float farZ, float aspectRation)
{
FOV = fov;
NearZ = nearZ;
FarZ = farZ;
AspectRatio = aspectRation;
}
}
/// <summary>
/// Create a orthogonal camera for obtaining projection matrix.
/// </summary>
/// <seealso cref="SharpDXWrapper.IProjectionCamera" />
public class OrthogonalCamera : IProjectionCamera
{
/// <summary>
/// Gets or sets the width.
/// </summary>
/// <value>The width.</value>
public float Width { get; set; }
/// <summary>
/// Gets or sets the height.
/// </summary>
/// <value>The height.</value>
public float Height { get; set; }
/// <summary>
/// Gets or sets the near z.
/// </summary>
/// <value>The near z.</value>
public float NearZ { get; set; }
/// <summary>
/// Gets or sets the far z.
/// </summary>
/// <value>The far z.</value>
public float FarZ { get; set; }
/// <summary>
/// Gets the projection.
/// </summary>
/// <value>The projection.</value>
public Matrix Projection
{
get
{
return Matrix.OrthoLH(Width, Height, NearZ, FarZ);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="OrthogonalCamera"/> class.
/// </summary>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="nearZ">The near z.</param>
/// <param name="farZ">The far z.</param>
public OrthogonalCamera(float width, float height, float nearZ, float farZ)
{
Width = width;
Height = height;
NearZ = nearZ;
FarZ = farZ;
}
}
}