Skip to content

Commit

Permalink
Merge branch 'master' into default_to_imgui_bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
einarf authored Nov 23, 2024
2 parents 9466f24 + 35f218e commit fbf9754
Show file tree
Hide file tree
Showing 37 changed files with 470 additions and 530 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ and resources to make this project possible.

### Testing & Utility

* Pyrr (https://github.com/adamlwgriffiths/Pyrr)
* PyGLM (https://github.com/Zuzu-Typ/PyGLM)
* numpy (https://github.com/numpy/numpy)
* pytest (https://docs.pytest.org/en/latest/)
* flake8 (https://gitlab.com/pycqa/flake8)
Expand Down
44 changes: 30 additions & 14 deletions examples/advanced/animated_sprites.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path
import moderngl_window as mglw
from moderngl_window import geometry
from pyrr import Matrix44
import glm

# from moderngl_window.conf import settings
# settings.SCREENSHOT_PATH = 'screenshots'
Expand All @@ -11,18 +11,22 @@

class Test(mglw.WindowConfig):
title = "Animated Sprite"
resource_dir = (Path(__file__) / '../../resources').resolve()
resource_dir = (Path(__file__) / "../../resources").resolve()
aspect_ratio = 320 / 256
window_size = 320 * 3, 256 * 3

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('textures/animated_sprites/player_2.gif', layers=35)
self.caveman_texture = self.load_texture_array(
"textures/animated_sprites/player_2.gif", layers=35
)
self.caveman_texture.repeat_x = False
self.caveman_texture.repeat_y = False
self.caveman_texture.filter = moderngl.NEAREST, moderngl.NEAREST
Expand All @@ -33,24 +37,36 @@ def __init__(self, **kwargs):
self.quad_fs = geometry.quad_fs()

# Programs
self.sprite_program = self.load_program('programs/animated_sprites/sprite_array.glsl')
self.texture_program = self.load_program('programs/texture.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 = Matrix44.orthogonal_projection(0, 320, 0, 256, -1.0, 1.0, dtype='f4')
self.sprite_program['projection'].write(self.projection)
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):
# Render sprite of offscreen
self.offscreen.use()
self.ctx.clear(0.5, 0.5, 0.5, 0.0)

self.render_sprite(self.background_texture, frame=int(time * 15) % self.background_texture.layers)
self.render_sprite(self.caveman_texture, frame=int(time * 15) % self.caveman_texture.layers, blend=True, position=(260, 20))
self.render_sprite(
self.background_texture,
frame=int(time * 15) % self.background_texture.layers,
)
self.render_sprite(
self.caveman_texture,
frame=int(time * 15) % self.caveman_texture.layers,
blend=True,
position=(260, 20),
)

# Display offscreen
self.ctx.screen.use()
Expand All @@ -65,14 +81,14 @@ def render_sprite(self, texture, blend=False, frame=0, position=(0, 0)):
self.ctx.enable(moderngl.BLEND)

texture.use(location=0)
self.sprite_program['layer_id'] = frame
self.sprite_program['position'] = position
self.sprite_program["layer_id"] = frame
self.sprite_program["position"] = position
self.sprite_geometry.render(self.sprite_program)

if blend:
self.ctx.disable(moderngl.BLEND)


if __name__ == '__main__':
if __name__ == "__main__":
mglw.run_window_config(Test)
# Test.run()
91 changes: 0 additions & 91 deletions examples/advanced/boids.py

This file was deleted.

69 changes: 0 additions & 69 deletions examples/advanced/boids2.py

This file was deleted.

6 changes: 3 additions & 3 deletions examples/advanced/fragment_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path

import moderngl
from pyrr import Matrix44
import glm
import moderngl_window
from moderngl_window import geometry
from moderngl_window.opengl.projection import Projection3D
Expand Down Expand Up @@ -126,8 +126,8 @@ def __init__(self, **kwargs):
def render(self, time, frametime):
self.ctx.enable(moderngl.DEPTH_TEST | moderngl.CULL_FACE)

translation = Matrix44.from_translation((0, 0, -45 + self.zoom), dtype='f4')
rotation = Matrix44.from_eulers((self.y_rot, self.x_rot, 0), dtype='f4')
translation = glm.translate(glm.vec3(0, 0, -45 + self.zoom))
rotation = glm.mat4(glm.quat(glm.vec3(self.y_rot, self.x_rot, 0)))
self.modelview = translation * rotation

# Render the scene to offscreen buffer
Expand Down
Loading

0 comments on commit fbf9754

Please sign in to comment.