-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene.py
More file actions
208 lines (164 loc) · 6.77 KB
/
scene.py
File metadata and controls
208 lines (164 loc) · 6.77 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# pygame is just used to create a window with the operating system on which to draw.
import pygame
# imports all openGL functions
from OpenGL.GL import *
# import the shader class
from shaders import *
# import the camera class
from camera import Camera
# and we import a bunch of helper functions
from matutils import *
from lightSource import LightSource
class Scene:
'''
This is the main class for adrawing an OpenGL scene using the PyGame library
'''
def __init__(self, width=900, height=900, shaders=None):
'''
Initialises the scene
'''
self.window_size = (width, height)
# by default, wireframe mode is off
self.wireframe = False
# the first two lines initialise the pygame window. You could use another library for this,
# for example GLut or Qt
pygame.init()
screen = pygame.display.set_mode(self.window_size, pygame.OPENGL | pygame.DOUBLEBUF, 24)
# Here we start initialising the window from the OpenGL side
glViewport(0, 0, self.window_size[0], self.window_size[1])
# this selects the background color
glClearColor(0.17, 0.08, 0.15, 1.0)
# enable back face culling (see lecture on clipping and visibility
# glEnable(GL_CULL_FACE)
# depending on your model, or your projection matrix, the winding order may be inverted,
# Typically, you see the far side of the model instead of the front one
# uncommenting the following line should provide an easy fix.
# enable the vertex array capability
glEnableClientState(GL_VERTEX_ARRAY)
# enable depth test for clean output (see lecture on clipping & visibility for an explanation
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
# set the default shader program (can be set on a per-mesh basis)
self.shaders = 'flat'
# initialise the projective transform
near = 1.5
far = 50
left = -1.0
right = 1.0
top = -1.0
bottom = 1.0
# cycle through models
self.show_model = -1
# to start with, we use an orthographic projection; change this.
self.P = frustumMatrix(left,right,top,bottom,near,far)
# initialises the camera object
self.camera = Camera()
# initialise the light source
self.lights = [LightSource(self, position=[5., 5., 5.])]
# rendering mode for the shaders
self.mode = 1 # initialise to full interpolated shading
# This class will maintain a list of models to draw in the scene,
self.models = []
def add_model(self, model):
'''
This method just adds a model to the scene.
:param model: The model object to add to the scene
:return: None
'''
# bind the default shader to the mesh
model.bind_shader(self.shaders)
# and add to the list
self.models.append(model)
def add_models_list(self, models_list):
'''
This method just adds a model to the scene.
:param model: The model object to add to the scene
:return: None
'''
for model in models_list:
self.add_model(model)
def draw(self):
'''
Draw all models in the scene
:return: None
'''
# first we need to clear the scene, we also clear the depth buffer to handle occlusions
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# ensure that the camera view matrix is up to date
self.camera.update()
# then we loop over all models in the list and draw them
for model in self.models:
model.draw()
# once we are done drawing, we display the scene
# Note that here we use double buffering to avoid artefacts:
# we draw on a different buffer than the one we display,
# and flip the two buffers once we are done drawing.
pygame.display.flip()
def keyboard(self, event):
'''
Method to process keyboard events. Check Pygame documentation for a list of key events
:param event: the event object that was raised
'''
if event.key == pygame.K_q:
self.running = False
# flag to switch wireframe rendering
elif event.key == pygame.K_s:
if not self.animated:
self.animated = True
elif event.key == pygame.K_f:
if self.animated:
self.animated = False
def pygameEvents(self):
'''
Method to handle PyGame events for user interaction.
'''
# check whether the window has been closed
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
# keyboard events
elif event.type == pygame.KEYDOWN:
self.keyboard(event)
elif event.type == pygame.MOUSEBUTTONDOWN:
mods = pygame.key.get_mods()
if event.button == 4:
#pass
if mods & pygame.KMOD_CTRL:
self.light.position *= 1.1
self.light.update()
else:
self.camera.distance -= 0.5
elif event.button == 5:
#pass
if mods & pygame.KMOD_CTRL:
self.light.position *= 0.9
self.light.update()
else:
self.camera.distance += 0.5
elif event.type == pygame.MOUSEMOTION:
if pygame.mouse.get_pressed()[0]:
if self.mouse_mvt is not None:
self.mouse_mvt = pygame.mouse.get_rel()
self.camera.center[0] -= (float(self.mouse_mvt[0]) / self.window_size[0])/2
self.camera.center[1] -= (float(self.mouse_mvt[1]) / self.window_size[1])/2
else:
self.mouse_mvt = pygame.mouse.get_rel()
elif pygame.mouse.get_pressed()[2]:
if self.mouse_mvt is not None:
self.mouse_mvt = pygame.mouse.get_rel()
self.camera.phi -= (float(self.mouse_mvt[0]) / self.window_size[0])
self.camera.psi -= (float(self.mouse_mvt[1]) / self.window_size[1])
else:
self.mouse_mvt = pygame.mouse.get_rel()
else:
self.mouse_mvt = None
def run(self):
'''
Draws the scene in a loop until exit.
'''
# We have a classic program loop
self.running = True
while self.running:
self.pygameEvents()
# otherwise, continue drawing
self.draw()