Skip to content

Commit

Permalink
Rename callback functions (on_ prefix)
Browse files Browse the repository at this point in the history
  • Loading branch information
einarf committed Nov 28, 2024
1 parent e215368 commit 1894470
Show file tree
Hide file tree
Showing 49 changed files with 411 additions and 445 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import moderngl_window as mglw
class Test(mglw.WindowConfig):
gl_version = (3, 3)

def render(self, time, frametime):
def on_render(self, time: float, frametime: float):
self.ctx.clear(1.0, 0.0, 0.0, 0.0)

Test.run()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ def __init__(self, **kwargs):
super().__init__(**kwargs)
self.buffer_size = 320, 256
# Textures
self.background_texture = self.load_texture_array(
"textures/animated_sprites/giphy.gif"
)
self.background_texture = self.load_texture_array("textures/animated_sprites/giphy.gif")
self.background_texture.repeat_x = False
self.background_texture.repeat_y = False
self.caveman_texture = self.load_texture_array(
Expand All @@ -39,22 +37,18 @@ def __init__(self, **kwargs):
self.quad_fs = geometry.quad_fs()

# Programs
self.sprite_program = self.load_program(
"programs/animated_sprites/sprite_array.glsl"
)
self.sprite_program = self.load_program("programs/animated_sprites/sprite_array.glsl")
self.texture_program = self.load_program("programs/texture.glsl")

# Offscreen buffer
self.offscreen_texture = self.ctx.texture(self.buffer_size, 4)
self.offscreen_texture.filter = moderngl.NEAREST, moderngl.NEAREST
self.offscreen = self.ctx.framebuffer(
color_attachments=[self.offscreen_texture]
)
self.offscreen = self.ctx.framebuffer(color_attachments=[self.offscreen_texture])

self.projection = glm.ortho(0, 320, 0, 256, -1.0, 1.0)
self.sprite_program["projection"].write(self.projection)

def render(self, time, frame_time):
def on_render(self, time, frame_time):
# Render sprite of offscreen
self.offscreen.use()
self.ctx.clear(0.5, 0.5, 0.5, 0.0)
Expand Down
6 changes: 3 additions & 3 deletions examples/advanced/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __init__(self, **kwargs):
self.camera = KeyboardCamera(self.wnd.keys, aspect_ratio=self.wnd.aspect_ratio)
self.camera_enabled = True

def key_event(self, key, action, modifiers):
def on_key_event(self, key, action, modifiers):
keys = self.wnd.keys

if self.camera_enabled:
Expand All @@ -24,9 +24,9 @@ def key_event(self, key, action, modifiers):
if key == keys.SPACE:
self.timer.toggle_pause()

def mouse_position_event(self, x: int, y: int, dx, dy):
def on_mouse_position_event(self, x: int, y: int, dx, dy):
if self.camera_enabled:
self.camera.rot_state(-dx, -dy)

def resize(self, width: int, height: int):
def on_resize(self, width: int, height: int):
self.camera.projection.update(aspect_ratio=self.wnd.aspect_ratio)
15 changes: 8 additions & 7 deletions examples/advanced/compute_render_to_texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,30 @@

class ComputeRenderToTexture(mglw.WindowConfig):
"""Simple example rendering to a texture with a compute shader"""

title = "Render Texture Using Compute Shader"
resource_dir = (Path(__file__) / '../../resources').resolve()
resource_dir = (Path(__file__) / "../../resources").resolve()
gl_version = 4, 3
aspect_ratio = 1.0

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.compute_shader = self.load_compute_shader('programs/compute/render_to_texture.glsl')
self.compute_shader['destTex'] = 0
self.texture_program = self.load_program('programs/texture.glsl')
self.compute_shader = self.load_compute_shader("programs/compute/render_to_texture.glsl")
self.compute_shader["destTex"] = 0
self.texture_program = self.load_program("programs/texture.glsl")
self.quad_fs = geometry.quad_fs()
self.texture = self.ctx.texture((256, 256), 4)
self.texture.filter = mgl.NEAREST, mgl.NEAREST

def render(self, time, frame_time):
def on_render(self, time, frame_time):
self.ctx.clear(0.3, 0.3, 0.3)

w, h = self.texture.size
gw, gh = 16, 16
nx, ny, nz = int(w / gw), int(h / gh), 1

try:
self.compute_shader['time'] = time
self.compute_shader["time"] = time
except Exception:
pass
# Automatically binds as a GL_R32F / r32f (read from the texture)
Expand All @@ -42,5 +43,5 @@ def render(self, time, frame_time):
self.quad_fs.render(self.texture_program)


if __name__ == '__main__':
if __name__ == "__main__":
mglw.run_window_config(ComputeRenderToTexture)
16 changes: 8 additions & 8 deletions examples/advanced/fragment_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self, **kwargs):

