gamengine3d 1.1.0__tar.gz → 1.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {gamengine3d-1.1.0 → gamengine3d-1.3.0}/PKG-INFO +1 -1
- {gamengine3d-1.1.0 → gamengine3d-1.3.0}/gamengine3d/context.py +80 -1
- {gamengine3d-1.1.0 → gamengine3d-1.3.0}/gamengine3d/engine.py +1 -0
- {gamengine3d-1.1.0 → gamengine3d-1.3.0}/gamengine3d/helper.py +124 -105
- gamengine3d-1.3.0/gamengine3d/shaders/default.frag +63 -0
- gamengine3d-1.3.0/gamengine3d/shaders/default.vert +24 -0
- gamengine3d-1.3.0/gamengine3d/shaders/depth.frag +2 -0
- gamengine3d-1.3.0/gamengine3d/shaders/depth.vert +9 -0
- {gamengine3d-1.1.0 → gamengine3d-1.3.0}/gamengine3d.egg-info/PKG-INFO +1 -1
- {gamengine3d-1.1.0 → gamengine3d-1.3.0}/gamengine3d.egg-info/SOURCES.txt +3 -1
- {gamengine3d-1.1.0 → gamengine3d-1.3.0}/pyproject.toml +1 -1
- gamengine3d-1.1.0/gamengine3d/shaders/default.frag +0 -33
- gamengine3d-1.1.0/gamengine3d/shaders/default.vert +0 -20
- {gamengine3d-1.1.0 → gamengine3d-1.3.0}/LICENSE.txt +0 -0
- {gamengine3d-1.1.0 → gamengine3d-1.3.0}/README.md +0 -0
- {gamengine3d-1.1.0 → gamengine3d-1.3.0}/gamengine3d/__init__.py +0 -0
- {gamengine3d-1.1.0 → gamengine3d-1.3.0}/gamengine3d/game_objects.py +0 -0
- {gamengine3d-1.1.0 → gamengine3d-1.3.0}/gamengine3d/shaders/__init__.py +0 -0
- {gamengine3d-1.1.0 → gamengine3d-1.3.0}/gamengine3d.egg-info/dependency_links.txt +0 -0
- {gamengine3d-1.1.0 → gamengine3d-1.3.0}/gamengine3d.egg-info/requires.txt +0 -0
- {gamengine3d-1.1.0 → gamengine3d-1.3.0}/gamengine3d.egg-info/top_level.txt +0 -0
- {gamengine3d-1.1.0 → gamengine3d-1.3.0}/setup.cfg +0 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from .game_objects import GameObject
|
|
2
2
|
from .helper import Color, vector3d, Light, Camera, Renderer
|
|
3
3
|
from pygame.time import Clock
|
|
4
|
+
import time
|
|
4
5
|
|
|
5
6
|
class Functions:
|
|
6
7
|
def __init__(self):
|
|
@@ -15,6 +16,32 @@ class Functions:
|
|
|
15
16
|
self.remove_object = lambda name: None
|
|
16
17
|
self.remove_light = lambda name: None
|
|
17
18
|
|
|
19
|
+
class Delay:
|
|
20
|
+
def __init__(self, delay_time, callback):
|
|
21
|
+
self.start_time = time.perf_counter()
|
|
22
|
+
self.delay_time = delay_time
|
|
23
|
+
self.callback = callback
|
|
24
|
+
self.finished = False
|
|
25
|
+
|
|
26
|
+
def update(self):
|
|
27
|
+
if time.perf_counter() - self.start_time >= self.delay_time:
|
|
28
|
+
self.callback()
|
|
29
|
+
self.finished = True
|
|
30
|
+
|
|
31
|
+
class DelayVar:
|
|
32
|
+
def __init__(self, delay, obj, attr, value):
|
|
33
|
+
self.start_time = time.perf_counter()
|
|
34
|
+
self.delay = delay
|
|
35
|
+
self.obj = obj
|
|
36
|
+
self.attr = attr
|
|
37
|
+
self.value = value
|
|
38
|
+
self.finished = False
|
|
39
|
+
|
|
40
|
+
def update(self):
|
|
41
|
+
if time.perf_counter() - self.start_time >= self.delay:
|
|
42
|
+
setattr(self.obj, self.attr, self.value)
|
|
43
|
+
self.finished = True
|
|
44
|
+
|
|
18
45
|
class KeyCallback:
|
|
19
46
|
def __init__(self, key, event_type, callback, args=None, dt=False):
|
|
20
47
|
self.callback = callback
|
|
@@ -68,6 +95,23 @@ class KeyCallbackVar:
|
|
|
68
95
|
if self.key in releaed_keys:
|
|
69
96
|
setattr(self.obj, self.attr, self.value)
|
|
70
97
|
|
|
98
|
+
class ScheduledCaller:
|
|
99
|
+
def __init__(self, n_times, callback, delay, caller_id):
|
|
100
|
+
self.n_times = n_times
|
|
101
|
+
self.callback = callback
|
|
102
|
+
self.delay = delay
|
|
103
|
+
self.caller_id = caller_id
|
|
104
|
+
self.finished = False
|
|
105
|
+
self.delay_obj = Delay(self.delay, self.callback)
|
|
106
|
+
|
|
107
|
+
def update(self):
|
|
108
|
+
self.delay_obj.update()
|
|
109
|
+
if self.delay_obj.finished:
|
|
110
|
+
self.n_times -= 1
|
|
111
|
+
self.delay_obj = Delay(self.delay, self.callback)
|
|
112
|
+
|
|
113
|
+
self.finished = self.n_times < 0
|
|
114
|
+
|
|
71
115
|
class Context:
|
|
72
116
|
def __init__(self):
|
|
73
117
|
self.functions: Functions = Functions()
|
|
@@ -86,6 +130,9 @@ class Context:
|
|
|
86
130
|
self.keys_released: list[str] = []
|
|
87
131
|
self.key_callbacks: list[KeyCallback] = []
|
|
88
132
|
self.key_callback_vars: list[KeyCallbackVar] = []
|
|
133
|
+
self.delays: list[Delay] = []
|
|
134
|
+
self.delay_vars: list[DelayVar] = []
|
|
135
|
+
self.scheduled_callers: list[ScheduledCaller] = []
|
|
89
136
|
self.exit = False
|
|
90
137
|
|
|
91
138
|
def on_key_press(self, key, callback, args=None, dt=False):
|
|
@@ -117,4 +164,36 @@ class Context:
|
|
|
117
164
|
callback.update(self.keys_pressed, self.keys_held, self.keys_released, dt)
|
|
118
165
|
|
|
119
166
|
for callback_var in self.key_callback_vars:
|
|
120
|
-
callback_var.update(self.keys_pressed, self.keys_held, self.keys_released
|
|
167
|
+
callback_var.update(self.keys_pressed, self.keys_held, self.keys_released)
|
|
168
|
+
|
|
169
|
+
delays = self.delays[:]
|
|
170
|
+
for delay in delays:
|
|
171
|
+
delay.update()
|
|
172
|
+
if delay.finished:
|
|
173
|
+
self.delays.remove(delay)
|
|
174
|
+
|
|
175
|
+
delay_vars = self.delay_vars[:]
|
|
176
|
+
for delay in delay_vars:
|
|
177
|
+
delay.update()
|
|
178
|
+
if delay.finished:
|
|
179
|
+
self.delay_vars.remove(delay)
|
|
180
|
+
|
|
181
|
+
scheduled_callers = self.scheduled_callers[:]
|
|
182
|
+
for caller in scheduled_callers:
|
|
183
|
+
caller.update()
|
|
184
|
+
if caller.finished:
|
|
185
|
+
self.scheduled_callers.remove(caller)
|
|
186
|
+
|
|
187
|
+
def add_delay(self, delay, callback):
|
|
188
|
+
self.delays.append(Delay(delay, callback))
|
|
189
|
+
|
|
190
|
+
def add_delay_var(self, delay, obj, attr, value):
|
|
191
|
+
self.delay_vars.append(DelayVar(delay, obj, attr, value))
|
|
192
|
+
|
|
193
|
+
def add_scheduled_caller(self, n_times, delay, callback, caller_id=None):
|
|
194
|
+
if caller_id is None:
|
|
195
|
+
max_id = -1
|
|
196
|
+
if self.scheduled_callers:
|
|
197
|
+
max_id = max([caller.caller_id for caller in self.scheduled_callers])
|
|
198
|
+
caller_id = max_id + 1
|
|
199
|
+
self.scheduled_callers.append(ScheduledCaller(n_times, callback, delay, caller_id))
|
|
@@ -227,11 +227,13 @@ class Light:
|
|
|
227
227
|
self.show = show
|
|
228
228
|
self.name = name
|
|
229
229
|
|
|
230
|
+
|
|
230
231
|
class Renderer:
|
|
231
232
|
def __init__(self, width=800, height=600, flags=DOUBLEBUF | OPENGL):
|
|
232
233
|
pygame.init()
|
|
233
234
|
self.screen = pygame.display.set_mode((width, height), flags)
|
|
234
235
|
self.ctx = moderngl.create_context()
|
|
236
|
+
self.ctx.viewport = (0, 0, width, height)
|
|
235
237
|
self.samples = 4
|
|
236
238
|
self.color_tex = self.ctx.texture((width, height), 3, samples=self.samples)
|
|
237
239
|
self.depth_rbo = self.ctx.depth_renderbuffer((width, height), samples=self.samples)
|
|
@@ -241,6 +243,8 @@ class Renderer:
|
|
|
241
243
|
self.height = height
|
|
242
244
|
self.ambient = 0.2
|
|
243
245
|
self.ctx.enable(moderngl.DEPTH_TEST)
|
|
246
|
+
|
|
247
|
+
# === Main shader ===
|
|
244
248
|
self.prog = self._load_shader("default.vert", "default.frag")
|
|
245
249
|
self.prog["model"].write(np.eye(4, dtype="f4").tobytes())
|
|
246
250
|
self.proj = Matrix44.perspective_projection(60.0, width / height, 0.1, 100.0)
|
|
@@ -252,6 +256,22 @@ class Renderer:
|
|
|
252
256
|
self.mvp = self.proj * self.view
|
|
253
257
|
self.prog["mvp"].write(self.mvp.astype("f4").tobytes())
|
|
254
258
|
self.prog["ambient"].value = self.ambient
|
|
259
|
+
|
|
260
|
+
# === Shadow map setup ===
|
|
261
|
+
self.shadow_size = 2048
|
|
262
|
+
self.shadow_depth_tex = self.ctx.depth_texture((self.shadow_size, self.shadow_size))
|
|
263
|
+
try:
|
|
264
|
+
self.shadow_depth_tex.repeat_x = False
|
|
265
|
+
self.shadow_depth_tex.repeat_y = False
|
|
266
|
+
except Exception:
|
|
267
|
+
pass
|
|
268
|
+
self.shadow_fbo = self.ctx.framebuffer(depth_attachment=self.shadow_depth_tex)
|
|
269
|
+
self.depth_prog = self._load_shader("depth.vert", "depth.frag")
|
|
270
|
+
self.light_space_matrix = np.eye(4, dtype="f4")
|
|
271
|
+
|
|
272
|
+
# ------------------------------------------
|
|
273
|
+
# Window / Resize Handling
|
|
274
|
+
# ------------------------------------------
|
|
255
275
|
def resize(self, width, height):
|
|
256
276
|
self.width = width
|
|
257
277
|
self.height = height
|
|
@@ -262,26 +282,93 @@ class Renderer:
|
|
|
262
282
|
|
|
263
283
|
self.ctx = moderngl.create_context()
|
|
264
284
|
self.ctx.viewport = (0, 0, width, height)
|
|
285
|
+
|
|
286
|
+
# Recreate framebuffer and textures for new context
|
|
265
287
|
self.color_tex = self.ctx.texture((width, height), 3, samples=self.samples)
|
|
266
288
|
self.depth_rbo = self.ctx.depth_renderbuffer((width, height), samples=self.samples)
|
|
267
289
|
self.fbo = self.ctx.framebuffer(color_attachments=[self.color_tex], depth_attachment=self.depth_rbo)
|
|
268
290
|
|
|
291
|
+
# Recreate shaders in new context
|
|
269
292
|
self.prog = self._load_shader("default.vert", "default.frag")
|
|
270
293
|
self.prog["model"].write(np.eye(4, dtype="f4").tobytes())
|
|
271
|
-
|
|
272
294
|
aspect = width / height
|
|
273
295
|
self.proj = Matrix44.perspective_projection(60.0, aspect, 0.1, 100.0)
|
|
274
296
|
self.mvp = self.proj * self.view
|
|
275
297
|
self.prog["mvp"].write(self.mvp.astype("f4").tobytes())
|
|
276
298
|
self.prog["ambient"].value = self.ambient
|
|
277
299
|
|
|
300
|
+
# Recreate shadow map resources for this context
|
|
301
|
+
self.shadow_depth_tex = self.ctx.depth_texture((self.shadow_size, self.shadow_size))
|
|
302
|
+
try:
|
|
303
|
+
self.shadow_depth_tex.repeat_x = False
|
|
304
|
+
self.shadow_depth_tex.repeat_y = False
|
|
305
|
+
except Exception:
|
|
306
|
+
pass
|
|
307
|
+
self.shadow_fbo = self.ctx.framebuffer(depth_attachment=self.shadow_depth_tex)
|
|
308
|
+
self.depth_prog = self._load_shader("depth.vert", "depth.frag")
|
|
309
|
+
|
|
278
310
|
self._send_lights()
|
|
311
|
+
|
|
312
|
+
# ------------------------------------------
|
|
313
|
+
# Light & Matrix
|
|
314
|
+
# ------------------------------------------
|
|
279
315
|
def add_light(self, light: Light):
|
|
280
316
|
self.lights.append(light)
|
|
317
|
+
|
|
281
318
|
def _compute_normal(self, p1, p2, p3):
|
|
282
319
|
u = p2 - p1
|
|
283
320
|
v = p3 - p1
|
|
284
321
|
return u.cross(v).normalized
|
|
322
|
+
|
|
323
|
+
def compute_light_matrix(self, light: Light):
|
|
324
|
+
"""Compute light-space projection * view for shadow mapping."""
|
|
325
|
+
light_proj = Matrix44.orthogonal_projection(-10, 10, -10, 10, 1, 30)
|
|
326
|
+
light_view = Matrix44.look_at(
|
|
327
|
+
eye=light.position.totuple(),
|
|
328
|
+
target=(0, 0, 0),
|
|
329
|
+
up=(0, 1, 0)
|
|
330
|
+
)
|
|
331
|
+
return light_proj * light_view
|
|
332
|
+
|
|
333
|
+
# ------------------------------------------
|
|
334
|
+
# Shadow Pass
|
|
335
|
+
# ------------------------------------------
|
|
336
|
+
def render_shadow_pass(self, objects):
|
|
337
|
+
"""Render depth map from light's POV."""
|
|
338
|
+
if not self.lights:
|
|
339
|
+
return
|
|
340
|
+
light = self.lights[0]
|
|
341
|
+
self.light_space_matrix = self.compute_light_matrix(light)
|
|
342
|
+
|
|
343
|
+
self.shadow_fbo.clear(depth=1.0)
|
|
344
|
+
self.shadow_fbo.use()
|
|
345
|
+
self.ctx.enable(moderngl.DEPTH_TEST)
|
|
346
|
+
|
|
347
|
+
for obj in objects:
|
|
348
|
+
if not hasattr(obj, "get_corners"):
|
|
349
|
+
continue
|
|
350
|
+
corners = obj.get_corners()
|
|
351
|
+
vertices = []
|
|
352
|
+
for face in obj.faces:
|
|
353
|
+
p1, p2, p3, p4 = [corners[i] for i in face]
|
|
354
|
+
vertices.extend([
|
|
355
|
+
*p1.totuple(), *p2.totuple(), *p3.totuple(),
|
|
356
|
+
*p1.totuple(), *p3.totuple(), *p4.totuple()
|
|
357
|
+
])
|
|
358
|
+
vertices = np.array(vertices, dtype="f4")
|
|
359
|
+
vbo = self.ctx.buffer(vertices.tobytes())
|
|
360
|
+
vao = self.ctx.vertex_array(self.depth_prog, [(vbo, "3f", "in_vert")])
|
|
361
|
+
self.depth_prog["model"].write(np.eye(4, dtype="f4").tobytes())
|
|
362
|
+
self.depth_prog["light_space_matrix"].write(self.light_space_matrix.astype("f4").tobytes())
|
|
363
|
+
vao.render()
|
|
364
|
+
vao.release()
|
|
365
|
+
vbo.release()
|
|
366
|
+
|
|
367
|
+
self.ctx.screen.use()
|
|
368
|
+
|
|
369
|
+
# ------------------------------------------
|
|
370
|
+
# Render Functions
|
|
371
|
+
# ------------------------------------------
|
|
285
372
|
def render_plane(self, p1, p2, p3, color):
|
|
286
373
|
normal = self._compute_normal(p1, p2, p3)
|
|
287
374
|
vertices = np.array([
|
|
@@ -291,13 +378,16 @@ class Renderer:
|
|
|
291
378
|
], dtype="f4")
|
|
292
379
|
vbo = self.ctx.buffer(vertices.tobytes())
|
|
293
380
|
vao = self.ctx.vertex_array(
|
|
294
|
-
self.prog,
|
|
295
|
-
[(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
|
|
381
|
+
self.prog, [(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
|
|
296
382
|
)
|
|
297
383
|
self._send_lights()
|
|
384
|
+
self.prog["light_space_matrix"].write(self.light_space_matrix.astype("f4").tobytes())
|
|
385
|
+
self.shadow_depth_tex.use(location=0)
|
|
386
|
+
self.prog["shadow_map"].value = 0
|
|
298
387
|
vao.render()
|
|
299
388
|
vao.release()
|
|
300
389
|
vbo.release()
|
|
390
|
+
|
|
301
391
|
def render_quad(self, p1, p2, p3, p4, color):
|
|
302
392
|
normal = self._compute_normal(p1, p2, p3)
|
|
303
393
|
vertices = np.array([
|
|
@@ -310,42 +400,36 @@ class Renderer:
|
|
|
310
400
|
], dtype="f4")
|
|
311
401
|
vbo = self.ctx.buffer(vertices.tobytes())
|
|
312
402
|
vao = self.ctx.vertex_array(
|
|
313
|
-
self.prog,
|
|
314
|
-
[(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
|
|
403
|
+
self.prog, [(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
|
|
315
404
|
)
|
|
316
405
|
self._send_lights()
|
|
406
|
+
self.prog["light_space_matrix"].write(self.light_space_matrix.astype("f4").tobytes())
|
|
407
|
+
self.shadow_depth_tex.use(location=0)
|
|
408
|
+
self.prog["shadow_map"].value = 0
|
|
317
409
|
vao.render()
|
|
318
410
|
vao.release()
|
|
319
411
|
vbo.release()
|
|
412
|
+
|
|
320
413
|
def render_sphere(self, center, radius, color, segments=32, rings=16):
|
|
321
414
|
vertices = []
|
|
322
|
-
normals = []
|
|
323
415
|
for i in range(rings):
|
|
324
416
|
theta1 = math.pi * i / rings
|
|
325
417
|
theta2 = math.pi * (i + 1) / rings
|
|
326
418
|
for j in range(segments):
|
|
327
419
|
phi1 = 2 * math.pi * j / segments
|
|
328
420
|
phi2 = 2 * math.pi * (j + 1) / segments
|
|
329
|
-
p1 = vector3d(
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
)
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
radius * math.cos(theta2),
|
|
342
|
-
radius * math.sin(theta2) * math.sin(phi2)
|
|
343
|
-
) + center
|
|
344
|
-
p4 = vector3d(
|
|
345
|
-
radius * math.sin(theta1) * math.cos(phi2),
|
|
346
|
-
radius * math.cos(theta1),
|
|
347
|
-
radius * math.sin(theta1) * math.sin(phi2)
|
|
348
|
-
) + center
|
|
421
|
+
p1 = vector3d(radius * math.sin(theta1) * math.cos(phi1),
|
|
422
|
+
radius * math.cos(theta1),
|
|
423
|
+
radius * math.sin(theta1) * math.sin(phi1)) + center
|
|
424
|
+
p2 = vector3d(radius * math.sin(theta2) * math.cos(phi1),
|
|
425
|
+
radius * math.cos(theta2),
|
|
426
|
+
radius * math.sin(theta2) * math.sin(phi1)) + center
|
|
427
|
+
p3 = vector3d(radius * math.sin(theta2) * math.cos(phi2),
|
|
428
|
+
radius * math.cos(theta2),
|
|
429
|
+
radius * math.sin(theta2) * math.sin(phi2)) + center
|
|
430
|
+
p4 = vector3d(radius * math.sin(theta1) * math.cos(phi2),
|
|
431
|
+
radius * math.cos(theta1),
|
|
432
|
+
radius * math.sin(theta1) * math.sin(phi2)) + center
|
|
349
433
|
for tri in [(p1, p2, p3), (p1, p3, p4)]:
|
|
350
434
|
n1 = (tri[0] - center).normalized
|
|
351
435
|
n2 = (tri[1] - center).normalized
|
|
@@ -358,88 +442,19 @@ class Renderer:
|
|
|
358
442
|
vertices = np.array(vertices, dtype="f4")
|
|
359
443
|
vbo = self.ctx.buffer(vertices.tobytes())
|
|
360
444
|
vao = self.ctx.vertex_array(
|
|
361
|
-
self.prog,
|
|
362
|
-
[(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
|
|
363
|
-
)
|
|
364
|
-
self._send_lights()
|
|
365
|
-
vao.render()
|
|
366
|
-
vao.release()
|
|
367
|
-
vbo.release()
|
|
368
|
-
def render_cylinder(self, center, height, radius, color, segments=32, rotation=vector3d.zero):
|
|
369
|
-
vertices = []
|
|
370
|
-
half_h = height / 2.0
|
|
371
|
-
rx, ry, rz = map(math.radians, (rotation.x, rotation.y, rotation.z))
|
|
372
|
-
rot_x = np.array([
|
|
373
|
-
[1, 0, 0],
|
|
374
|
-
[0, math.cos(rx), -math.sin(rx)],
|
|
375
|
-
[0, math.sin(rx), math.cos(rx)]
|
|
376
|
-
], dtype="f4")
|
|
377
|
-
rot_y = np.array([
|
|
378
|
-
[ math.cos(ry), 0, math.sin(ry)],
|
|
379
|
-
[0, 1, 0],
|
|
380
|
-
[-math.sin(ry), 0, math.cos(ry)]
|
|
381
|
-
], dtype="f4")
|
|
382
|
-
rot_z = np.array([
|
|
383
|
-
[math.cos(rz), -math.sin(rz), 0],
|
|
384
|
-
[math.sin(rz), math.cos(rz), 0],
|
|
385
|
-
[0, 0, 1]
|
|
386
|
-
], dtype="f4")
|
|
387
|
-
# Combined rotation matrix (Z * Y * X)
|
|
388
|
-
rotation_matrix = rot_z @ rot_y @ rot_x
|
|
389
|
-
def apply_rotation(v: vector3d):
|
|
390
|
-
vec = np.array([[v.x], [v.y], [v.z]], dtype="f4")
|
|
391
|
-
rotated = rotation_matrix @ vec
|
|
392
|
-
return vector3d(rotated[0][0], rotated[1][0], rotated[2][0])
|
|
393
|
-
for i in range(segments):
|
|
394
|
-
theta1 = 2 * math.pi * i / segments
|
|
395
|
-
theta2 = 2 * math.pi * (i + 1) / segments
|
|
396
|
-
x1, z1 = radius * math.cos(theta1), radius * math.sin(theta1)
|
|
397
|
-
x2, z2 = radius * math.cos(theta2), radius * math.sin(theta2)
|
|
398
|
-
# Cylinder face vertices
|
|
399
|
-
p1 = vector3d(x1, -half_h, z1)
|
|
400
|
-
p2 = vector3d(x2, -half_h, z2)
|
|
401
|
-
p3 = vector3d(x2, half_h, z2)
|
|
402
|
-
p4 = vector3d(x1, half_h, z1)
|
|
403
|
-
# Apply rotation
|
|
404
|
-
p1 = apply_rotation(p1) + center
|
|
405
|
-
p2 = apply_rotation(p2) + center
|
|
406
|
-
p3 = apply_rotation(p3) + center
|
|
407
|
-
p4 = apply_rotation(p4) + center
|
|
408
|
-
n1 = apply_rotation(vector3d(x1, 0, z1).normalized)
|
|
409
|
-
n2 = apply_rotation(vector3d(x2, 0, z2).normalized)
|
|
410
|
-
# Side faces
|
|
411
|
-
vertices.extend([
|
|
412
|
-
*p1.totuple(), *color.to_rgb(), *n1.totuple(),
|
|
413
|
-
*p2.totuple(), *color.to_rgb(), *n2.totuple(),
|
|
414
|
-
*p3.totuple(), *color.to_rgb(), *n2.totuple(),
|
|
415
|
-
*p1.totuple(), *color.to_rgb(), *n1.totuple(),
|
|
416
|
-
*p3.totuple(), *color.to_rgb(), *n2.totuple(),
|
|
417
|
-
*p4.totuple(), *color.to_rgb(), *n1.totuple(),
|
|
418
|
-
])
|
|
419
|
-
# Base
|
|
420
|
-
base_normal = apply_rotation(vector3d(0, -1, 0))
|
|
421
|
-
vertices.extend([
|
|
422
|
-
*apply_rotation(vector3d(0, -half_h, 0) + center).totuple(), *color.to_rgb(), *base_normal.totuple(),
|
|
423
|
-
*p2.totuple(), *color.to_rgb(), *base_normal.totuple(),
|
|
424
|
-
*p1.totuple(), *color.to_rgb(), *base_normal.totuple(),
|
|
425
|
-
])
|
|
426
|
-
# Top
|
|
427
|
-
top_normal = apply_rotation(vector3d(0, 1, 0))
|
|
428
|
-
vertices.extend([
|
|
429
|
-
*apply_rotation(vector3d(0, half_h, 0) + center).totuple(), *color.to_rgb(), *top_normal.totuple(),
|
|
430
|
-
*p4.totuple(), *color.to_rgb(), *top_normal.totuple(),
|
|
431
|
-
*p3.totuple(), *color.to_rgb(), *top_normal.totuple(),
|
|
432
|
-
])
|
|
433
|
-
vertices = np.array(vertices, dtype="f4")
|
|
434
|
-
vbo = self.ctx.buffer(vertices.tobytes())
|
|
435
|
-
vao = self.ctx.vertex_array(
|
|
436
|
-
self.prog,
|
|
437
|
-
[(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
|
|
445
|
+
self.prog, [(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
|
|
438
446
|
)
|
|
439
447
|
self._send_lights()
|
|
448
|
+
self.prog["light_space_matrix"].write(self.light_space_matrix.astype("f4").tobytes())
|
|
449
|
+
self.shadow_depth_tex.use(location=0)
|
|
450
|
+
self.prog["shadow_map"].value = 0
|
|
440
451
|
vao.render()
|
|
441
452
|
vao.release()
|
|
442
453
|
vbo.release()
|
|
454
|
+
|
|
455
|
+
# ------------------------------------------
|
|
456
|
+
# Utility
|
|
457
|
+
# ------------------------------------------
|
|
443
458
|
def _send_lights(self):
|
|
444
459
|
max_lights = 8
|
|
445
460
|
num_lights = min(len(self.lights), max_lights)
|
|
@@ -450,18 +465,22 @@ class Renderer:
|
|
|
450
465
|
self.prog[f"lights[{i}].position"].value = light.position.totuple()
|
|
451
466
|
self.prog[f"lights[{i}].color"].value = tuple(c / 255 for c in light.color.to_rgb())
|
|
452
467
|
self.prog[f"lights[{i}].intensity"].value = light.intensity
|
|
468
|
+
|
|
453
469
|
def clear(self, color=Color(26, 26, 31)):
|
|
454
470
|
color_f = np.array(color.to_rgb(), dtype="f4") / 255.0
|
|
455
471
|
self.fbo.clear(*color_f, depth=1.0)
|
|
456
472
|
self.fbo.use()
|
|
473
|
+
|
|
457
474
|
def swap(self):
|
|
458
475
|
self.ctx.copy_framebuffer(dst=self.ctx.screen, src=self.fbo)
|
|
459
476
|
pygame.display.flip()
|
|
477
|
+
|
|
460
478
|
def quit(self):
|
|
461
479
|
pygame.quit()
|
|
480
|
+
|
|
462
481
|
def _load_shader(self, vert_name, frag_name):
|
|
463
|
-
vert_src = pkg_resources.read_text(f"{__package__}.shaders",
|
|
464
|
-
frag_src = pkg_resources.read_text(f"{__package__}.shaders",
|
|
482
|
+
vert_src = pkg_resources.read_text(f"{__package__}.shaders", vert_name)
|
|
483
|
+
frag_src = pkg_resources.read_text(f"{__package__}.shaders", frag_name)
|
|
465
484
|
return self.ctx.program(vertex_shader=vert_src, fragment_shader=frag_src)
|
|
466
485
|
|
|
467
486
|
class Camera:
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
|
|
2
|
+
#version 330
|
|
3
|
+
|
|
4
|
+
struct Light {
|
|
5
|
+
vec3 position;
|
|
6
|
+
vec3 color;
|
|
7
|
+
float intensity;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
#define MAX_LIGHTS 8
|
|
11
|
+
uniform int num_lights;
|
|
12
|
+
uniform Light lights[MAX_LIGHTS];
|
|
13
|
+
uniform float ambient;
|
|
14
|
+
uniform sampler2DShadow shadow_map; // NOTE: sampler2DShadow
|
|
15
|
+
|
|
16
|
+
in vec3 frag_pos;
|
|
17
|
+
in vec3 frag_normal;
|
|
18
|
+
in vec3 frag_color;
|
|
19
|
+
in vec4 frag_pos_light_space;
|
|
20
|
+
|
|
21
|
+
out vec4 f_color;
|
|
22
|
+
|
|
23
|
+
void main() {
|
|
24
|
+
vec3 normal = normalize(frag_normal);
|
|
25
|
+
vec3 color = frag_color / 255.0;
|
|
26
|
+
vec3 result = ambient * color;
|
|
27
|
+
|
|
28
|
+
for (int i = 0; i < num_lights; i++) {
|
|
29
|
+
vec3 light_dir = normalize(lights[i].position - frag_pos);
|
|
30
|
+
float diff = max(dot(normal, light_dir), 0.0);
|
|
31
|
+
|
|
32
|
+
// Project to [0,1] for shadow sampling
|
|
33
|
+
vec3 proj_coords = frag_pos_light_space.xyz / frag_pos_light_space.w;
|
|
34
|
+
proj_coords = proj_coords * 0.5 + 0.5;
|
|
35
|
+
|
|
36
|
+
// Hardware PCF shadow sampling
|
|
37
|
+
float shadow = 0.0;
|
|
38
|
+
if (proj_coords.x >= 0.0 && proj_coords.x <= 1.0 &&
|
|
39
|
+
proj_coords.y >= 0.0 && proj_coords.y <= 1.0) {
|
|
40
|
+
|
|
41
|
+
float bias = max(0.02 * (1.0 - dot(normal, light_dir)), 0.005);
|
|
42
|
+
|
|
43
|
+
// Simple 3x3 PCF
|
|
44
|
+
ivec2 texSize = textureSize(shadow_map, 0);
|
|
45
|
+
float texelSizeX = 1.0 / float(texSize.x);
|
|
46
|
+
float texelSizeY = 1.0 / float(texSize.y);
|
|
47
|
+
|
|
48
|
+
for (int x = -1; x <= 1; ++x) {
|
|
49
|
+
for (int y = -1; y <= 1; ++y) {
|
|
50
|
+
vec2 offset = vec2(float(x) * texelSizeX, float(y) * texelSizeY);
|
|
51
|
+
shadow += texture(shadow_map, vec3(proj_coords.xy + offset, proj_coords.z - bias));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
shadow /= 9.0;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
// No inversion needed
|
|
59
|
+
result += shadow * diff * lights[i].intensity * lights[i].color * color;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
f_color = vec4(result, 1.0);
|
|
63
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
|
|
2
|
+
#version 330
|
|
3
|
+
|
|
4
|
+
in vec3 in_vert;
|
|
5
|
+
in vec3 in_color;
|
|
6
|
+
in vec3 in_normal;
|
|
7
|
+
|
|
8
|
+
out vec3 frag_pos;
|
|
9
|
+
out vec3 frag_normal;
|
|
10
|
+
out vec3 frag_color;
|
|
11
|
+
out vec4 frag_pos_light_space;
|
|
12
|
+
|
|
13
|
+
uniform mat4 mvp;
|
|
14
|
+
uniform mat4 model;
|
|
15
|
+
uniform mat4 light_space_matrix;
|
|
16
|
+
|
|
17
|
+
void main() {
|
|
18
|
+
vec4 world_pos = model * vec4(in_vert, 1.0);
|
|
19
|
+
frag_pos = world_pos.xyz;
|
|
20
|
+
frag_normal = mat3(transpose(inverse(model))) * in_normal;
|
|
21
|
+
frag_color = in_color;
|
|
22
|
+
frag_pos_light_space = light_space_matrix * world_pos;
|
|
23
|
+
gl_Position = mvp * vec4(in_vert, 1.0);
|
|
24
|
+
}
|
|
@@ -13,4 +13,6 @@ gamengine3d.egg-info/requires.txt
|
|
|
13
13
|
gamengine3d.egg-info/top_level.txt
|
|
14
14
|
gamengine3d/shaders/__init__.py
|
|
15
15
|
gamengine3d/shaders/default.frag
|
|
16
|
-
gamengine3d/shaders/default.vert
|
|
16
|
+
gamengine3d/shaders/default.vert
|
|
17
|
+
gamengine3d/shaders/depth.frag
|
|
18
|
+
gamengine3d/shaders/depth.vert
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
#version 330
|
|
3
|
-
|
|
4
|
-
struct Light {
|
|
5
|
-
vec3 position;
|
|
6
|
-
vec3 color;
|
|
7
|
-
float intensity;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
#define MAX_LIGHTS 8
|
|
11
|
-
uniform int num_lights;
|
|
12
|
-
uniform Light lights[MAX_LIGHTS];
|
|
13
|
-
uniform float ambient;
|
|
14
|
-
|
|
15
|
-
in vec3 frag_pos;
|
|
16
|
-
in vec3 frag_normal;
|
|
17
|
-
in vec3 frag_color;
|
|
18
|
-
|
|
19
|
-
out vec4 f_color;
|
|
20
|
-
|
|
21
|
-
void main() {
|
|
22
|
-
vec3 normal = normalize(frag_normal);
|
|
23
|
-
vec3 color = frag_color / 255.0; // <-- convert vertex color to 0-1
|
|
24
|
-
vec3 result = ambient * color;
|
|
25
|
-
|
|
26
|
-
for (int i = 0; i < num_lights; i++) {
|
|
27
|
-
vec3 light_dir = normalize(lights[i].position - frag_pos);
|
|
28
|
-
float diff = max(dot(normal, light_dir), 0.0);
|
|
29
|
-
result += diff * lights[i].intensity * lights[i].color * color;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
f_color = vec4(result, 1.0);
|
|
33
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
#version 330
|
|
3
|
-
|
|
4
|
-
in vec3 in_vert;
|
|
5
|
-
in vec3 in_color;
|
|
6
|
-
in vec3 in_normal;
|
|
7
|
-
|
|
8
|
-
out vec3 frag_pos;
|
|
9
|
-
out vec3 frag_normal;
|
|
10
|
-
out vec3 frag_color;
|
|
11
|
-
|
|
12
|
-
uniform mat4 mvp;
|
|
13
|
-
uniform mat4 model; // optional if you want model transforms
|
|
14
|
-
|
|
15
|
-
void main() {
|
|
16
|
-
frag_pos = vec3(model * vec4(in_vert, 1.0)); // world space position
|
|
17
|
-
frag_normal = mat3(transpose(inverse(model))) * in_normal; // transform normal
|
|
18
|
-
frag_color = in_color;
|
|
19
|
-
gl_Position = mvp * vec4(in_vert, 1.0);
|
|
20
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|