-
Notifications
You must be signed in to change notification settings - Fork 29
/
demo.py
250 lines (214 loc) · 10 KB
/
demo.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import pyglet
from OpenGL import GL
import numpy as np
import sys
# No es necesario este bloque de código si se ejecuta desde la carpeta raíz del repositorio
# v
if sys.path[0] != "":
sys.path.insert(0, "")
sys.path.append('../../')
# ^
# No es necesario este bloque de código si se ejecuta desde la carpeta raíz del repositorio
import auxiliares.utils.shapes as shapes
from auxiliares.utils.camera import FreeCamera
from auxiliares.utils.scene_graph import SceneGraph
from auxiliares.utils.drawables import Model, Texture, DirectionalLight, PointLight, SpotLight, Material
from auxiliares.utils.helpers import init_axis, init_pipeline, mesh_from_file, get_path
WIDTH = 640
HEIGHT = 640
class Controller(pyglet.window.Window):
def __init__(self, title, *args, **kargs):
super().__init__(*args, **kargs)
self.set_minimum_size(240, 240) # Evita error cuando se redimensiona a 0
self.set_caption(title)
self.key_handler = pyglet.window.key.KeyStateHandler()
self.push_handlers(self.key_handler)
self.program_state = { "total_time": 0.0, "camera": None }
self.init()
def init(self):
GL.glClearColor(1, 1, 1, 1.0)
GL.glEnable(GL.GL_DEPTH_TEST)
GL.glEnable(GL.GL_CULL_FACE)
GL.glCullFace(GL.GL_BACK)
GL.glFrontFace(GL.GL_CCW)
def is_key_pressed(self, key):
return self.key_handler[key]
if __name__ == "__main__":
# Instancia del controller
controller = Controller("Demo", width=WIDTH, height=HEIGHT, resizable=True)
controller.program_state["camera"] = FreeCamera([1, 2, 2], "perspective")
controller.program_state["camera"].yaw = -3* np.pi/ 4
controller.program_state["camera"].pitch = -np.pi / 4
axis_scene = init_axis(controller)
color_mesh_pipeline = init_pipeline(
get_path("auxiliares/shaders/color_mesh.vert"),
get_path("auxiliares/shaders/color_mesh.frag"))
textured_mesh_pipeline = init_pipeline(
get_path("auxiliares/shaders/textured_mesh.vert"),
get_path("auxiliares/shaders/textured_mesh.frag"))
color_mesh_lit_pipeline = init_pipeline(
get_path("auxiliares/shaders/color_mesh_lit.vert"),
get_path("auxiliares/shaders/color_mesh_lit.frag"))
textured_mesh_lit_pipeline = init_pipeline(
get_path("auxiliares/shaders/textured_mesh_lit.vert"),
get_path("auxiliares/shaders/textured_mesh_lit.frag"))
cube = Model(shapes.Cube["position"], shapes.Cube["uv"], shapes.Cube["normal"], index_data=shapes.Cube["indices"])
pyramid = Model(shapes.SquarePyramid["position"], shapes.SquarePyramid["uv"], shapes.SquarePyramid["normal"], index_data=shapes.SquarePyramid["indices"])
triangle = Model(shapes.Triangle["position"], shapes.Triangle["uv"], shapes.Triangle["normal"])
quad = Model(shapes.Square["position"], shapes.Square["uv"], shapes.Square["normal"], index_data=shapes.Square["indices"])
arrow = mesh_from_file("assets/arrow.off")[0]["mesh"]
bricks = Texture("assets/bricks.jpg")
wall2 = Texture("assets/wall2.jpg")
boo = Texture("assets/boo.png", maxFilterMode=GL.GL_NEAREST)
graph = SceneGraph(controller)
graph.add_node("sun",
pipeline=[color_mesh_lit_pipeline, textured_mesh_lit_pipeline],
position=[0, 2, 0],
light=DirectionalLight(diffuse = [0.5, 0.5, 0.5], specular = [0.5, 0.25, 0.5], ambient = [0.15, 0.15, 0.15]))
graph.add_node("sun_arrow",
attach_to="sun",
mesh=arrow,
position=[0, 0, -0.5],
rotation=[-np.pi, 0, 0],
scale=[0.5, 0.5, 0.5],
color=[1,1,0],
pipeline=color_mesh_pipeline)
graph.add_node("point_lights")
graph.add_node("point_light1",
attach_to="point_lights",
pipeline=[color_mesh_lit_pipeline, textured_mesh_lit_pipeline],
position=[8, 1, 8],
light=PointLight(diffuse = [0, 0, 1], specular = [1, 1, 0], ambient = [0.1, 0.1, 0.1]))
graph.add_node("point_light2",
attach_to="point_lights",
pipeline=[color_mesh_lit_pipeline, textured_mesh_lit_pipeline],
position=[-8, 1, -8],
light=PointLight(diffuse = [0, 1, 0], specular = [1, 0, 1], ambient = [0.1, 0.1, 0.1]))
graph.add_node("point_light3",
attach_to="point_lights",
pipeline=[color_mesh_lit_pipeline, textured_mesh_lit_pipeline],
position=[-8, 1, 8],
light=PointLight(diffuse = [1, 0, 0], specular = [0, 1, 1], ambient = [0.1, 0.1, 0.1]))
graph.add_node("spotlight",
pipeline=[color_mesh_lit_pipeline, textured_mesh_lit_pipeline],
position=[0, 3, 0],
rotation=[-np.pi/4, 0, 0],
light=SpotLight(diffuse = [1, 1, 1],
specular = [0, 1, 0],
ambient = [0.1, 0.1, 0.1]))
graph.add_node("shapes")
# mesh_from_file() devuelve una lista de diccionarios, cada uno con la información de un mesh
# [{id, mesh, texture}, ...]
zorzal = mesh_from_file("assets/zorzal.obj")
graph.add_node("zorzal")
for i in range(len(zorzal)):
graph.add_node(zorzal[i]["id"],
attach_to="zorzal",
mesh=zorzal[i]["mesh"],
pipeline=textured_mesh_lit_pipeline,
material=Material(),
texture=zorzal[i]["texture"],
cull_face=False)
auto = mesh_from_file("assets/auto.off")
graph.add_node(auto[0]["id"],
attach_to="root",
mesh=auto[0]["mesh"],
pipeline=color_mesh_lit_pipeline,
material=Material(),
cull_face=False)
graph.add_node("cube",
attach_to="shapes",
mesh = cube,
pipeline = textured_mesh_lit_pipeline,
texture = bricks,
position = [-2, 0, 0],
material = Material(
diffuse = [1, 0, 0],
specular = [1, 0, 1],
ambient = [0.1, 0, 0],
shininess = 128
))
graph.add_node("pyramid",
attach_to="shapes",
mesh = pyramid,
pipeline = textured_mesh_lit_pipeline,
position = [2, 0, 0],
material = Material(
diffuse = [0, 0, 1],
specular = [0, 1, 0],
ambient = [0.1, 0, 0],
shininess = 32
))
graph.add_node("boo",
attach_to="shapes",
mesh = quad,
pipeline = textured_mesh_pipeline,
position = [0, 0, -2],
texture = boo,
color = [0, 0, 1])
graph.add_node("floor",
attach_to="shapes",
mesh = quad,
pipeline = textured_mesh_lit_pipeline,
position = [0, -1, 0],
rotation = [-np.pi/2, 0, 0],
scale = [20, 20, 20],
texture=wall2,
material = Material(
diffuse = [1, 1, 1],
specular = [0.5, 0.5, 0.5],
ambient = [0.1, 0.1, 0.1],
shininess = 256
))
def update(dt):
controller.program_state["total_time"] += dt
camera = controller.program_state["camera"]
sun = graph["sun"]
if controller.is_key_pressed(pyglet.window.key.A):
camera.position -= camera.right * dt
if controller.is_key_pressed(pyglet.window.key.D):
camera.position += camera.right * dt
if controller.is_key_pressed(pyglet.window.key.W):
camera.position += camera.forward * dt
if controller.is_key_pressed(pyglet.window.key.S):
camera.position -= camera.forward * dt
if controller.is_key_pressed(pyglet.window.key.Q):
camera.position[1] -= dt
if controller.is_key_pressed(pyglet.window.key.E):
camera.position[1] += dt
if controller.is_key_pressed(pyglet.window.key._1):
camera.type = "perspective"
if controller.is_key_pressed(pyglet.window.key._2):
camera.type = "orthographic"
camera.update()
if controller.is_key_pressed(pyglet.window.key.UP):
sun["rotation"][0] -= 2 * dt
if controller.is_key_pressed(pyglet.window.key.DOWN):
sun["rotation"][0] += 2 * dt
if controller.is_key_pressed(pyglet.window.key.LEFT):
sun["rotation"][1] -= 2 * dt
if controller.is_key_pressed(pyglet.window.key.RIGHT):
sun["rotation"][1] += 2 * dt
graph["point_lights"]["rotation"][1] += 2 * dt
@controller.event
def on_resize(width, height):
controller.program_state["camera"].resize(width, height)
@controller.event
def on_mouse_drag(x, y, dx, dy, buttons, modifiers):
if buttons & pyglet.window.mouse.RIGHT:
controller.program_state["camera"].yaw += dx * 0.01
controller.program_state["camera"].pitch += dy * 0.01
if buttons & pyglet.window.mouse.LEFT:
graph["root"]["rotation"][0] += dy * 0.01
graph["root"]["rotation"][1] += dx * 0.01
if buttons & pyglet.window.mouse.MIDDLE:
graph["spotlight"]["rotation"][0] += dy * 0.01
graph["spotlight"]["rotation"][1] += dx * 0.01
# draw loop
@controller.event
def on_draw():
controller.clear()
axis_scene.draw()
graph.draw()
pyglet.clock.schedule_interval(update, 1/60)
pyglet.app.run()