gamengine3d 1.12.0__tar.gz → 1.13.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.12.0 → gamengine3d-1.13.0}/PKG-INFO +1 -1
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/engine.py +2 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/examples/scripts/player_movement.py +0 -1
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/helper.py +131 -148
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d.egg-info/PKG-INFO +1 -1
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/pyproject.toml +1 -1
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/LICENSE.txt +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/README.md +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/__init__.py +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/cli.py +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/context.py +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/examples/__init__.py +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/examples/basics.py +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/examples/from_obj.py +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/examples/load_scene.py +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/examples/messages_demo.py +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/examples/objects/__init__.py +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/examples/objects/icosahedron.obj +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/examples/player_movement.py +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/examples/save_scene.py +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/examples/scripts/cylinder.py +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/examples/scripts/message_receiver.py +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/examples/scripts/messanger_script.py +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/examples/scripts/settings.py +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/examples/the_everything_demo.py +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/game_objects.py +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/obj_reader.py +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/shaders/__init__.py +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/shaders/default.frag +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/shaders/default.vert +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/shaders/depth.frag +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d/shaders/depth.vert +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d.egg-info/SOURCES.txt +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d.egg-info/dependency_links.txt +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d.egg-info/entry_points.txt +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d.egg-info/requires.txt +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/gamengine3d.egg-info/top_level.txt +0 -0
- {gamengine3d-1.12.0 → gamengine3d-1.13.0}/setup.cfg +0 -0
|
@@ -78,6 +78,7 @@ class Engine:
|
|
|
78
78
|
self.mouse_sensitivity = self.context.mouse_sensitivity
|
|
79
79
|
self.pan_sensitivity = self.context.pan_sensitivity
|
|
80
80
|
self.context.fps = self.clock.get_fps()
|
|
81
|
+
print(self.context.fps)
|
|
81
82
|
|
|
82
83
|
for event in pygame.event.get():
|
|
83
84
|
if event.type == pygame.QUIT:
|
|
@@ -150,6 +151,7 @@ class Engine:
|
|
|
150
151
|
if self.context.exit:
|
|
151
152
|
running = False
|
|
152
153
|
|
|
154
|
+
self.renderer.flush()
|
|
153
155
|
self.renderer.swap()
|
|
154
156
|
self.clock.tick(fps)
|
|
155
157
|
|
|
@@ -262,6 +262,7 @@ class Renderer:
|
|
|
262
262
|
self.height = height
|
|
263
263
|
self.ambient = 0.2
|
|
264
264
|
self.ctx.enable(moderngl.DEPTH_TEST)
|
|
265
|
+
self.MAX_VERTICES = 500_000 # adjust based on your scene complexity
|
|
265
266
|
|
|
266
267
|
# === Main shader ===
|
|
267
268
|
self.prog = self._load_shader("default.vert", "default.frag")
|
|
@@ -287,44 +288,45 @@ class Renderer:
|
|
|
287
288
|
self.depth_prog = self._load_shader("depth.vert", "depth.frag")
|
|
288
289
|
self.light_space_matrix = np.eye(4, dtype="f4")
|
|
289
290
|
|
|
291
|
+
# === Frame batching ===
|
|
292
|
+
self.frame_vertices = []
|
|
293
|
+
self.vbo = self.ctx.buffer(reserve=self.MAX_VERTICES * 4 * 9)
|
|
294
|
+
# 4 bytes per float, 9 floats per vertex (pos 3 + color 3 + normal 3)
|
|
295
|
+
|
|
296
|
+
# create a persistent VAO once
|
|
297
|
+
self.vao = self.ctx.vertex_array(
|
|
298
|
+
self.prog,
|
|
299
|
+
[(self.vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
|
|
290
303
|
def resize(self, width, height):
|
|
291
304
|
self.width = width
|
|
292
305
|
self.height = height
|
|
293
|
-
pygame.display.set_mode(
|
|
294
|
-
(width, height),
|
|
295
|
-
pygame.OPENGL | pygame.DOUBLEBUF | pygame.RESIZABLE
|
|
296
|
-
)
|
|
306
|
+
pygame.display.set_mode((width, height), pygame.OPENGL | pygame.DOUBLEBUF | pygame.RESIZABLE)
|
|
297
307
|
|
|
298
|
-
|
|
308
|
+
# just update viewport, don’t create a new context
|
|
299
309
|
self.ctx.viewport = (0, 0, width, height)
|
|
300
310
|
|
|
301
|
-
#
|
|
302
|
-
self.color_tex = self.ctx.texture((width, height), 3, samples=self.samples)
|
|
303
|
-
self.depth_rbo = self.ctx.depth_renderbuffer((width, height), samples=self.samples)
|
|
304
|
-
self.fbo = self.ctx.framebuffer(color_attachments=[self.color_tex], depth_attachment=self.depth_rbo)
|
|
305
|
-
|
|
306
|
-
# Recreate shaders in new context
|
|
307
|
-
self.prog = self._load_shader("default.vert", "default.frag")
|
|
308
|
-
self.prog["model"].write(np.eye(4, dtype="f4").tobytes())
|
|
311
|
+
# update projection matrix
|
|
309
312
|
aspect = width / height
|
|
310
313
|
self.proj = Matrix44.perspective_projection(60.0, aspect, 0.1, 100.0)
|
|
311
314
|
self.mvp = self.proj * self.view
|
|
312
315
|
self.prog["mvp"].write(self.mvp.astype("f4").tobytes())
|
|
313
|
-
self.prog["ambient"].value = self.ambient
|
|
314
316
|
|
|
315
|
-
#
|
|
316
|
-
self.
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
self.
|
|
323
|
-
self.
|
|
317
|
+
# resize textures and framebuffers
|
|
318
|
+
self.color_tex.release()
|
|
319
|
+
self.color_tex = self.ctx.texture((width, height), 3, samples=self.samples)
|
|
320
|
+
|
|
321
|
+
self.depth_rbo.release()
|
|
322
|
+
self.depth_rbo = self.ctx.depth_renderbuffer((width, height), samples=self.samples)
|
|
323
|
+
|
|
324
|
+
self.fbo.release()
|
|
325
|
+
self.fbo = self.ctx.framebuffer(color_attachments=[self.color_tex], depth_attachment=self.depth_rbo)
|
|
324
326
|
|
|
325
327
|
self._send_lights()
|
|
326
328
|
|
|
327
|
-
def add_light(self, light
|
|
329
|
+
def add_light(self, light):
|
|
328
330
|
self.lights.append(light)
|
|
329
331
|
|
|
330
332
|
def _compute_normal(self, p1, p2, p3):
|
|
@@ -332,8 +334,7 @@ class Renderer:
|
|
|
332
334
|
v = p3 - p1
|
|
333
335
|
return u.cross(v).normalized
|
|
334
336
|
|
|
335
|
-
def compute_light_matrix(self, light
|
|
336
|
-
"""Compute light-space projection * view for shadow mapping."""
|
|
337
|
+
def compute_light_matrix(self, light):
|
|
337
338
|
light_proj = Matrix44.orthogonal_projection(-20, 20, -20, 20, 1, 30)
|
|
338
339
|
light_view = Matrix44.look_at(
|
|
339
340
|
eye=light.position.totuple(),
|
|
@@ -342,58 +343,6 @@ class Renderer:
|
|
|
342
343
|
)
|
|
343
344
|
return light_proj * light_view
|
|
344
345
|
|
|
345
|
-
def render_shadow_pass(self, objects):
|
|
346
|
-
"""Render depth map from light's POV."""
|
|
347
|
-
if not self.lights:
|
|
348
|
-
return
|
|
349
|
-
light = self.lights[0]
|
|
350
|
-
self.light_space_matrix = self.compute_light_matrix(light)
|
|
351
|
-
|
|
352
|
-
self.shadow_fbo.clear(depth=1.0)
|
|
353
|
-
self.shadow_fbo.use()
|
|
354
|
-
self.ctx.enable(moderngl.DEPTH_TEST)
|
|
355
|
-
|
|
356
|
-
for obj in objects:
|
|
357
|
-
if not hasattr(obj, "get_corners"):
|
|
358
|
-
continue
|
|
359
|
-
corners = obj.get_corners()
|
|
360
|
-
vertices = []
|
|
361
|
-
for face in obj.faces:
|
|
362
|
-
p1, p2, p3, p4 = [corners[i] for i in face]
|
|
363
|
-
vertices.extend([
|
|
364
|
-
*p1.totuple(), *p2.totuple(), *p3.totuple(),
|
|
365
|
-
*p1.totuple(), *p3.totuple(), *p4.totuple()
|
|
366
|
-
])
|
|
367
|
-
vertices = np.array(vertices, dtype="f4")
|
|
368
|
-
vbo = self.ctx.buffer(vertices.tobytes())
|
|
369
|
-
vao = self.ctx.vertex_array(self.depth_prog, [(vbo, "3f", "in_vert")])
|
|
370
|
-
self.depth_prog["model"].write(np.eye(4, dtype="f4").tobytes())
|
|
371
|
-
self.depth_prog["light_space_matrix"].write(self.light_space_matrix.astype("f4").tobytes())
|
|
372
|
-
vao.render()
|
|
373
|
-
vao.release()
|
|
374
|
-
vbo.release()
|
|
375
|
-
|
|
376
|
-
self.ctx.screen.use()
|
|
377
|
-
|
|
378
|
-
def render_plane(self, p1, p2, p3, color):
|
|
379
|
-
normal = self._compute_normal(p1, p2, p3)
|
|
380
|
-
vertices = np.array([
|
|
381
|
-
*p1.totuple(), *color.to_rgb(), *normal.totuple(),
|
|
382
|
-
*p2.totuple(), *color.to_rgb(), *normal.totuple(),
|
|
383
|
-
*p3.totuple(), *color.to_rgb(), *normal.totuple()
|
|
384
|
-
], dtype="f4")
|
|
385
|
-
vbo = self.ctx.buffer(vertices.tobytes())
|
|
386
|
-
vao = self.ctx.vertex_array(
|
|
387
|
-
self.prog, [(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
|
|
388
|
-
)
|
|
389
|
-
self._send_lights()
|
|
390
|
-
self.prog["light_space_matrix"].write(self.light_space_matrix.astype("f4").tobytes())
|
|
391
|
-
self.shadow_depth_tex.use(location=0)
|
|
392
|
-
self.prog["shadow_map"].value = 0
|
|
393
|
-
vao.render()
|
|
394
|
-
vao.release()
|
|
395
|
-
vbo.release()
|
|
396
|
-
|
|
397
346
|
def render_cylinder(self, center, height, radius, color, segments=32, rotation=vector3d(0, 0, 0)):
|
|
398
347
|
vertices = []
|
|
399
348
|
|
|
@@ -407,7 +356,6 @@ class Renderer:
|
|
|
407
356
|
transformed = m @ vec4
|
|
408
357
|
return vector3d(*transformed[:3]).normalized
|
|
409
358
|
|
|
410
|
-
# Precompute rotation matrix if rotation is provided
|
|
411
359
|
rot_x = Matrix44.from_x_rotation(math.radians(rotation.x))
|
|
412
360
|
rot_y = Matrix44.from_y_rotation(math.radians(rotation.y))
|
|
413
361
|
rot_z = Matrix44.from_z_rotation(math.radians(rotation.z))
|
|
@@ -417,33 +365,21 @@ class Renderer:
|
|
|
417
365
|
theta1 = 2 * math.pi * i / segments
|
|
418
366
|
theta2 = 2 * math.pi * (i + 1) / segments
|
|
419
367
|
|
|
420
|
-
# Top circle vertices
|
|
368
|
+
# Top/bottom circle vertices
|
|
421
369
|
top1 = vector3d(radius * math.cos(theta1), height / 2, radius * math.sin(theta1))
|
|
422
370
|
top2 = vector3d(radius * math.cos(theta2), height / 2, radius * math.sin(theta2))
|
|
423
|
-
|
|
424
|
-
# Bottom circle vertices
|
|
425
371
|
bottom1 = vector3d(radius * math.cos(theta1), -height / 2, radius * math.sin(theta1))
|
|
426
372
|
bottom2 = vector3d(radius * math.cos(theta2), -height / 2, radius * math.sin(theta2))
|
|
427
373
|
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
bottom1 += center
|
|
438
|
-
bottom2 += center
|
|
439
|
-
|
|
440
|
-
# Compute normals for sides
|
|
441
|
-
normal1 = vector3d(math.cos(theta1), 0, math.sin(theta1))
|
|
442
|
-
normal2 = vector3d(math.cos(theta2), 0, math.sin(theta2))
|
|
443
|
-
normal1 = apply_matrix_normal(normal1, rot_matrix)
|
|
444
|
-
normal2 = apply_matrix_normal(normal2, rot_matrix)
|
|
445
|
-
|
|
446
|
-
# Side quad (two triangles)
|
|
374
|
+
top1 = apply_matrix(top1, rot_matrix) + center
|
|
375
|
+
top2 = apply_matrix(top2, rot_matrix) + center
|
|
376
|
+
bottom1 = apply_matrix(bottom1, rot_matrix) + center
|
|
377
|
+
bottom2 = apply_matrix(bottom2, rot_matrix) + center
|
|
378
|
+
|
|
379
|
+
normal1 = apply_matrix_normal(vector3d(math.cos(theta1), 0, math.sin(theta1)), rot_matrix)
|
|
380
|
+
normal2 = apply_matrix_normal(vector3d(math.cos(theta2), 0, math.sin(theta2)), rot_matrix)
|
|
381
|
+
|
|
382
|
+
# Side quad
|
|
447
383
|
for tri in [(top1, bottom1, bottom2), (top1, bottom2, top2)]:
|
|
448
384
|
n1 = (tri[0] - center).normalized
|
|
449
385
|
n2 = (tri[1] - center).normalized
|
|
@@ -455,8 +391,7 @@ class Renderer:
|
|
|
455
391
|
])
|
|
456
392
|
|
|
457
393
|
# Top cap
|
|
458
|
-
top_center = vector3d(0, height / 2, 0)
|
|
459
|
-
top_center = apply_matrix(top_center, rot_matrix) + center
|
|
394
|
+
top_center = apply_matrix(vector3d(0, height / 2, 0), rot_matrix) + center
|
|
460
395
|
normal_top = apply_matrix_normal(vector3d(0, 1, 0), rot_matrix)
|
|
461
396
|
vertices.extend([
|
|
462
397
|
*top_center.totuple(), *color.to_rgb(), *normal_top.totuple(),
|
|
@@ -465,8 +400,7 @@ class Renderer:
|
|
|
465
400
|
])
|
|
466
401
|
|
|
467
402
|
# Bottom cap
|
|
468
|
-
bottom_center = vector3d(0, -height / 2, 0)
|
|
469
|
-
bottom_center = apply_matrix(bottom_center, rot_matrix) + center
|
|
403
|
+
bottom_center = apply_matrix(vector3d(0, -height / 2, 0), rot_matrix) + center
|
|
470
404
|
normal_bottom = apply_matrix_normal(vector3d(0, -1, 0), rot_matrix)
|
|
471
405
|
vertices.extend([
|
|
472
406
|
*bottom_center.totuple(), *color.to_rgb(), *normal_bottom.totuple(),
|
|
@@ -474,40 +408,7 @@ class Renderer:
|
|
|
474
408
|
*bottom2.totuple(), *color.to_rgb(), *normal_bottom.totuple(),
|
|
475
409
|
])
|
|
476
410
|
|
|
477
|
-
|
|
478
|
-
vbo = self.ctx.buffer(vertices.tobytes())
|
|
479
|
-
vao = self.ctx.vertex_array(
|
|
480
|
-
self.prog, [(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
|
|
481
|
-
)
|
|
482
|
-
self._send_lights()
|
|
483
|
-
self.prog["light_space_matrix"].write(self.light_space_matrix.astype("f4").tobytes())
|
|
484
|
-
self.shadow_depth_tex.use(location=0)
|
|
485
|
-
self.prog["shadow_map"].value = 0
|
|
486
|
-
vao.render()
|
|
487
|
-
vao.release()
|
|
488
|
-
vbo.release()
|
|
489
|
-
|
|
490
|
-
def render_quad(self, p1, p2, p3, p4, color):
|
|
491
|
-
normal = self._compute_normal(p1, p2, p3)
|
|
492
|
-
vertices = np.array([
|
|
493
|
-
*p1.totuple(), *color.to_rgb(), *normal.totuple(),
|
|
494
|
-
*p2.totuple(), *color.to_rgb(), *normal.totuple(),
|
|
495
|
-
*p3.totuple(), *color.to_rgb(), *normal.totuple(),
|
|
496
|
-
*p1.totuple(), *color.to_rgb(), *normal.totuple(),
|
|
497
|
-
*p3.totuple(), *color.to_rgb(), *normal.totuple(),
|
|
498
|
-
*p4.totuple(), *color.to_rgb(), *normal.totuple()
|
|
499
|
-
], dtype="f4")
|
|
500
|
-
vbo = self.ctx.buffer(vertices.tobytes())
|
|
501
|
-
vao = self.ctx.vertex_array(
|
|
502
|
-
self.prog, [(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
|
|
503
|
-
)
|
|
504
|
-
self._send_lights()
|
|
505
|
-
self.prog["light_space_matrix"].write(self.light_space_matrix.astype("f4").tobytes())
|
|
506
|
-
self.shadow_depth_tex.use(location=0)
|
|
507
|
-
self.prog["shadow_map"].value = 0
|
|
508
|
-
vao.render()
|
|
509
|
-
vao.release()
|
|
510
|
-
vbo.release()
|
|
411
|
+
self._append_vertices(vertices)
|
|
511
412
|
|
|
512
413
|
def render_sphere(self, center, radius, color, segments=32, rings=16):
|
|
513
414
|
vertices = []
|
|
@@ -517,6 +418,7 @@ class Renderer:
|
|
|
517
418
|
for j in range(segments):
|
|
518
419
|
phi1 = 2 * math.pi * j / segments
|
|
519
420
|
phi2 = 2 * math.pi * (j + 1) / segments
|
|
421
|
+
|
|
520
422
|
p1 = vector3d(radius * math.sin(theta1) * math.cos(phi1),
|
|
521
423
|
radius * math.cos(theta1),
|
|
522
424
|
radius * math.sin(theta1) * math.sin(phi1)) + center
|
|
@@ -529,6 +431,7 @@ class Renderer:
|
|
|
529
431
|
p4 = vector3d(radius * math.sin(theta1) * math.cos(phi2),
|
|
530
432
|
radius * math.cos(theta1),
|
|
531
433
|
radius * math.sin(theta1) * math.sin(phi2)) + center
|
|
434
|
+
|
|
532
435
|
for tri in [(p1, p2, p3), (p1, p3, p4)]:
|
|
533
436
|
n1 = (tri[0] - center).normalized
|
|
534
437
|
n2 = (tri[1] - center).normalized
|
|
@@ -538,18 +441,88 @@ class Renderer:
|
|
|
538
441
|
*tri[1].totuple(), *color.to_rgb(), *n2.totuple(),
|
|
539
442
|
*tri[2].totuple(), *color.to_rgb(), *n3.totuple(),
|
|
540
443
|
])
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
444
|
+
|
|
445
|
+
self._append_vertices(vertices)
|
|
446
|
+
|
|
447
|
+
def render_shadow_pass(self, objects):
|
|
448
|
+
if not self.lights:
|
|
449
|
+
return
|
|
450
|
+
light = self.lights[0]
|
|
451
|
+
self.light_space_matrix = self.compute_light_matrix(light)
|
|
452
|
+
|
|
453
|
+
self.shadow_fbo.clear(depth=1.0)
|
|
454
|
+
self.shadow_fbo.use()
|
|
455
|
+
self.ctx.enable(moderngl.DEPTH_TEST)
|
|
456
|
+
|
|
457
|
+
# Batch all shadow vertices for this frame
|
|
458
|
+
shadow_vertices = []
|
|
459
|
+
|
|
460
|
+
for obj in objects:
|
|
461
|
+
if not hasattr(obj, "get_corners"):
|
|
462
|
+
continue
|
|
463
|
+
corners = obj.get_corners()
|
|
464
|
+
for face in obj.faces:
|
|
465
|
+
p1, p2, p3, p4 = [corners[i] for i in face]
|
|
466
|
+
# Two triangles per quad
|
|
467
|
+
shadow_vertices.extend([
|
|
468
|
+
*p1.totuple(), *p2.totuple(), *p3.totuple(),
|
|
469
|
+
*p1.totuple(), *p3.totuple(), *p4.totuple()
|
|
470
|
+
])
|
|
471
|
+
|
|
472
|
+
if shadow_vertices:
|
|
473
|
+
vertices = np.array(shadow_vertices, dtype="f4")
|
|
474
|
+
vbo = self.ctx.buffer(vertices.tobytes())
|
|
475
|
+
vao = self.ctx.vertex_array(self.depth_prog, [(vbo, "3f", "in_vert")])
|
|
476
|
+
self.depth_prog["model"].write(np.eye(4, dtype="f4").tobytes())
|
|
477
|
+
self.depth_prog["light_space_matrix"].write(self.light_space_matrix.astype("f4").tobytes())
|
|
478
|
+
vao.render()
|
|
479
|
+
vao.release()
|
|
480
|
+
vbo.release()
|
|
481
|
+
|
|
482
|
+
self.ctx.screen.use()
|
|
483
|
+
|
|
484
|
+
def _append_vertices(self, vertices):
|
|
485
|
+
self.frame_vertices.extend(vertices)
|
|
486
|
+
|
|
487
|
+
def flush(self):
|
|
488
|
+
if not self.frame_vertices:
|
|
489
|
+
return
|
|
490
|
+
vertices = np.array(self.frame_vertices, dtype="f4")
|
|
491
|
+
# write to the preallocated VBO
|
|
492
|
+
self.vbo.write(vertices.tobytes())
|
|
493
|
+
|
|
494
|
+
# send lights and shadow map uniforms
|
|
546
495
|
self._send_lights()
|
|
547
496
|
self.prog["light_space_matrix"].write(self.light_space_matrix.astype("f4").tobytes())
|
|
548
497
|
self.shadow_depth_tex.use(location=0)
|
|
549
498
|
self.prog["shadow_map"].value = 0
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
499
|
+
|
|
500
|
+
# render the pre-made VAO
|
|
501
|
+
self.vao.render(vertices=len(vertices))
|
|
502
|
+
|
|
503
|
+
self.frame_vertices.clear()
|
|
504
|
+
# === Modified render calls to batch ===
|
|
505
|
+
|
|
506
|
+
def render_plane(self, p1, p2, p3, color):
|
|
507
|
+
normal = self._compute_normal(p1, p2, p3)
|
|
508
|
+
vertices = [
|
|
509
|
+
*p1.totuple(), *color.to_rgb(), *normal.totuple(),
|
|
510
|
+
*p2.totuple(), *color.to_rgb(), *normal.totuple(),
|
|
511
|
+
*p3.totuple(), *color.to_rgb(), *normal.totuple()
|
|
512
|
+
]
|
|
513
|
+
self._append_vertices(vertices)
|
|
514
|
+
|
|
515
|
+
def render_quad(self, p1, p2, p3, p4, color):
|
|
516
|
+
normal = self._compute_normal(p1, p2, p3)
|
|
517
|
+
vertices = [
|
|
518
|
+
*p1.totuple(), *color.to_rgb(), *normal.totuple(),
|
|
519
|
+
*p2.totuple(), *color.to_rgb(), *normal.totuple(),
|
|
520
|
+
*p3.totuple(), *color.to_rgb(), *normal.totuple(),
|
|
521
|
+
*p1.totuple(), *color.to_rgb(), *normal.totuple(),
|
|
522
|
+
*p3.totuple(), *color.to_rgb(), *normal.totuple(),
|
|
523
|
+
*p4.totuple(), *color.to_rgb(), *normal.totuple()
|
|
524
|
+
]
|
|
525
|
+
self._append_vertices(vertices)
|
|
553
526
|
|
|
554
527
|
def _send_lights(self):
|
|
555
528
|
max_lights = 8
|
|
@@ -562,12 +535,14 @@ class Renderer:
|
|
|
562
535
|
self.prog[f"lights[{i}].color"].value = tuple(c / 255 for c in light.color.to_rgb())
|
|
563
536
|
self.prog[f"lights[{i}].intensity"].value = light.intensity
|
|
564
537
|
|
|
565
|
-
def clear(self, color
|
|
538
|
+
def clear(self, color):
|
|
566
539
|
color_f = np.array(color.to_rgb(), dtype="f4") / 255.0
|
|
567
540
|
self.fbo.clear(*color_f, depth=1.0)
|
|
568
541
|
self.fbo.use()
|
|
542
|
+
self.frame_vertices.clear()
|
|
569
543
|
|
|
570
544
|
def swap(self):
|
|
545
|
+
self.flush() # Render everything in one draw call
|
|
571
546
|
self.ctx.copy_framebuffer(dst=self.ctx.screen, src=self.fbo)
|
|
572
547
|
pygame.display.flip()
|
|
573
548
|
|
|
@@ -764,3 +739,11 @@ class RigidBody:
|
|
|
764
739
|
|
|
765
740
|
def add_force(self, f):
|
|
766
741
|
self.forces += f
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
def get_forward(rotation):
|
|
745
|
+
yaw = math.radians(rotation.z)
|
|
746
|
+
fx = -math.sin(yaw)
|
|
747
|
+
fy = 0
|
|
748
|
+
fz = math.cos(yaw)
|
|
749
|
+
return vector3d(fx, fy, fz)
|
|
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
|
|
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
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|