-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera.py
63 lines (51 loc) · 1.55 KB
/
camera.py
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
from math import tan, radians
from PyQt4.QtGui import QVector3D, QMatrix4x4
class MayaCamera:
def __init__(self):
self.center = QVector3D(0.0, 0.0, 0.0)
self.focal_len = 5.0
self.y_rot = 0.0
self.x_rot = 0.0
self.fovy = 60.0
self.aspect = 1.0
self.znear = 0.1
self.zfar = 1000.0
def frame(self):
x = QVector3D(1.0, 0.0, 0.0)
y = QVector3D(0.0, 1.0, 0.0)
z = QVector3D(0.0, 0.0, 1.0)
m = QMatrix4x4()
m.rotate(self.y_rot, y)
x = m * x
z = m * z
m = QMatrix4x4()
m.rotate(self.x_rot, x)
y = m * y
z = m * z
return (x, y, z)
@property
def proj_matrix(self):
res = QMatrix4x4()
res.perspective(self.fovy, self.aspect, self.znear, self.zfar)
return res
@property
def view_matrix(self):
(x, y, z) = self.frame()
res = QMatrix4x4()
res.lookAt(self.center + self.focal_len * z, self.center, y)
return res
@property
def view_matrix_inv(self):
return self.view_matrix.inverted()[0]
def rotate(self, dx, dy):
self.y_rot -= dx * 360.0
self.x_rot -= dy * 360.0
def pan(self, dx, dy):
ly = 2.0 * self.focal_len * tan(radians(self.fovy / 2.0))
lx = ly * self.aspect
(x, y, z) = self.frame()
self.center -= dx * lx * x
self.center += dy * ly * y
def zoom(self, dx, dy):
self.focal_len *= (1.0 - dy) - dx
self.focal_len = max(self.focal_len, 0.1)