# Load scene cached to speed up loading!
self.scene = self.load_scene("scenes/fragment_picking/centered.obj", cache=True)
# Grab the raw mesh/vertexarray
# Grab the raw mesh/vertex array
self.mesh = self.scene.root_nodes[0].mesh.vao
self.mesh_texture = self.scene.root_nodes[0].mesh.material.mat_texture.texture

Expand Down Expand Up @@ -117,7 +117,7 @@ def __init__(self, **kwargs):
# Marker geometry
self.marker_buffer = self.ctx.buffer(
reserve=self.marker_byte_size * 1000
) # Resever room for 1000 points
) # Reserve room for 1000 points
self.marker_vao = VAO(name="markers", mode=moderngl.POINTS)
self.marker_vao.buffer(
self.marker_buffer, "3f 3f 1f", ["in_position", "in_normal", "temperature"]
Expand All @@ -129,7 +129,7 @@ def __init__(self, **kwargs):
self.quad_depth = geometry.quad_2d(size=(0.25, 0.25), pos=(0.5, 0.875))
self.quad_positions = geometry.quad_2d(size=(0.25, 0.25), pos=(0.25, 0.875))

def render(self, time, frametime):
def on_render(self, time: float, frametime: float):
self.ctx.enable(moderngl.DEPTH_TEST | moderngl.CULL_FACE)

translation = glm.translate(glm.vec3(0, 0, -45 + self.zoom))
Expand Down Expand Up @@ -179,12 +179,12 @@ def render_debug(self):
self.offscreen_viewpos.use()
self.quad_positions.render(self.texture_program)

def mouse_drag_event(self, x, y, dx, dy):
def on_mouse_drag_event(self, x, y, dx, dy):
"""Pick up mouse drag movements"""
self.x_rot -= dx / 100
self.y_rot -= dy / 100

def mouse_press_event(self, x, y, button):
def on_mouse_press_event(self, x, y, button):
"""Attempts to get the view position from a fragment"""

# only care about right mouse button clicks
Expand Down Expand Up @@ -220,10 +220,10 @@ def mouse_press_event(self, x, y, button):
)
self.num_markers += 1

def mouse_scroll_event(self, x_offset, y_offset):
def on_mouse_scroll_event(self, x_offset, y_offset):
self.zoom += y_offset

def key_event(self, key, action, modifiers):
def on_key_event(self, key, action, modifiers):
keys = self.wnd.keys

# Key presses
Expand Down Expand Up @@ -256,4 +256,4 @@ def save_markers(self, path: Path):


if __name__ == "__main__":
moderngl_window.run_window_config(FragmentPicking)
FragmentPicking.run()
30 changes: 10 additions & 20 deletions examples/advanced/navier_stokes.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,12 @@ def __init__(self, *args, **kwargs):
self.walls_texture = self.ctx.texture(size, 1, dtype="f4")
self.difference_texture = self.ctx.texture(size, 1, dtype="f4")

self.momentum_fbo_1 = self.ctx.framebuffer(
color_attachments=[self.momentum_texture_1]
)
self.momentum_fbo_2 = self.ctx.framebuffer(
color_attachments=[self.momentum_texture_2]
)
self.pressure_fbo_1 = self.ctx.framebuffer(
color_attachments=[self.pressure_texture_1]
)
self.pressure_fbo_2 = self.ctx.framebuffer(
color_attachments=[self.pressure_texture_2]
)
self.momentum_fbo_1 = self.ctx.framebuffer(color_attachments=[self.momentum_texture_1])
self.momentum_fbo_2 = self.ctx.framebuffer(color_attachments=[self.momentum_texture_2])
self.pressure_fbo_1 = self.ctx.framebuffer(color_attachments=[self.pressure_texture_1])
self.pressure_fbo_2 = self.ctx.framebuffer(color_attachments=[self.pressure_texture_2])
self.walls_fbo = self.ctx.framebuffer(color_attachments=[self.walls_texture])
self.difference_fbo = self.ctx.framebuffer(
color_attachments=[self.difference_texture]
)
self.difference_fbo = self.ctx.framebuffer(color_attachments=[self.difference_texture])

# programs
self.texture_prog = self.load_program("programs/navier-stokes/texture.glsl")
Expand Down Expand Up @@ -134,7 +124,7 @@ def wall(self, x, y):
self.drop_prog["write_value"].value = 1.0
self.drop_geometry.render(self.drop_prog)

def render(self, time, frame_time):
def on_render(self, time: float, frame_time: float):
# calculate momentum
self.momentum_fbo_1.use()
self.momentum_texture_2.use(location=0)
Expand Down Expand Up @@ -189,19 +179,19 @@ def pressure_swap(self):
self.pressure_fbo_1,
)

def mouse_press_event(self, x, y, button):
def on_mouse_press_event(self, x, y, button):
if button == self.wnd.mouse.left:
self.drop(x, y)
elif button == self.wnd.mouse.right:
self.wall(x, y)

