-
-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathCamera.cs
381 lines (351 loc) · 14.4 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
using SharpDX;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodeWalker.World
{
public class Camera
{
public Vector3 TargetRotation = Vector3.Zero;
public Vector3 CurrentRotation = Vector3.Zero;
public float Smoothness;// 10.0f;//0.15f;
public float Sensitivity;// 0.005f;
public float TargetDistance = 1.0f;
public float CurrentDistance = 1.0f;
public float ZoomCurrentTime = 0.0f;
public float ZoomTargetTime = 2.0f;
public float ZoomVelocity = 0.0f;
public float ZoomSpeed = 0.1f;
public float Width = 1920.0f;
public float Height = 1080.0f;
public float FieldOfView;// 1.0f;
public float FieldOfViewFactor = 0.5f / (float)Math.Tan(/*FieldOfView*/ 1.0f * 0.5f);
public float AspectRatio = 1920.0f / 1080.0f;
public float ZNear = 0.01f;
public float ZFar = 100000.0f;
public Entity FollowEntity = null;
public Vector3 LocalLookAt = Vector3.ForwardLH;
public float VOffset = 0.0f;
public bool UpdateProj = true;
public bool IsMapView = false;
public bool IsOrthographic = false;
public float OrthographicSize = 20.0f;
public float OrthographicTargetSize = 20.0f;
public Matrix ProjMatrix = Matrix.Identity;
public Vector3 Position = Vector3.Zero;
public Vector3 UpDirection = Vector3.Up;
public Vector3 ViewDirection = Vector3.ForwardLH;
public Quaternion ViewQuaternion = Quaternion.Identity;
public Quaternion ViewInvQuaternion = Quaternion.Identity;
public Matrix ViewMatrix = Matrix.Identity;
public Matrix ViewInvMatrix = Matrix.Identity;
public Matrix ViewProjMatrix = Matrix.Identity;
public Matrix ViewProjInvMatrix = Matrix.Identity;
public Frustum ViewFrustum = new Frustum();
public Vector3 MouseRayNear = Vector3.Zero;
public Vector3 MouseRayFar = Vector3.Zero;
public Ray MouseRay;
private float MouseX = 0;
private float MouseY = 0;
private object syncRoot = new object();
public Camera(float smoothness, float sensitivity, float fov)
{
Smoothness = smoothness;
Sensitivity = sensitivity;
FieldOfView = fov;
FieldOfViewFactor = 0.5f / (float)Math.Tan(FieldOfView * 0.5f);
}
public void SetMousePosition(int x, int y)
{
MouseX = (x / Width) * 2.0f - 1.0f;
MouseY = (y / Height) * -2.0f + 1.0f;
}
public void SetFollowEntity(Entity e)
{
FollowEntity = e;
}
public void Update(float elapsed)
{
lock (syncRoot)
{
UpdateFollow(elapsed);
if (UpdateProj) UpdateProjMatrix();
//float mx = (LastMouseX / Width) * 2.0f;
//float my = (LastMouseY / Height) * -2.0f;
////MousedItem = nullptr;
////MousedThing = nullptr;
////MousedItemSpace = nullptr;
UpdateProjection();//, mx, my);
}
}
private void UpdateFollow(float elapsed)
{
const float ythresh = 1.55f;
const float nythresh = -1.55f;
Vector3 up = Vector3.Up;// new Vector3(0.0f, 1.0f, 0.0f);
if (TargetRotation.Y > ythresh) TargetRotation.Y = ythresh;
if (TargetRotation.Y < nythresh) TargetRotation.Y = nythresh;
float sv = Math.Min(Smoothness * elapsed, 1.0f);
CurrentRotation = CurrentRotation + ((TargetRotation - CurrentRotation) * sv);
if (TargetDistance > 11000.0f) TargetDistance = 11000.0f; //11km max zoom dist
if (TargetDistance < 0.0001f) TargetDistance = 0.0001f; //0.1mm min zoom dist...
ZoomCurrentTime += elapsed;
if (ZoomCurrentTime > ZoomTargetTime) ZoomCurrentTime = ZoomTargetTime;
float currentTime = ZoomCurrentTime / ZoomTargetTime;
float deltaDist = TargetDistance - CurrentDistance;
if (currentTime < 1.0f && deltaDist > 0.0f)
{
//TODO: when to properly reset ZoomCurrentTime?
float y = currentTime*currentTime*currentTime; //powf(currentTime, 3.0f);
deltaDist *= y;
}
CurrentDistance = CurrentDistance + deltaDist * ZoomSpeed;
if (IsOrthographic || IsMapView)
{
if (OrthographicTargetSize > 20000.0f) OrthographicTargetSize = 20000.0f;
if (OrthographicTargetSize < 1.0f) OrthographicTargetSize = 1.0f;
OrthographicSize = OrthographicSize + ((OrthographicTargetSize - OrthographicSize) * sv);
UpdateProj = true;
}
if (IsMapView)
{
//in map view, need a constant view matrix aligned to XY.
Vector3 cpos = new Vector3();
if (FollowEntity != null)
{
cpos = FollowEntity.Position;
}
LocalLookAt = Vector3.Zero;
Position = cpos;
Position.Z = 1000.0f;
ViewDirection = -Vector3.UnitZ;
UpDirection = Vector3.UnitY;
}
else
{
//normal view mode
Vector3 rdir = new Vector3();
float cryd = (float)Math.Cos(CurrentRotation.Y);
rdir.X = -(float)Math.Sin(-CurrentRotation.X) * cryd;
rdir.Z = -(float)Math.Cos(-CurrentRotation.X) * cryd;
rdir.Y = (float)Math.Sin(CurrentRotation.Y);
Vector3 lookat = new Vector3(0.0f, VOffset, 0.0f);
Vector3 cpos = new Vector3();
if (FollowEntity != null)
{
up = FollowEntity.Orientation.Multiply(up);
lookat = FollowEntity.Orientation.Multiply(lookat);
rdir = FollowEntity.Orientation.Multiply(rdir);
cpos = FollowEntity.Position;
}
LocalLookAt = (rdir * CurrentDistance) + lookat;
Position = cpos + LocalLookAt;
ViewDirection = Vector3.Normalize(-rdir);
UpDirection = up;
}
//M16FLookAt(LocalProjection.ViewMatrix, V3F(0.0f, 0.0f, 0.0f), LocalProjection.ViewDirection, LocalProjection.UpDirection);
ViewQuaternion = Quaternion.LookAtRH(Vector3.Zero, ViewDirection, UpDirection);
ViewInvQuaternion = Quaternion.Invert(ViewQuaternion);
ViewMatrix = ViewQuaternion.ToMatrix();
ViewInvMatrix = Matrix.Invert(ViewMatrix);
}
private void UpdateProjMatrix()
{
if (IsMapView)
{
ProjMatrix = Matrix.OrthoRH(AspectRatio * OrthographicSize, OrthographicSize, 3000.0f, 1.0f);
}
else if (IsOrthographic)
{
ProjMatrix = Matrix.OrthoRH(AspectRatio * OrthographicSize, OrthographicSize, ZFar, ZNear);
}
else
{
ProjMatrix = Matrix.PerspectiveFovRH(FieldOfView, AspectRatio, ZFar, ZNear);
}
//ProjMatrix._33/=ZFar;
//ProjMatrix._43/=ZFar;
UpdateProj = false;
}
private void UpdateProjection() //CameraSpaceProjection& p, float mx, float my)
{
float mx = MouseX;
float my = MouseY;
ViewProjMatrix = Matrix.Multiply(ViewMatrix, ProjMatrix);
ViewProjInvMatrix = Matrix.Invert(ViewProjMatrix);
MouseRayNear = ViewProjInvMatrix.MultiplyW(new Vector3(mx, my, 1.0f));
MouseRayFar = ViewProjInvMatrix.MultiplyW(new Vector3(mx, my, 0.0f));
MouseRay.Position = Vector3.Zero;
MouseRay.Direction = Vector3.Normalize(MouseRayFar - MouseRayNear);
if (IsMapView || IsOrthographic)
{
MouseRay.Position = MouseRayNear;
}
ViewFrustum.Update(ref ViewProjMatrix);
ViewFrustum.Position = Position;
}
private void UpdateMousedItem()
{
//////MousedItem = nullptr;
//////MousedThing = nullptr;
//////MousedItemSpace = nullptr;
////int i = 0;
//////var cp = &SpaceProjections[i++];
//////auto item = cp->MousedItem;
//////auto thing = cp->MousedThing;
////auto count = cp->MouseTestedItems;
//////while((item==nullptr) && (thing==nullptr) && (i<SpaceProjections.size()))
////Moused.Clear();
////if (cp->Moused.HasValue) Moused.Set(cp->Moused);
////while (!Moused.HasValue && (i < SpaceProjections.size()))
////{
//// cp = &SpaceProjections[i++];
//// //item = cp->MousedItem;
//// //thing = cp->MousedThing;
//// if (cp->Moused.HasValue)
//// {
//// Moused.Set(cp->Moused);
//// }
//// count += cp->MouseTestedItems;
////}
//////if((item!=nullptr) || (thing!=nullptr))
//////{
////// MousedItem = item;
////// MousedThing = thing;
////// MousedItemSpace = cp->MousedItemSpace;
//////}
}
public void OnWindowResize(int w, int h)
{
lock (syncRoot)
{
Width = (float)w;
Height = (float)h;
AspectRatio = Width / Height;
UpdateProj = true;
}
}
public void ControllerRotate(float x, float y, float elapsed)
{
lock (syncRoot)
{
TargetRotation.X += x*elapsed;
TargetRotation.Y += y*elapsed;
}
}
public void ControllerZoom(float z)
{
lock (syncRoot)
{
float v = (z < 0) ? (1.0f - z) : (z > 0) ? (1.0f / (1.0f + z)) : 1.0f;
TargetDistance *= v;
OrthographicTargetSize *= v;
}
}
public void MouseRotate(int x, int y)
{
lock (syncRoot)
{
TargetRotation.X += x * Sensitivity;
TargetRotation.Y += y * Sensitivity;
}
}
public void MouseZoom(int z)
{
lock (syncRoot)
{
float v = (z < 0) ? 1.1f : (z > 0) ? 1.0f / 1.1f : 1.0f;
TargetDistance *= v;
OrthographicTargetSize *= v;
}
}
}
public class Frustum
{
public Plane[] Planes = new Plane[6];
public Vector3 Position;
public void Update(ref Matrix vp)
{
//left, right, top, bottom, near, far
Planes[0] = Plane.Normalize(new Plane((vp.M14 + vp.M11), (vp.M24 + vp.M21), (vp.M34 + vp.M31), (vp.M44 + vp.M41)));
Planes[1] = Plane.Normalize(new Plane((vp.M14 - vp.M11), (vp.M24 - vp.M21), (vp.M34 - vp.M31), (vp.M44 - vp.M41)));
Planes[2] = Plane.Normalize(new Plane((vp.M14 - vp.M12), (vp.M24 - vp.M22), (vp.M34 - vp.M32), (vp.M44 - vp.M42)));
Planes[3] = Plane.Normalize(new Plane((vp.M14 + vp.M12), (vp.M24 + vp.M22), (vp.M34 + vp.M32), (vp.M44 + vp.M42)));
Planes[4] = Plane.Normalize(new Plane((vp.M14 - vp.M13), (vp.M24 - vp.M23), (vp.M34 - vp.M33), (vp.M44 - vp.M43)));
Planes[5] = Plane.Normalize(new Plane((vp.M13), (vp.M23), (vp.M33), 0.0f));//(vp.M43));
}
//public bool ContainsSphere(ref Vector3 c, float cls, float r)
//{
// //cls = c length squared, for optimization
// if (cls < (r * r))
// {
// return true; //frustrum center is in the sphere
// }
// float nr = -r;
// for (int i = 0; i < 6; i++)
// {
// if (Plane.DotCoordinate(Planes[i], c) < nr)
// {
// return false;
// }
// }
// return true;
//}
public bool ContainsSphereNoClipNoOpt(ref Vector3 c, float r)
{
float nr = -r;
for (int i = 0; i < 5; i++)
{
if (Plane.DotCoordinate(Planes[i], c) < nr)
{
return false;
}
}
return true;
}
public bool ContainsAABBNoClip(ref Vector3 cen, ref Vector3 e)
{
var c = cen - Position;
for (int i = 0; i < 5; i++)
{
var pn = Planes[i].Normal;
var d = (c.X * pn.X) + (c.Y * pn.Y) + (c.Z * pn.Z); //Vector3.Dot(c, pn);//
var r = (e.X * (pn.X > 0 ? pn.X : -pn.X)) + (e.Y * (pn.Y > 0 ? pn.Y : -pn.Y)) + (e.Z * (pn.Z > 0 ? pn.Z : -pn.Z)); //Vector3.Dot(e, pn.Abs()); //
if ((d + r) < 0) return false;
}
return true;
}
public bool ContainsAABBNoClipNoOpt(ref Vector3 bmin, ref Vector3 bmax)
{
var c = (bmax + bmin) * 0.5f - Position;
var e = (bmax - bmin) * 0.5f;
for (int i = 0; i < 5; i++)
{
var pd = Planes[i].D;
var pn = Planes[i].Normal;
var d = (c.X * pn.X) + (c.Y * pn.Y) + (c.Z * pn.Z);
var r = (e.X * (pn.X > 0 ? pn.X : -pn.X)) + (e.Y * (pn.Y > 0 ? pn.Y : -pn.Y)) + (e.Z * (pn.Z > 0 ? pn.Z : -pn.Z));
if ((d + r) < -pd) return false;
//if ((d - r) < -pd) ; //intersecting
}
return true;
}
public bool ContainsAABBNoFrontClipNoOpt(ref Vector3 bmin, ref Vector3 bmax)
{
var c = (bmax + bmin) * 0.5f - Position;
var e = (bmax - bmin) * 0.5f;
for (int i = 0; i < 4; i++)
{
var pd = Planes[i].D;
var pn = Planes[i].Normal;
var d = (c.X * pn.X) + (c.Y * pn.Y) + (c.Z * pn.Z);
var r = (e.X * (pn.X > 0 ? pn.X : -pn.X)) + (e.Y * (pn.Y > 0 ? pn.Y : -pn.Y)) + (e.Z * (pn.Z > 0 ? pn.Z : -pn.Z));
if ((d + r) < -pd) return false;
//if ((d - r) < -pd) ; //intersecting
}
return true;
}
}
}