|
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 set_view(self, view): |
|
42
|
|
|
# Initialize view mode |
|
43
|
|
|
self.view = view |
|
44
|
|
|
|
|
45
|
|
|
# set proper function to setup projection |
|
46
|
|
|
if self.camType is MODE_ORTHOGRAPHIC: |
|
47
|
|
|
self.set_projection = self._proj_ortho |
|
48
|
|
|
if not self.view.if_projection('ortho'): |
|
49
|
|
|
self.view.new_projection('ortho', self.projection) |
|
50
|
|
|
elif self.camType is MODE_PERSPECTIVE: |
|
51
|
|
|
self.set_projection = self._proj_persp |
|
52
|
|
|
if not self.view.if_projection('persp'): |
|
53
|
|
|
self.view.new_projection('persp', self.projection) |
|
54
|
|
|
else: |
|
55
|
|
|
self.set_projection = None |
|
56
|
|
|
|
|
57
|
|
|
def _proj_ortho(self, left, right, bottom, top, znear, zfar): |
|
58
|
|
|
self.projection = matrix.orthographic(left, right, bottom, top, znear, zfar) |
|
59
|
|
|
|
|
60
|
|
|
def _proj_persp(self, fov, aspect, znear, zfar): |
|
61
|
|
|
self.projection = matrix.perspective(fov, aspect, znear, zfar) |
|
62
|
|
|
|
|
63
|
|
|
def set_program(self, program): |
|
64
|
|
|
if self.camType is MODE_ORTHOGRAPHIC: |
|
65
|
|
|
self.view.register_shader('ortho', program) |
|
66
|
|
|
elif self.camType is MODE_PERSPECTIVE: |
|
67
|
|
|
self.view.register_shader('persp', program) |
|
68
|
|
|
else: |
|
69
|
|
|
return NotImplemented |
|
70
|
|
|
|
|
71
|
|
|
def make_current(self): |
|
72
|
|
|
if self.camType is MODE_ORTHOGRAPHIC: |
|
73
|
|
|
self.view.set_projection('ortho', self.projection) |
|
74
|
|
|
elif self.camType is MODE_PERSPECTIVE: |
|
75
|
|
|
self.view.set_projection('persp', self.projection) |
|
76
|
|
|
else: |
|
77
|
|
|
return NotImplemented |
|
78
|
|
|
|
|
79
|
|
|
def calcViewMatrix(self): |
|
80
|
|
|
self.cameraRotation = self.rotation.conjugate().toMatrix() |
|
81
|
|
|
self.cameraPosition = self.position * -1.0 |
|
82
|
|
|
self.cameraTranslation = matrix.Matrix(4).translate(self.cameraPosition) |
|
83
|
|
|
|
|
84
|
|
|
self.viewMatrix = self.cameraTranslation * self.cameraRotation |
|
85
|
|
|
self.viewMatrixInverse = self.viewMatrix.inverse() |
|
86
|
|
|
|
|
87
|
|
|
@property |
|
88
|
|
|
def vec_up(self): |
|
89
|
|
|
return self.rotation.getUp() |
|
90
|
|
|
|
|
91
|
|
|
@property |
|
92
|
|
|
def vec_down(self): |
|
93
|
|
|
return self.rotation.getDown() |
|
94
|
|
|
|
|
95
|
|
|
@property |
|
96
|
|
|
def vec_left(self): |
|
97
|
|
|
return self.rotation.getLeft() |
|
98
|
|
|
|
|
99
|
|
|
@property |
|
100
|
|
|
def vec_right(self): |
|
101
|
|
|
return self.rotation.getRight() |
|
102
|
|
|
|
|
103
|
|
|
@property |
|
104
|
|
|
def vec_forward(self): |
|
105
|
|
|
return self.rotation.getForward() |
|
106
|
|
|
|
|
107
|
|
|
@property |
|
108
|
|
|
def vec_back(self): |
|
109
|
|
|
return self.rotation.getBack() |
|
110
|
|
|
|
|
111
|
|
|
def rotate(self, axis, angle): |
|
112
|
|
|
self.rotation = (quaternion.quat_from_axis_angle(axis, angle) * self.rotation).normalize() |
|
113
|
|
|
|
|
114
|
|
|
def move(self, direction, amount): |
|
115
|
|
|
self.position = self.position + (direction * amount * self.sensitivity) |
|
116
|
|
|
|
|
117
|
|
|
def setPosition(self, position): |
|
118
|
|
|
self.position = position |
|
119
|
|
|
|
|
120
|
|
|
def getViewMatrix(self): |
|
121
|
|
|
self.calcViewMatrix() |
|
122
|
|
|
return self.viewMatrix |