From 835ef51868246e8272f97875a3529cadc755d2a9 Mon Sep 17 00:00:00 2001 From: Einar Forselv Date: Sat, 23 Nov 2024 22:42:42 +0100 Subject: [PATCH] Example tweaks --- examples/geometry_cube.py | 16 ++++++------- examples/geometry_cube_instanced.py | 28 ++++++++++++---------- examples/gltf_scenes.py | 37 +++++++++++++++++------------ 3 files changed, 46 insertions(+), 35 deletions(-) diff --git a/examples/geometry_cube.py b/examples/geometry_cube.py index 36ad58c..50a7038 100644 --- a/examples/geometry_cube.py +++ b/examples/geometry_cube.py @@ -10,28 +10,28 @@ class CubeSimple(CameraWindow): title = "Plain Cube" - resource_dir = (Path(__file__).parent / 'resources').resolve() + resource_dir = (Path(__file__).parent / "resources").resolve() def __init__(self, **kwargs): super().__init__(**kwargs) self.wnd.mouse_exclusivity = True self.cube = geometry.cube(size=(2, 2, 2)) - self.prog = self.load_program('programs/cube_simple.glsl') - self.prog['color'].value = 1.0, 1.0, 1.0, 1.0 + self.prog = self.load_program("programs/cube_simple.glsl") + self.prog["color"].value = 1.0, 1.0, 1.0, 1.0 def render(self, time: float, frametime: float): self.ctx.enable_only(moderngl.CULL_FACE | moderngl.DEPTH_TEST) rotation = glm.mat4(glm.quat(glm.vec3(time, time, time))) translation = glm.translate(glm.vec3(0.0, 0.0, -3.5)) - modelview = translation * rotation + modelview = translation @ rotation - self.prog['m_proj'].write(self.camera.projection.matrix) - self.prog['m_model'].write(modelview) - self.prog['m_camera'].write(self.camera.matrix) + self.prog["m_proj"].write(self.camera.projection.matrix) + self.prog["m_model"].write(modelview) + self.prog["m_camera"].write(self.camera.matrix) self.cube.render(self.prog) -if __name__ == '__main__': +if __name__ == "__main__": moderngl_window.run_window_config(CubeSimple) diff --git a/examples/geometry_cube_instanced.py b/examples/geometry_cube_instanced.py index 43baadb..892966a 100644 --- a/examples/geometry_cube_instanced.py +++ b/examples/geometry_cube_instanced.py @@ -4,6 +4,7 @@ Each cube is animated in the vertex shader offset by gl_InstanceID """ + from pathlib import Path import numpy @@ -16,24 +17,25 @@ class CubeSimpleInstanced(CameraWindow): """Renders cubes using instancing""" + title = "Plain Cube" - resource_dir = (Path(__file__).parent / 'resources').resolve() + resource_dir = (Path(__file__).parent / "resources").resolve() aspect_ratio = None - def __init__(self, **kwargs): + def __init__(self, **kwargs) -> None: super().__init__(**kwargs) self.wnd.mouse_exclusivity = True self.camera.projection.update(near=1, far=1000) self.cube = geometry.cube(size=(2, 2, 2)) - self.prog = self.load_program('programs/cube_simple_instanced.glsl') - self.prog['m_model'].write(glm.mat4()) + self.prog = self.load_program("programs/cube_simple_instanced.glsl") + self.prog["m_model"].write(glm.mat4()) # Generate per instance data represeting a grid of cubes N = 100 self.instances = N * N def gen_data(x_res, z_res, spacing=2.5): - """Generates a grid of N * N poistions and random colors on the xz plane""" + """Generates a grid of N * N position and random colors on the xz plane""" for y in range(z_res): for x in range(x_res): yield -N * spacing / 2 + spacing * x @@ -43,18 +45,20 @@ def gen_data(x_res, z_res, spacing=2.5): yield numpy.random.uniform(0, 1) yield numpy.random.uniform(0, 1) - self.instance_data = self.ctx.buffer(numpy.fromiter(gen_data(N, N), 'f4', count=self.instances * 6)) - self.cube.buffer(self.instance_data, '3f 3f/i', ['in_offset', 'in_color']) + self.instance_data = self.ctx.buffer( + numpy.fromiter(gen_data(N, N), "f4", count=self.instances * 6) + ) + self.cube.buffer(self.instance_data, "3f 3f/i", ["in_offset", "in_color"]) - def render(self, time: float, frametime: float): + def render(self, time: float, frametime: float) -> None: self.ctx.enable_only(moderngl.CULL_FACE | moderngl.DEPTH_TEST) - self.prog['m_proj'].write(self.camera.projection.matrix) - self.prog['m_camera'].write(self.camera.matrix) - self.prog['time'].value = time + self.prog["m_proj"].write(self.camera.projection.matrix) + self.prog["m_camera"].write(self.camera.matrix) + self.prog["time"].value = time self.cube.render(self.prog, instances=self.instances) -if __name__ == '__main__': +if __name__ == "__main__": moderngl_window.run_window_config(CubeSimpleInstanced) diff --git a/examples/gltf_scenes.py b/examples/gltf_scenes.py index ba6ba4b..b458a3f 100644 --- a/examples/gltf_scenes.py +++ b/examples/gltf_scenes.py @@ -13,24 +13,25 @@ class CubeModel(CameraWindow): model samples repository and ensure resource_dir is set correctly: https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0 """ - title = 'GL Transmission Format (glTF) 2.0 Scene' + + title = "GL Transmission Format (glTF) 2.0 Scene" window_size = 1280, 720 aspect_ratio = None - resource_dir = Path(__file__, '../../../glTF-Sample-Models/2.0').resolve() + resource_dir = Path(__file__, "../../../glTF-Sample-Models/2.0").resolve() def __init__(self, **kwargs): super().__init__(**kwargs) self.wnd.mouse_exclusivity = True # --- glTF-Sample-Models --- - # self.scene = self.load_scene('2CylinderEngine/glTF-Binary/2CylinderEngine.glb') + # self.scene = self.load_scene("2CylinderEngine/glTF-Binary/2CylinderEngine.glb") # self.scene = self.load_scene('CesiumMilkTruck/glTF-Embedded/CesiumMilkTruck.gltf') # self.scene = self.load_scene('CesiumMilkTruck/glTF-Binary/CesiumMilkTruck.glb') - # self.scene = self.load_scene('CesiumMilkTruck/glTF/CesiumMilkTruck.gltf') - self.scene = self.load_scene('Sponza/glTF/Sponza.gltf') - # self.scene = self.load_scene('Lantern/glTF-Binary/Lantern.glb') - # self.scene = self.load_scene('Buggy/glTF-Binary/Buggy.glb') - # self.scene = self.load_scene('VC/glTF-Binary/VC.glb') + # self.scene = self.load_scene("CesiumMilkTruck/glTF/CesiumMilkTruck.gltf") + self.scene = self.load_scene("Sponza/glTF/Sponza.gltf") + # self.scene = self.load_scene("Lantern/glTF-Binary/Lantern.glb") + # self.scene = self.load_scene("Buggy/glTF-Binary/Buggy.glb") + # self.scene = self.load_scene("VC/glTF-Binary/VC.glb") # self.scene = self.load_scene('DamagedHelmet/glTF-Binary/DamagedHelmet.glb') # self.scene = self.load_scene('BoxInterleaved/glTF/BoxInterleaved.gltf') # self.scene = self.load_scene('OrientationTest/glTF/OrientationTest.gltf') @@ -45,25 +46,31 @@ def __init__(self, **kwargs): # self.scene = self.load_scene('FlightHelmet/glTF/FlightHelmet.gltf') # self.scene = self.load_scene('Fox/glTF/Fox.gltf') # self.scene = self.load_scene('GearboxAssy/glTF/GearboxAssy.gltf') - # self.scene = self.load_scene('ReciprocatingSaw/glTF/ReciprocatingSaw.gltf') + # self.scene = self.load_scene("ReciprocatingSaw/glTF/ReciprocatingSaw.gltf") # self.scene = self.load_scene('RiggedFigure/glTF/RiggedFigure.gltf') # self.scene = self.load_scene('RiggedSimple/glTF/RiggedSimple.gltf') # self.scene = self.load_scene('SciFiHelmet/glTF/SciFiHelmet.gltf') # self.scene = self.load_scene('SimpleMeshes/glTF/SimpleMeshes.gltf') # self.scene = self.load_scene('SimpleSparseAccessor/glTF/SimpleSparseAccessor.gltf') - # self.scene = self.load_scene('Suzanne/glTF/Suzanne.gltf') + # self.scene = self.load_scene("Suzanne/glTF/Suzanne.gltf") # self.scene = self.load_scene('TextureCoordinateTest/glTF/TextureCoordinateTest.gltf') # self.scene = self.load_scene('TextureSettingsTest/glTF/TextureSettingsTest.gltf') # self.scene = self.load_scene('VertexColorTest/glTF/VertexColorTest.gltf') # self.scene = self.load_scene('WaterBottle/glTF/WaterBottle.gltf') - self.camera = KeyboardCamera(self.wnd.keys, fov=75.0, aspect_ratio=self.wnd.aspect_ratio, near=0.1, far=1000.0) - # self.camera.velocity = 7.0 - # self.camera.mouse_sensitivity = 0.3 + self.camera = KeyboardCamera( + self.wnd.keys, + fov=75.0, + aspect_ratio=self.wnd.aspect_ratio, + near=0.1, + far=1000.0, + ) + self.camera.velocity = 10.0 + self.camera.mouse_sensitivity = 0.25 # Use this for gltf scenes for better camera controls if self.scene.diagonal_size > 0: - self.camera.velocity = self.scene.diagonal_size / 5.0 + self.camera.velocity = self.scene.diagonal_size / 10.0 def render(self, time: float, frame_time: float): """Render the scene""" @@ -94,5 +101,5 @@ def render(self, time: float, frame_time: float): # ) -if __name__ == '__main__': +if __name__ == "__main__": mglw.run_window_config(CubeModel)