def mouse_drag_event(self, x, y, dx, dy):
def on_mouse_drag_event(self, x, y, dx, dy):
if self.wnd.mouse_states.left:
self.drop(x, y)
elif self.wnd.mouse_states.right:
self.wall(x, y)

def key_event(self, key, action, modifiers):
def on_key_event(self, key, action, modifiers):
keys = self.wnd.keys

if action == keys.ACTION_PRESS:
Expand All @@ -210,4 +200,4 @@ def key_event(self, key, action, modifiers):


if __name__ == "__main__":
moderngl_window.run_window_config(NavierStokes2D)
NavierStokes2D.run()
2 changes: 1 addition & 1 deletion examples/advanced/pygame2.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, **kwargs):
)
self.texture_prog["m_model"].write(glm.mat4())

def render(self, time, frametime):
def on_render(self, time, frametime):
# time = self.wnd.frames / 30

self.ctx.enable_only(moderngl.DEPTH_TEST | moderngl.CULL_FACE)
Expand Down
21 changes: 12 additions & 9 deletions examples/advanced/pygame2_background_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Clears the screen using opengl with a constantly changing
color value and alpha blend a pygame surface on top.
"""

import math
from pathlib import Path

Expand All @@ -20,9 +21,10 @@ class Pygame(moderngl_window.WindowConfig):
Example using pygame with moderngl.
Needs to run with ``--window pygame2`` option.
"""

title = "Pygame"
window_size = 1280, 720
resource_dir = (Path(__file__) / '../../resources').absolute()
resource_dir = (Path(__file__) / "../../resources").absolute()

def __init__(self, **kwargs):
super().__init__(**kwargs)
Expand All @@ -34,18 +36,18 @@ def __init__(self, **kwargs):
self.pg_texture.filter = moderngl.NEAREST, moderngl.NEAREST
self.pg_texture.swizzle = "BGRA"

self.texture_program = self.load_program('programs/texture.glsl')
self.quad_texture = self.load_texture_2d('textures/python-bg.png')
self.texture_program = self.load_program("programs/texture.glsl")
self.quad_texture = self.load_texture_2d("textures/python-bg.png")
self.quad_fs = geometry.quad_fs()

def render(self, time, frametime):
def on_render(self, time: float, frametime: float):
self.ctx.clear()

self.ctx.enable(moderngl.BLEND)

# Render background graphics
self.quad_texture.use()
self.texture_program['texture0'].value = 0
self.texture_program["texture0"].value = 0
self.quad_fs.render(self.texture_program)

# Render foreground objects
Expand All @@ -66,15 +68,16 @@ def render_pygame(self, time):
((i * 50) % 255, (i * 100) % 255, (i * 20) % 255),
(
math.sin(time + time_offset) * 200 + self.window_size[0] // 2,
math.cos(time + time_offset) * 200 + self.window_size[1] // 2),
math.cos(time + time_offset) * 200 + self.window_size[1] // 2,
),
math.sin(time) * 7 + 15,
)

# Get the buffer view of the Surface's pixels
# and write this data into the texture
texture_data = self.pg_screen.get_view('1')
texture_data = self.pg_screen.get_view("1")
self.pg_texture.write(texture_data)


if __name__ == '__main__':
moderngl_window.run_window_config(Pygame, args=('--window', 'pygame2'))
if __name__ == "__main__":
moderngl_window.run_window_config(Pygame, args=("--window", "pygame2"))
2 changes: 1 addition & 1 deletion examples/advanced/pygame2_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def __init__(self, **kwargs):
],
)

def render(self, time: float, frame_time: float):
def on_render(self, time: float, frame_time: float):
"""Called every frame"""
self.render_pygame(time)

Expand Down
13 changes: 7 additions & 6 deletions examples/advanced/shader_includes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
We include a library doing different blend types
and render each quadrant of the screen with different blend types
"""

from pathlib import Path

import moderngl_window as mglw
Expand All @@ -13,21 +14,21 @@

class ShaderInclude(mglw.WindowConfig):
title = "Shader Include"
resource_dir = (Path(__file__) / '../../resources').resolve()
resource_dir = (Path(__file__) / "../../resources").resolve()
aspect_ratio = 1

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.program = self.load_program('programs/blend_include.glsl')
self.texture_0 = self.load_texture_2d('textures/cubemaps/yokohama/negx.jpg')
self.texture_1 = self.load_texture_2d('textures/cubemaps/yokohama/negz.jpg')
self.program = self.load_program("programs/blend_include.glsl")
self.texture_0 = self.load_texture_2d("textures/cubemaps/yokohama/negx.jpg")
self.texture_1 = self.load_texture_2d("textures/cubemaps/yokohama/negz.jpg")
self.quad_fs = geometry.quad_fs()

def render(self, time, frame_time):
def on_render(self, time, frame_time):
self.texture_0.use(location=0)
self.texture_1.use(location=1)
self.quad_fs.render(self.program)


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

0 comments on commit 1894470

Please sign in to comment.