Skip to content

Commit

Permalink
Merge pull request #198 from henri-gasc/master
Browse files Browse the repository at this point in the history
Drop Pyrr and replace it with PyGLM
  • Loading branch information
einarf authored Nov 23, 2024
2 parents 2cf5d63 + 1870772 commit e009da6
Show file tree
Hide file tree
Showing 34 changed files with 158 additions and 173 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
4 changes: 2 additions & 2 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 Down Expand Up @@ -41,7 +41,7 @@ def __init__(self, **kwargs):
self.offscreen_texture.filter = moderngl.NEAREST, moderngl.NEAREST
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.projection = glm.orthographic(0, 320, 0, 256, -1.0, 1.0)
self.sprite_program['projection'].write(self.projection)

def render(self, time, frame_time):
Expand Down
5 changes: 2 additions & 3 deletions examples/advanced/boids.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path
import random
import numpy
from pyrr import matrix44
import glm

import moderngl
import moderngl_window
Expand Down Expand Up @@ -61,11 +61,10 @@ def gen_initial_data(n, x_area=2.0, y_area=2.0):
self.boids_transform_program = self.load_program('programs/boids/boids_transform.glsl')

# Prepare for rendering
self.m_proj = matrix44.create_orthogonal_projection(
self.m_proj = glm.orthographic(
-self.aspect_ratio, self.aspect_ratio,
-1.0, 1.0,
-1.0, 1.0,
dtype='f4',
)
self.boids_render_program['m_proj'].write(self.m_proj.tobytes())
self.boids_transform_program['data'].value = 0
Expand Down
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
5 changes: 2 additions & 3 deletions examples/advanced/navier_stokes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import random
from pathlib import Path
import numpy as np
from pyrr import matrix44
import glm

import moderngl_window
from moderngl_window import geometry
Expand All @@ -31,11 +31,10 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# various vars
self.m_proj = matrix44.create_orthogonal_projection(
self.m_proj = glm.orthographic(
0, self.wnd.buffer_width,
0, self.wnd.buffer_height,
-1, 1,
dtype='f4',
)
size = self.wnd.buffer_size

Expand Down
12 changes: 6 additions & 6 deletions examples/advanced/pygame2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import moderngl
import moderngl_window
from moderngl_window import geometry
from pyrr import matrix44
import glm

# from moderngl_window.conf import settings
# settings.SCREENSHOT_PATH = 'capture'
Expand Down Expand Up @@ -41,18 +41,18 @@ def __init__(self, **kwargs):
# Simple geometry and shader to render
self.cube = geometry.cube(size=(2.0, 2.0, 2.0))
self.texture_prog = self.load_program('programs/cube_simple_texture.glsl')
self.texture_prog['m_proj'].write(matrix44.create_perspective_projection(60, self.wnd.aspect_ratio, 1, 100, dtype='f4'))
self.texture_prog['m_model'].write(matrix44.create_identity(dtype='f4'))
self.texture_prog['m_proj'].write(glm.perspective(glm.radians(60), self.wnd.aspect_ratio, 1, 100))
self.texture_prog['m_model'].write(glm.mat4())

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

self.ctx.enable_only(moderngl.DEPTH_TEST | moderngl.CULL_FACE)
self.render_pygame(time)

rotate = matrix44.create_from_eulers((time, time * 1.2, time * 1.3), dtype='f4')
translate = matrix44.create_from_translation((0, 0, -3.5), dtype='f4')
camera = matrix44.multiply(rotate, translate)
rotate = glm.mat4(glm.quat(glm.vec3(time, time * 1.2, time * 1.3)))
translate = glm.translate(glm.vec3(0, 0, -3.5))
camera = rotate * translate

self.texture_prog['m_camera'].write(camera)
self.pg_texture.use()
Expand Down
19 changes: 9 additions & 10 deletions examples/advanced/shadow_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""
import math
from pathlib import Path
from pyrr import Matrix44, matrix44, Vector3
import glm

import moderngl
import moderngl_window
Expand Down Expand Up @@ -59,15 +59,15 @@ def __init__(self, **kwargs):

def render(self, time, frametime):
self.ctx.enable_only(moderngl.DEPTH_TEST | moderngl.CULL_FACE)
self.lightpos = Vector3((math.sin(time) * 20, 5, math.cos(time) * 20), dtype='f4')
scene_pos = Vector3((0, -5, -32), dtype='f4')
self.lightpos = glm.vec3(math.sin(time) * 20, 5, math.cos(time) * 20)
scene_pos = glm.vec3(0, -5, -32)

# --- PASS 1: Render shadow map
self.offscreen.clear()
self.offscreen.use()

depth_projection = Matrix44.orthogonal_projection(-20, 20, -20, 20, -20, 40, dtype='f4')
depth_view = Matrix44.look_at(self.lightpos, (0, 0, 0), (0, 1, 0), dtype='f4')
depth_projection = glm.orthographic(-20, 20, -20, 20, -20, 40)
depth_view = glm.lookAt(self.lightpos, (0, 0, 0), (0, 1, 0))
depth_mvp = depth_projection * depth_view
self.shadowmap_program['mvp'].write(depth_mvp)

Expand All @@ -79,15 +79,14 @@ def render(self, time, frametime):
self.wnd.use()
self.basic_light['m_proj'].write(self.camera.projection.matrix)
self.basic_light['m_camera'].write(self.camera.matrix)
self.basic_light['m_model'].write(Matrix44.from_translation(scene_pos, dtype='f4'))
bias_matrix = Matrix44(
self.basic_light['m_model'].write(glm.translate(glm.vec3(scene_pos)))
bias_matrix = glm.mat4(
[[0.5, 0.0, 0.0, 0.0],
[0.0, 0.5, 0.0, 0.0],
[0.0, 0.0, 0.5, 0.0],
[0.5, 0.5, 0.5, 1.0]],
dtype='f4',
)
self.basic_light['m_shadow_bias'].write(matrix44.multiply(depth_mvp, bias_matrix))
self.basic_light['m_shadow_bias'].write(depth_mvp * bias_matrix)
self.basic_light['lightDir'].write(self.lightpos)
self.offscreen_depth.use(location=0)
self.floor.render(self.basic_light)
Expand All @@ -97,7 +96,7 @@ def render(self, time, frametime):
# Render the sun position
self.sun_prog['m_proj'].write(self.camera.projection.matrix)
self.sun_prog['m_camera'].write(self.camera.matrix)
self.sun_prog['m_model'].write(Matrix44.from_translation(self.lightpos + scene_pos, dtype='f4'))
self.sun_prog['m_model'].write(glm.translate(glm.vec3(self.lightpos + scene_pos)))
self.sun.render(self.sun_prog)

# --- PASS 3: Debug ---
Expand Down
19 changes: 9 additions & 10 deletions examples/advanced/shadow_mapping_56.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""
import math
from pathlib import Path
from pyrr import Matrix44, matrix44, Vector3
import glm

import moderngl
import moderngl_window
Expand Down Expand Up @@ -59,15 +59,15 @@ def __init__(self, **kwargs):

def render(self, time, frametime):
self.ctx.enable_only(moderngl.DEPTH_TEST | moderngl.CULL_FACE)
self.lightpos = Vector3((math.sin(time) * 20, 5, math.cos(time) * 20), dtype='f4')
scene_pos = Vector3((0, -5, -32), dtype='f4')
self.lightpos = glm.vec3(math.sin(time) * 20, 5, math.cos(time) * 20)
scene_pos = glm.vec3(0, -5, -32)

# --- PASS 1: Render shadow map
self.offscreen.clear()
self.offscreen.use()

depth_projection = Matrix44.orthogonal_projection(-20, 20, -20, 20, -20, 40, dtype='f4')
depth_view = Matrix44.look_at(self.lightpos, (0, 0, 0), (0, 1, 0), dtype='f4')
depth_projection = glm.orthographic(-20, 20, -20, 20, -20, 40)
depth_view = glm.lookAt(self.lightpos, (0, 0, 0), (0, 1, 0))
depth_mvp = depth_projection * depth_view
self.shadowmap_program['mvp'].write(depth_mvp)

Expand All @@ -79,15 +79,14 @@ def render(self, time, frametime):
self.wnd.use()
self.basic_light['m_proj'].write(self.camera.projection.matrix)
self.basic_light['m_camera'].write(self.camera.matrix)
self.basic_light['m_model'].write(Matrix44.from_translation(scene_pos, dtype='f4'))
bias_matrix = Matrix44(
self.basic_light['m_model'].write(glm.translate(glm.vec3(scene_pos)))
bias_matrix = glm.mat4(
[[0.5, 0.0, 0.0, 0.0],
[0.0, 0.5, 0.0, 0.0],
[0.0, 0.0, 0.5, 0.0],
[0.5, 0.5, 0.5, 1.0]],
dtype='f4',
)
self.basic_light['m_shadow_bias'].write(matrix44.multiply(depth_mvp, bias_matrix))
self.basic_light['m_shadow_bias'].write(depth_mvp * bias_matrix)
self.basic_light['lightDir'].write(self.lightpos)
self.offscreen_depth.use(location=0)
self.floor.render(self.basic_light)
Expand All @@ -97,7 +96,7 @@ def render(self, time, frametime):
# Render the sun position
self.sun_prog['m_proj'].write(self.camera.projection.matrix)
self.sun_prog['m_camera'].write(self.camera.matrix)
self.sun_prog['m_model'].write(Matrix44.from_translation(self.lightpos + scene_pos, dtype='f4'))
self.sun_prog['m_model'].write(glm.translate(glm.vec3(self.lightpos + scene_pos)))
self.sun.render(self.sun_prog)

# --- PASS 3: Debug ---
Expand Down
8 changes: 4 additions & 4 deletions examples/advanced/tetrahedral_mesh.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pathlib import Path
import numpy as np
from pyrr import Matrix44
import glm

import moderngl
import moderngl_window
Expand Down Expand Up @@ -105,9 +105,9 @@ def render(self, time, frametime):
self.ctx.enable_only(moderngl.DEPTH_TEST | moderngl.CULL_FACE)

# Render tetrahedral mesh
translate = Matrix44.from_translation((0.0, 2.5, -15.0), dtype='f4')
rotate = Matrix44.from_eulers((np.radians(180), 0, 0), dtype='f4')
scale = Matrix44.from_scale((400, 400, 400), dtype='f4')
translate = glm.translate(glm.vec3(0.0, 2.5, -15.0))
rotate = glm.mat4(glm.quat(glm.vec3(np.radians(180), 0, 0)))
scale = glm.scale(glm.vec3(400, 400, 400))
mat = self.camera.matrix * translate * rotate * scale

# All render calls inside this context are timed
Expand Down
11 changes: 4 additions & 7 deletions examples/advanced/voxel_cubes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@

import moderngl
from moderngl.program_members import varying
from pyrr.matrix44 import inverse
from moderngl_window import geometry
from base import CameraWindow
from pyrr import Matrix44, Matrix33
import glm


class CubeVoxel(CameraWindow):
Expand Down Expand Up @@ -129,11 +128,10 @@ def max_cubes(self) -> int:

def render_wireframe(self, *, projection_matrix, camera_matrix, model_matrix=None):
self.ctx.wireframe = True
translate = Matrix44.from_translation((
translate = glm.translate(glm.vec3(
-self._size[0] / 2,
-self._size[0] / 2,
-self._size[0] * 2),
dtype='f4',
)
mat = camera_matrix * translate
self.voxel_wireframe_prog["m_proj"].write(projection_matrix)
Expand All @@ -144,14 +142,13 @@ def render_wireframe(self, *, projection_matrix, camera_matrix, model_matrix=Non
def render(self, *, projection_matrix, camera_matrix, model_matrix=None):
"""Render out the voxel to the screen"""

translate = Matrix44.from_translation((
translate = glm.translate(glm.vec3(
-self._size[0] / 2,
-self._size[0] / 2,
-self._size[0] * 2),
dtype='f4',
)
mat = camera_matrix * translate
normal = Matrix33.from_matrix44(mat).inverse.transpose().astype("f4").tobytes()
normal = glm.transpose(glm.inverse(glm.mat3(mat))).to_bytes()
self.voxel_light_prog["m_proj"].write(projection_matrix)
self.voxel_light_prog["m_modelview"].write(mat)
self.voxel_light_prog["m_normal"].write(normal)
Expand Down
6 changes: 3 additions & 3 deletions examples/cube_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from pyrr import Matrix44
import glm

import moderngl
import moderngl_window
Expand All @@ -25,8 +25,8 @@ def __init__(self, **kwargs):
def render(self, time: float, frametime: float):
self.ctx.enable_only(moderngl.DEPTH_TEST | moderngl.CULL_FACE)

translation = Matrix44.from_translation((0, 0, -1.5))
rotation = Matrix44.from_eulers((0, 0, 0))
translation = glm.translate(glm.vec3(0, 0, -1.5))
rotation = glm.mat4(glm.quat(glm.vec3(0, 0, 0)))
model_matrix = translation * rotation
camera_matrix = self.camera.matrix * model_matrix

Expand Down
18 changes: 9 additions & 9 deletions examples/cubes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import moderngl
import moderngl_window

from pyrr import Matrix44
import glm


class Cubes(moderngl_window.WindowConfig):
Expand All @@ -31,35 +31,35 @@ def __init__(self, **kwargs):

def render(self, time, frame_time):
self.ctx.enable_only(moderngl.DEPTH_TEST | moderngl.CULL_FACE)
rot = Matrix44.from_eulers((time, time/2, time/3))
rot = glm.mat4(glm.quat(glm.vec3(time, time/2, time/3)))

# Box 1
view = Matrix44.from_translation((-5, 2, -10), dtype='f4')
view = glm.translate(glm.vec3(-5, 2, -10))
self.box_v3.draw(self.projection, view * rot)

# Box 2
view = Matrix44.from_translation((0, 2, -10), dtype='f4')
view = glm.translate(glm.vec3(0, 2, -10))
self.box_c3_v3.draw(self.projection, view * rot)

# Box 3
view = Matrix44.from_translation((5, 2, -10), dtype='f4')
view = glm.translate(glm.vec3(5, 2, -10))
self.box_n3_v3.draw(self.projection, view * rot)

# Box 4
view = Matrix44.from_translation((-5, -2, -10), dtype='f4')
view = glm.translate(glm.vec3(-5, -2, -10))
self.box_t2_v3.draw(self.projection, view * rot)

# Box 5
view = Matrix44.from_translation((0, -2, -10), dtype='f4')
view = glm.translate(glm.vec3(0, -2, -10))
self.box_t2_c3_v3.draw(self.projection, view * rot)

# Box 6
view = Matrix44.from_translation((5, -2, -10), dtype='f4')
view = glm.translate(glm.vec3(5, -2, -10))
self.box_t2_n3_v3.draw(self.projection, view * rot)

def resize(self, width, height):
self.ctx.viewport = 0, 0, width, height
self.projection = Matrix44.perspective_projection(45, width / height, 1, 50, dtype='f4')
self.projection = glm.perspective(glm.radians(45), width / height, 1, 50)


if __name__ == '__main__':
Expand Down
Loading

0 comments on commit e009da6

Please sign in to comment.