Completed
Push — master ( 2154a3...ce8cea )
by
unknown
01:03
created

ed2d.Camera.set_program()   A

Complexity

Conditions 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 10
rs 9.4285
1
import math
2
from gem import vector
3
from gem import matrix
4
from gem import quaternion
5
from ed2d import view
6
7
class Camera(object):
8
    def __init__(self):
9
10
        # Camera information
11
        self.yAxis = vector.Vector(3, data=[0.0, 1.0, 0.0])
12
        self.position = vector.Vector(3)
13
        self.rotation = quaternion.Quaternion()
14
        self.sensitivity = 0.05
15
        self.aperature = 0
16
17
        # Camera modifiers
18
        self.cameraRotation = matrix.Matrix(4)
19
        self.cameraPosition = matrix.Matrix(4)
20
        self.cameraTranslation = matrix.Matrix(4)
21
22
        # These are required for rendering
23
        self.perspectiveProj = matrix.Matrix(4)
24
        self.orthographicProj = matrix.Matrix(4)
25
        self.viewMatrix = matrix.Matrix(4)
26
        self.viewMatrixInverse = matrix.Matrix(4)
27
28
        # Camera will rotate around this point
29
        self.originVisiblity = True
30
        self.originPosition = [0.0, 0.0, 0.0]
31
32
        # Arcball propertise
33
        self.cur_x = 0
34
        self.cur_y = 0
35
        self.arcball_on = False
36
37
        # Initialize view mode
38
        self.view = view.View()
39
40
        # Modes
41
        self.MODE_ORTHOGRAPHIC = 1
42
        self.MODE_PERSPECTIVE = 2
43
        self.currentMode = 0
44
45
46
    def perspectiveProjection(self, fov, aspect, znear, zfar):
47
        self.perspectiveProj = matrix.perspective(fov, aspect, znear, zfar)
48
        self.view.new_projection('persp', self.perspectiveProj)
49
50
    def orthographicProjection(self, left, right, bottom, top, znear, zfar):
51
        self.orthographicProj = matrix.orthographic(left, right, bottom, top, znear, zfar)
52
        self.view.new_projection('ortho', self.orthographicProj)
53
54
    def set_program(self, mtype, program):
55
        if mtype is 1:
56
            self.currentMode = self.MODE_ORTHOGRAPHIC
57
            self.view.register_shader('ortho', program)
58
        elif mtype is 2:
59
            self.currentMode = self.MODE_PERSPECTIVE
60
            self.view.register_shader('persp', program)
61
        else:
62
            self.currentMode = 0
63
            return NotImplemented
64
65
    def set_mode(self, mtype):
66
        if mtype is 1:
67
            self.currentMode = self.MODE_ORTHOGRAPHIC
68
            self.view.set_projection('ortho', self.orthographicProj)
69
        elif mtype is 2:
70
            self.currentMode = self.MODE_PERSPECTIVE
71
            self.view.set_projection('persp', self.perspectiveProj)
72
        else:
73
            self.currentMode = 0
74
            return NotImplemented
75
76
    def calcViewMatrix(self):
77
        self.cameraRotation = self.rotation.conjugate().toMatrix()
78
        self.cameraPosition = self.position * -1.0
79
        self.cameraTranslation = matrix.Matrix(4).translate(self.cameraPosition)
80
81
        self.viewMatrix = self.cameraTranslation * self.cameraRotation
82
        self.viewMatrixInverse = self.viewMatrix.inverse()
83
84
85
    def onKeys(self, keys, tick):
86
        moveAmount = 0.5 * tick
87
88
        if 'w' in keys:
89
            self.move(self.rotation.getForward(), -moveAmount)
90
91
        if 's' in keys:
92
            self.move(self.rotation.getForward(), moveAmount)
93
94
        if 'a' in keys:
95
            self.move(self.rotation.getLeft(), moveAmount)
96
97
        if 'd' in keys:
98
            self.move(self.rotation.getRight(), moveAmount)
99
100
        if 'q' in keys:
101
            self.move(self.rotation.getUp(), moveAmount)
102
103
        if 'e' in keys:
104
            self.move(self.rotation.getUp(), -moveAmount)
105
106
        if 'UP' in keys:
107
            self.rotate(self.rotation.getRight(), moveAmount * 0.05)
108
109
        if 'DOWN' in keys:
110
            self.rotate(self.rotation.getRight(), -moveAmount * 0.05)
111
112
        if 'LEFT' in keys:
113
            self.rotate(self.rotation.getUp(), moveAmount * 0.05)
114
115
        if 'RIGHT' in keys:
116
            self.rotate(self.rotation.getUp(), -moveAmount * 0.05)
117
118
    def onMouseMove(self, deltaX, deltaY, tick):
119
        sensitivity = 0.5
120
        if deltaX != 0:
121
            self.rotate(self.yAxis, math.radians(deltaX * sensitivity * tick))
122
123
        if deltaY != 0:
124
            self.rotate(self.rotation.getRight(), math.radians(deltaY * sensitivity * tick))
125
126
    def rotate(self, axis, angle):
127
        self.rotation = (quaternion.quat_from_axis_angle(axis, angle) * self.rotation).normalize()
128
129
    def move(self, direction, amount):
130
        self.position = self.position + (direction * amount * self.sensitivity)
131
132
    def setPosition(self, position):
133
        self.position = position
134
135
    def getViewMatrix(self):
136
        self.calcViewMatrix()
137
        return self.viewMatrix
138