forked from boskee/Minecraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cameras.py
61 lines (45 loc) · 1.42 KB
/
cameras.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
# Imports, sorted alphabetically.
# Python packages
from math import radians, cos, sin
# Third-party packages
from pyglet.gl import *
# Modules from this project
# Nothing for now...
__all__ = (
'Camera3D',
)
class Camera3D:
def __init__(self, target=None):
self.target = target
self.x = 0.0
self.y = 0.0
self.z = 0.0
self.x_rotation = 0.0
self.y_rotation = 0.0
# if possible, we should use sinf and cosf from cython
self.has_sinf = True
try: sinf(1)
except NameError: self.has_sinf = False
def rotate(self, x, y):
self.x_rotation = x
self.y_rotation = y
def update(self, dt):
if self.target:
self.x, self.y, self.z = self.target.position
def transform(self):
glRotatef(self.x_rotation, 0, 1, 0)
x_r = radians(self.x_rotation)
if self.has_sinf:
glRotatef(-self.y_rotation, cosf(x_r), 0, sinf(x_r))
else:
glRotatef(-self.y_rotation, cos(x_r), 0, sin(x_r))
glTranslatef(-self.x, -self.y, -self.z)
def look(self):
glRotatef(self.x_rotation, 0, 1, 0)
x_r = radians(self.x_rotation)
if self.has_sinf:
glRotatef(-self.y_rotation, cosf(x_r), 0, sinf(x_r))
else:
glRotatef(-self.y_rotation, cos(x_r), 0, sin(x_r))
glTranslatef(0, -40.0, 0)
glRotatef(-90.0, 1, 0, 0)