-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGalaxyView.py
More file actions
137 lines (106 loc) · 5.21 KB
/
GalaxyView.py
File metadata and controls
137 lines (106 loc) · 5.21 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from pyglet.gl import *
import pywavefront
from pywavefront import visualization
import random
import math
import Util
import SkyBox
asteroid_model = pywavefront.Wavefront('resources/galaxy_view/asteroid.obj')
# This class encapsulates all things to do with displaying a view of the galaxy
class GalaxyView:
def __init__(self, window, fov):
# View variables - not influenced by the network state
self.view_numStars = 10000
self.view_starStdDev = 128
self.view_window = window
self.view_fov = fov
self.view_stars = self.generateStars();
self.view_skybox = SkyBox.SkyBox(pyglet.resource.image('resources/galaxy_view/skybox1_p3.png'))
# Spaceship Variables
self.spaceshipSpeed = 0.0
self.spaceshipPosition = [0, 0, 0]
self.spaceshipRotation = [0, 0, 0]
# Timing for view rotation events TODO: Move spaceship attributes into separate serializable class
self.rotating = False
self.rotTimePassed = 0
self.oldRotation = 0
self.newRotation = 0
def update(delta):
# Perform rotation then movement
if self.rotating:
progress = Util.getRotationInterval(self.rotTimePassed, 45, self.oldRotation, self.newRotation)
if not progress > 1:
self.setRotation([0.0, Util.angleSmoothLerp(self.oldRotation, self.newRotation, progress), 0.0])
self.rotTimePassed += delta
elif self.rotTimePassed != 0:
self.cancelRotation()
self.move(self.spaceshipSpeed*delta)
pyglet.clock.schedule_interval(update, 1/60.0)
@window.event
def on_resize(width, height):
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(self.view_fov, width / float(height), 0.1, 1000)
glMatrixMode(GL_MODELVIEW)
return pyglet.event.EVENT_HANDLED
def generateStars(self):
stars = []
for i in range(self.view_numStars*3):
stars.append((random.gauss(0.0, self.view_starStdDev)-0.5))
return stars
# Moves the spaceship along the correct direction TODO: add rising and falling to second argument
def move(self, movement):
self.spaceshipPosition = [self.spaceshipPosition[0] + movement*math.cos(math.radians(self.spaceshipRotation[1]+90)),
self.spaceshipPosition[1],
self.spaceshipPosition[2] + movement*math.sin(math.radians(self.spaceshipRotation[1]+90))]
#self.spaceshipPosition = [x + y for x, y in zip(self.spaceshipPosition, movement)]
def rotate(self, rotation):
# Combine the rotation and make it modulo 360 for compatability
self.spaceshipRotation = [(x + y) % 360 for x, y in zip(self.spaceshipRotation, rotation)]
def setPosition(self, x, y, z):
self.spaceshipPosition = [x, y, z]
def setRotation(self, rot):
self.spaceshipRotation = rot
def setSpeed(self, speed):
self.spaceshipSpeed = speed
# A map can be loaded. Then its corresponding objects will be displayed
def loadMap(self, mapLoad):
self.map = mapLoad
def cancelRotation(self):
self.rotTimePassed = 0
self.oldRotation = self.spaceshipRotation[1]
self.rotating = False
def setHeading(self, newHeading):
# Sets a new heading for the spaceship so it smoothly goes toward it
if self.rotating:
# If we're already rotating, use whatever the current rotation is as the old rotation
self.cancelRotation()
self.rotating = True
self.newRotation = newHeading
def render(self):
glLoadIdentity()
# Draw the skybox
self.view_skybox.draw(self.spaceshipRotation[0], self.spaceshipRotation[1])
# Move the camera
glLoadIdentity()
# TODO: Add spaceship position offset here before matrix transformation
glRotatef(self.spaceshipRotation[0], 1.0, 0.0, 0.0)
glRotatef(self.spaceshipRotation[1], 0.0, 1.0, 0.0)
glRotatef(self.spaceshipRotation[2], 0.0, 0.0, 1.0)
glTranslatef(*self.spaceshipPosition)
pyglet.graphics.draw(self.view_numStars, GL_POINTS, ("v3f", self.view_stars))
glEnable(GL_DEPTH_TEST)
for mapElem in self.map.getElements():
glLoadIdentity()
glRotatef(self.spaceshipRotation[0], 1.0, 0.0, 0.0)
glRotatef(self.spaceshipRotation[1], 0.0, 1.0, 0.0)
glRotatef(self.spaceshipRotation[2], 0.0, 0.0, 1.0)
glTranslatef(*self.spaceshipPosition)
glTranslatef(mapElem.x-(self.map.width//2), mapElem.h, -(mapElem.y-(self.map.height//2)))
if mapElem.orientation:
glRotatef(180.0, 0.0, 1.0, 0.0);
visualization.draw(asteroid_model)
else:
visualization.draw(asteroid_model)
glDisable(GL_DEPTH_TEST)