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