This repository was archived by the owner on Aug 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.py
More file actions
572 lines (480 loc) · 16.3 KB
/
camera.py
File metadata and controls
572 lines (480 loc) · 16.3 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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
from ctypes import c_float
import pyglet
from numpy.random import rand
import math
import pyglet.window.key as keyboard
import numpy as np
import vault
import logging
logger = logging.getLogger(__name__)
class Camera():
"""
Base camera class for Euclideon Vault Python Sample
This sets the default behaviour for a perspective camera
Stores the state of the camera, and provides functions for modifyting
that state
User input is passed from the VDKViewport object vio
the set_{}Pressed functions (for mapped functions)
Mouse Input is passed through the on_mouse_drag function
This is intended to be subclassed for custom camera behaviour
"""
def __init__(self, VDKview):
self.normalSpeed = 0.3
self.fastSpeed = 1
self.moveSpeed = self.normalSpeed
self.moveVelocity = [0, 0, 0]
self.matrix = np.identity(4)
self._view = VDKview
self.position = [0, 0, 0]
self.nearPlane = 0.01
self.farPlane = 2
self.FOV = 60
#booleans indicating button activation
self.forwardPressed = False
self.backPressed = False
self.rightPressed = False
self.leftPressed = False
self.upPressed = False
self.downPressed = False
self.shiftPressed = False
self.ctrlPressed = False
self.zoomInPressed = False
self.zoomOutPressed = False
self.theta = 0
self.phi = 0
self.zoom = 1
self.mouseSensitivity = 1 / 100
self.camRotation = [0, 0, 0]
self.lookAtTarget = [0, 0, 0]
self.rotationMatrix = np.array([[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
self.facingDirection = [0, 1, 0]
self.rotationAxis = np.array([0,0,1])
self.tangentVector = np.array([0,1,0])
self._projectionMatrix = []
self.controlString = """
W,S,A,D: Move\n
E: Move up\n
C: Move Down\n
Click + drag: Look around\n
Shift (Hold): Increase speed\n
O: Zoom in\n
P: Zoom out\n
"""
def on_cast(self):
"""
To be called when this class is converted to from another Camera derived class
ensures that appropriate variables are set in lieu of __init__being called without
resetting all variables
Returns
-------
"""
pass
@property
def position(self):
return self.__position
@position.setter
def position(self, newposition):
self.__position = tuple(newposition)
self.matrix[3, :3] = newposition
self._view.SetMatrix(vault.vdkRenderViewMatrix.Camera, self.matrix.flatten())
def get_controls_string(self):
return self.controlString
def get_view_vertices(self):
"""
Returns
-------
the extents of the viewing volume projected onto 2d space
"""
#TODO make this correctly display the location of near and far plane
rat = np.tan(self.FOV/2/180*np.pi)/self.farPlane
nearLeft = [-self.nearPlane * rat, self.nearPlane/self.farPlane]
farLeft = [-self.farPlane * rat, self.farPlane/self.farPlane]
nearRight = [self.nearPlane * rat, self.nearPlane/self.farPlane]
farRight = [self.farPlane * rat, self.farPlane/self.farPlane]
return [farLeft, nearLeft, nearRight, farRight]
def set_forwardPressed(self, val:bool):
self.forwardPressed = val
def set_backPressed(self, val):
self.backPressed = val
def set_rightPressed(self, val):
self.rightPressed = val
def set_leftPressed(self, val):
self.leftPressed = val
def set_upPressed(self, val):
self.upPressed = val
def set_downPressed(self, val):
self.downPressed = val
def set_shiftPressed(self, val):
self.shiftPressed = val
def set_ctrlPressed(self, val):
self.ctrlPressed = val
def set_zoomInPressed(self, val):
self.zoomInPressed = val
def set_zoomOutPressed(self, val):
self.zoomOutPressed = val
def reset_projection(self):
self.set_projection_perspective()
def on_key_press(self, symbol, modifiers):
"""
Defined for passing key presses not mapped using the key bindings in the view port
override subclasses
Parameters
----------
symbol
modifiers
Returns
-------
"""
pass
def on_key_release(self, symbol, modifiers):
pass
def rotate_polar(self, vec, dtheta, dphi):
"""
takes change in polar coordiantes and updates the camera rotation
based on it
Returns
-------
the a copy of vector vec rotated by dtheta in the xy plane and phi
"""
r = math.sqrt(vec[0]**2+vec[1]**2+vec[2]**2)
theta = math.atan2(vec[1], vec[0])
phi = math.acos(vec[2]/r)
#prevent rotation such that the vector is pointing directly up or down
thresh = 0.1
if abs(phi + dphi) < thresh or abs(phi + dphi - math.pi) < thresh:
dphi = 0
xprime = r * math.sin(phi+dphi)*math.cos(theta+dtheta)
yprime = r * math.sin(phi+dphi) * math.sin(theta + dtheta)
zprime = r * math.cos(phi+dphi)
self.phi = phi
self.theta = theta
return [xprime, yprime, zprime]
def set_projection_perspective(self, near=None, far=None, FOV=None):
if near is None:
near = self.nearPlane
if far is None:
far = self.farPlane
if FOV is None:
FOV = self.FOV
else:
self.FOV = FOV
FOV = FOV/180*np.pi
e = 1/np.tan(FOV/2)
a = self._view.height/self._view.width
self._projectionMatrix = \
[
e*a, 0, 0, 0,
0, 0, (far+near)/(far-near), 1,
0, e, 0, 0,
0, 0, -(2*far*near)/(far-near), 0
]
self._view.SetMatrix(vault.vdkRenderViewMatrix.Projection, self._projectionMatrix)
def set_projection_ortho(self, left, right, top, bottom, near, far):
self._projectionMatrix = \
[
2/(right-left), 0, 0, 0,
0, 0, 2/(far-near), 0,
0, 2/(top - bottom), 0, 0,
-(right+left)/(right-left), -(top+bottom)/(top-bottom), -(far+near)/(far-near), 1
]
self._view.SetMatrix(vault.vdkRenderViewMatrix.Projection, self._projectionMatrix)
def set_rotation(self, x=0, y=-5, z=0, roll=0, pitch=0, yaw=0):
"""
Sets the camera matrix to have a rotation of yaw, pictch roll
Parameters
----------
x
y
z
roll
pitch
yaw
Returns
-------
"""
sy = math.sin(yaw)
cy = math.cos(yaw)
sp = math.sin(pitch)
cp = math.cos(pitch)
sr = math.sin(roll)
cr = math.cos(roll)
self.matrix = np.array([
[cy*cp, cy*sp*sr-sy*cr, cy*sp*cr+sy*sr, 0],
[sy*cp, sy*sp*sr+cy*cr, sy*sp*cr-cy*sr, 0],
[-sp, cp*sr, cp*cr, 0],
[x, y, z, 1]
])
self.rotationMatrix = self.matrix[:3, :3]
self._view.SetMatrix(vault.vdkRenderViewMatrix.Camera, self.matrix.flatten())
def axisAngle(self, axis, theta):
#cTheta = np.dot(np.array([0,1,0]), dPoint) / np.linalg.norm(dPoint)
#theta = np.arccos(cTheta)
cTheta = np.cos(theta)
sTheta = np.sin(theta)
self.matrix = np.array(
[
[cTheta + axis[0] ** 2 * (1 - cTheta), axis[0] * axis[1] * (1 - cTheta) - axis[2] * sTheta, axis[0] * axis[2] * (1 - cTheta), 0],
[axis[1] * axis[0] * (1 - cTheta) + axis[2] * sTheta, cTheta + axis[1] ** 2 * (1 - cTheta), axis[1] * axis[2] * (1 - cTheta) - axis[0] * sTheta, 0],
[axis[2] * axis[0] * (1 - cTheta) - axis[1] * sTheta, axis[2] * axis[1] * (1 - cTheta) + axis[0] * sTheta, cTheta + axis[2] ** 2 * (1 - cTheta), 0],
[self.position[0], self.position[1], self.position[2], 1]
]
)
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
vec = self.rotate_polar(self.facingDirection,dx/100,dy/100)
self.look_direction(np.array(vec))
def look_at(self, lookAtPoint=None, cameraPosition=None):
"""
faces the camera at point2, positions the camera at point1
Parameters
----------
cameraPosition: position of the camera
lookAtPoint: x, y, z tuple to face the camera towards
"""
if cameraPosition is None:
cameraPosition = self.position
else:
self.position = cameraPosition
if lookAtPoint is None:
lookAtPoint = self.lookAtTarget
if not np.array_equal(lookAtPoint, cameraPosition):
#calculate our axis of rotation based on the distance between these points
dPoint = np.array(lookAtPoint) - np.array(cameraPosition)
else:
dPoint = np.array([1, 1, 0])
self.look_direction(dPoint)
def look_direction(self, dPoint: np.array):
"""
Points the camera in the direction vector dPoint
assumes that the tangent vector has a z value of zero (i.e. no roll)
Parameters
----------
dPoint
Returns
-------
"""
tangent = [0, 0, 0]
if dPoint[1] != 0:
tangent[0] = (dPoint[0]-np.sqrt(dPoint[0]**2+4*dPoint[1]**2))/(2*dPoint[1])
elif dPoint[2]>0:
tangent[0] = 1
else:
tangent[0] = -1
tangent[1] = 1-tangent[0]**2
tangent = -np.array(tangent)
tangent = tangent / np.sqrt(tangent.dot(tangent))
forward = dPoint/np.sqrt(dPoint.dot(dPoint))
axis = np.cross(tangent, forward)
axis = axis / np.sqrt(axis.dot(axis))
self.matrix = np.array(
[
[tangent[0], tangent[1], tangent[2], 0],
[forward[0], forward[1], forward[2], 0],
[axis[0], axis[1], axis[2], 0],
[self.position[0], self.position[1], self.position[2], 1]
]
)
self.rotationAxis = axis
self.tangentVector = tangent
self.rotationMatrix = self.matrix[:3, :3]
self.facingDirection = np.array([0,1,0]).dot(self.rotationMatrix).tolist()
self._view.SetMatrix(vault.vdkRenderViewMatrix.Camera, self.matrix.flatten())
def update_move_direction(self):
"""
updates the velocity and projection based on what keys have been pressed since the last call
"""
self.moveVelocity = [0, 0, 0]# in local coordinates
if self.shiftPressed:
self.moveSpeed = self.fastSpeed
else:
self.moveSpeed = self.normalSpeed
if self.forwardPressed:
self.moveVelocity[1] += self.moveSpeed
if self.backPressed:
self.moveVelocity[1] -= self.moveSpeed
if self.rightPressed:
self.moveVelocity[0] += self.moveSpeed
if self.leftPressed:
self.moveVelocity[0] -= self.moveSpeed
if self.upPressed:
self.moveVelocity[2] += self.moveSpeed
if self.downPressed:
self.moveVelocity[2] -= self.moveSpeed
if self.zoomInPressed:
self.zoom += 1
if self.zoomOutPressed and self.zoom>1:
self.zoom -= 1
self.mouseSensitivity = 0.1/self.zoom
self.set_projection_perspective(self.nearPlane, self.farPlane, 160/self.zoom)
self.moveVelocity = np.array(self.moveVelocity).dot(self.rotationMatrix).tolist()
def update_position(self, dt):
self.update_move_direction()
newposition = [0,0,0]
newposition[0] = self.position[0] + self.moveVelocity[0] * dt
newposition[1] = self.position[1] + self.moveVelocity[1] * dt
newposition[2] = self.position[2] + self.moveVelocity[2] * dt
self.position = newposition
class OrthoCamera(Camera):
def __init__(self, vdkView):
super().__init__(vdkView)
self.FOV = 90
def on_cast(self):
self.controlString = """
Ortho Camera (experimental):
W,S,A,D: Move\n
E: Move up\n
C: Move Down\n
Click + drag: Look around\n
Shift (Hold): Increase speed\n
O: Zoom in\n
P: Zoom out\n
"""
self.FOV = 90
def update_move_direction(self):
super().update_move_direction()
self.moveVelocity[2] = 0
v = np.array(self.moveVelocity)
mag = np.sqrt(v.dot(v))
if mag != 0:
self.moveVelocity = (v/mag).tolist()
if self.upPressed:
self.moveVelocity[2] += self.moveSpeed
if self.downPressed:
self.moveVelocity[2] -= self.moveSpeed
def update_position(self, dt):
super().update_position(dt)
ar = self._view.width/self._view.height
zoom = np.exp(self.zoom)
viewWidth = self.zoom/100
self.mouseSensitivity = 0.1/ zoom
self.set_projection_ortho(-ar/2*viewWidth, ar/2*viewWidth, 1/ar/2*viewWidth, -1/ar/2*viewWidth, self.nearPlane, self.farPlane)
def reset_projection(self):
pass
class MapCamera(OrthoCamera):
"""
Orthographic camera that follows a target and remains a set height above it
"""
def __init__(self, vdkView, target, elevation):
super().__init__(vdkView)
self.target = target
self.elevation = elevation
class DefaultTarget(object):
def __init__(self):
self.position = [0, 0, 0]
def on_cast(self):
pass
#here we override the default control behaviour of the camera
def update_move_direction(self):
pass
def on_mouse_drag(self, *args, **kwargs):
pass
def update_position(self, dt):
self.position = [self.target.position[0], self.target.position[1],self.target.position[2]+self.elevation]
self.look_direction(np.array([0, 0, -1]))
ar = self._view.width/self._view.height
zoom = self.zoom
self.set_projection_ortho(-ar/2*self.position[2]/zoom, ar/2*self.position[2]/zoom, 1/ar/2*self.position[2]/zoom, -1/ar/2*self.position[2]/zoom,self.nearPlane,self.farPlane)
class OrbitCamera(Camera):
"""
Movement of this camera is relative to a fixed point in space
"""
def on_cast(self):
self.controlString = """
Orbit Camera (experimental):
W,S,A,D: Move\n
E: Move up\n
C: Move Down\n
Click + drag: Move rotation Centre\n
Shift (Hold): Increase speed\n
O: Zoom in\n
P: Zoom out\n
"""
def update_move_direction(self):
self.look_at()
super(OrbitCamera, self).update_move_direction()
#self.moveVelocity = np.array(self.moveVelocity).dot(self.rotationMatrix).tolist()
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
horiz = dx * self.tangentVector * self.mouseSensitivity
vert = dy * self.rotationAxis * self.mouseSensitivity
if not self.ctrlPressed:
self.lookAtTarget = self.lookAtTarget + horiz + vert
else:
self.position = self.position - horiz - vert
class PerspectiveCamera(OrbitCamera):
def update_position(self, dt):
#self.facingDirection = np.array([0, 1, 0]).dot(self.rotationMatrix).tolist()
for i in range(3):
self.lookAtTarget[i] = self.position[i] + self.facingDirection[i]
super().update_position(dt)
class TrackCamera(Camera):
def update_position(self, dt):
self.lookAtTarget[1] += 0.0001
super().update_position(dt)
self.look_at()
class RecordCamera(Camera):
"""
A camera class for manual generation and replay of flythroughs of models
the user defines a set of waypoints by pressing space when the camera is positioned at
the desired locations
Pressing enter will replay the path
Backspace will delete the most recently added waypoint
"""
def __init__(self, *args, **kwargs):
super().__init(*args, **kwargs)
self.on_cast()
def on_cast(self):
self.controlString = """
Recording Camera:
W,S,A,D: Move\n
E: Move up\n
C: Move Down\n
Click + drag: Look around\n
Shift (Hold): Increase speed\n
O: Zoom in\n
P: Zoom out\n
Space: Record Position as Waypoint\n
Backspace: Remove Last Waypoint\n
Enter: Play back recorded path\n"""
try:
self.waypoints
except AttributeError:
self.waypoints = []
self.replayInd = 0
self.replaying = False
def on_key_press(self, symbol, modifiers):
if symbol == pyglet.window.key.SPACE:
self.waypoints.append(self.position.copy())
if symbol == pyglet.window.key.ENTER:
self.replaying = True
self.position = self.waypoints[0]
self.replayInd = 1
if symbol == pyglet.window.key.BACKSPACE:
self.waypoints.pop()
def update_move_direction(self):
try:
self.replaying
except AttributeError:
self.replaying = False
if not self.replaying:
super().update_move_direction()
return
#here we linearly interpolate the path and face the camera direction
dir = np.array(self.waypoints[self.replayInd]) - np.array(self.position)
mag = np.linalg.norm(dir)
ddir = dir - np.array(self.facingDirection)
#ddir = dir + np.array(self.lookAtTarget)-np.array(self.position)
#define the facing the one we are going in
dir = dir * self.moveSpeed
if abs(mag) < self.moveSpeed:
#we are as close as we can get in a single step to the waypoint
if self.replayInd+1 < len(self.waypoints):
#move to the next waypoint
self.replayInd += 1
else:
#end the replay
self.replaying = False
self.moveVelocity = [0, 0, 0]
self.look_direction(np.array(self.facingDirection) + ddir / 10)
self.moveVelocity = (dir / mag).tolist()
#self.look_at(self.waypoints[self.replayInd+1])