1
|
|
|
from gem import vector |
2
|
|
|
from gem import matrix |
3
|
|
|
from gem import quaternion |
4
|
|
|
|
5
|
|
|
# Modes |
6
|
|
|
MODE_ORTHOGRAPHIC = 1 |
7
|
|
|
MODE_PERSPECTIVE = 2 |
8
|
|
|
|
9
|
|
|
class Camera(object): |
10
|
|
|
def __init__(self, camType): |
11
|
|
|
|
12
|
|
|
self.camType = camType |
13
|
|
|
|
14
|
|
|
# Camera information |
15
|
|
|
self.yAxis = vector.Vector(3, data=[0.0, 1.0, 0.0]) |
16
|
|
|
self.position = vector.Vector(3) |
17
|
|
|
self.rotation = quaternion.Quaternion() |
18
|
|
|
self.sensitivity = 0.05 |
19
|
|
|
self.aperature = 0 |
20
|
|
|
|
21
|
|
|
# Camera modifiers |
22
|
|
|
self.cameraRotation = matrix.Matrix(4) |
23
|
|
|
self.cameraPosition = matrix.Matrix(4) |
24
|
|
|
self.cameraTranslation = matrix.Matrix(4) |
25
|
|
|
|
26
|
|
|
# These are required for rendering |
27
|
|
|
self.projection = matrix.Matrix(4) |
28
|
|
|
self.viewMatrix = matrix.Matrix(4) |
29
|
|
|
self.viewMatrixInverse = matrix.Matrix(4) |
30
|
|
|
|
31
|
|
|
# Camera will rotate around this point |
32
|
|
|
self.originVisiblity = True |
33
|
|
|
self.originPosition = [0.0, 0.0, 0.0] |
34
|
|
|
|
35
|
|
|
# Arcball properties |
36
|
|
|
self.cur_x = 0 |
37
|
|
|
self.cur_y = 0 |
38
|
|
|
self.arcball_on = False |
39
|
|
|
|
40
|
|
|
def get_mode(self): |
41
|
|
|
return self.camType |
42
|
|
|
|
43
|
|
|
def clone(self): |
44
|
|
|
camNew = Camera(self.camType) |
45
|
|
|
camNew.set_view(self.view) |
46
|
|
|
camNew.set_program(self.program) |
47
|
|
|
camNew.projection = self.projection |
48
|
|
|
camNew.position = self.position |
49
|
|
|
camNew.rotation = self.rotation |
50
|
|
|
return camNew |
51
|
|
|
|
52
|
|
|
def set_view(self, view): |
53
|
|
|
# Initialize view mode |
54
|
|
|
self.view = view |
55
|
|
|
|
56
|
|
|
# set proper function to setup projection |
57
|
|
|
if self.camType is MODE_ORTHOGRAPHIC: |
58
|
|
|
self.set_projection = self._proj_ortho |
59
|
|
|
if not self.view.if_projection('ortho'): |
60
|
|
|
self.view.new_projection('ortho', self.projection) |
61
|
|
|
elif self.camType is MODE_PERSPECTIVE: |
62
|
|
|
self.set_projection = self._proj_persp |
63
|
|
|
if not self.view.if_projection('persp'): |
64
|
|
|
self.view.new_projection('persp', self.projection) |
65
|
|
|
else: |
66
|
|
|
self.set_projection = None |
67
|
|
|
|
68
|
|
|
def _proj_ortho(self, left, right, bottom, top, znear, zfar): |
69
|
|
|
self.projection = matrix.orthographic(left, right, bottom, top, znear, zfar) |
70
|
|
|
|
71
|
|
|
def _proj_persp(self, fov, aspect, znear, zfar): |
72
|
|
|
self.projection = matrix.perspective(fov, aspect, znear, zfar) |
73
|
|
|
|
74
|
|
|
def set_program(self, program): |
75
|
|
|
self.program = program |
76
|
|
|
if self.camType is MODE_ORTHOGRAPHIC: |
77
|
|
|
self.view.register_shader('ortho', program) |
78
|
|
|
elif self.camType is MODE_PERSPECTIVE: |
79
|
|
|
self.view.register_shader('persp', program) |
80
|
|
|
else: |
81
|
|
|
return NotImplemented |
82
|
|
|
|
83
|
|
|
def make_current(self): |
84
|
|
|
if self.camType is MODE_ORTHOGRAPHIC: |
85
|
|
|
self.view.set_projection('ortho', self.projection) |
86
|
|
|
elif self.camType is MODE_PERSPECTIVE: |
87
|
|
|
self.view.set_projection('persp', self.projection) |
88
|
|
|
else: |
89
|
|
|
return NotImplemented |
90
|
|
|
|
91
|
|
|
def calcViewMatrix(self): |
92
|
|
|
self.cameraRotation = self.rotation.conjugate().toMatrix() |
93
|
|
|
self.cameraPosition = self.position * -1.0 |
94
|
|
|
self.cameraTranslation = matrix.Matrix(4).translate(self.cameraPosition) |
95
|
|
|
|
96
|
|
|
self.viewMatrix = self.cameraTranslation * self.cameraRotation |
97
|
|
|
self.viewMatrixInverse = self.viewMatrix.inverse() |
98
|
|
|
|
99
|
|
|
@property |
100
|
|
|
def vec_up(self): |
101
|
|
|
return self.rotation.getUp() |
102
|
|
|
|
103
|
|
|
@property |
104
|
|
|
def vec_down(self): |
105
|
|
|
return self.rotation.getDown() |
106
|
|
|
|
107
|
|
|
@property |
108
|
|
|
def vec_left(self): |
109
|
|
|
return self.rotation.getLeft() |
110
|
|
|
|
111
|
|
|
@property |
112
|
|
|
def vec_right(self): |
113
|
|
|
return self.rotation.getRight() |
114
|
|
|
|
115
|
|
|
@property |
116
|
|
|
def vec_forward(self): |
117
|
|
|
return self.rotation.getForward() |
118
|
|
|
|
119
|
|
|
@property |
120
|
|
|
def vec_back(self): |
121
|
|
|
return self.rotation.getBack() |
122
|
|
|
|
123
|
|
|
def rotate(self, axis, angle): |
124
|
|
|
self.rotation = (quaternion.quat_from_axis_angle(axis, angle) * self.rotation).normalize() |
125
|
|
|
|
126
|
|
|
def move(self, direction, amount): |
127
|
|
|
self.position = self.position + (direction * amount * self.sensitivity) |
128
|
|
|
|
129
|
|
|
def setPosition(self, position): |
130
|
|
|
self.position = position |
131
|
|
|
|
132
|
|
|
def getViewMatrix(self): |
133
|
|
|
self.calcViewMatrix() |
134
|
|
|
return self.viewMatrix |
135
|
|
|
|