Skip to content

Commit

Permalink
Example tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
einarf committed Nov 23, 2024
1 parent 1321c38 commit 835ef51
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 35 deletions.
16 changes: 8 additions & 8 deletions examples/geometry_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
28 changes: 16 additions & 12 deletions examples/geometry_cube_instanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Each cube is animated in the vertex shader offset by gl_InstanceID
"""

from pathlib import Path

import numpy
Expand All @@ -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
Expand All @@ -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)
37 changes: 22 additions & 15 deletions examples/gltf_scenes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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"""
Expand Down Expand Up @@ -94,5 +101,5 @@ def render(self, time: float, frame_time: float):
# )


if __name__ == '__main__':
if __name__ == "__main__":
mglw.run_window_config(CubeModel)

0 comments on commit 835ef51

Please sign in to comment.