Skip to content

Commit

Permalink
moderngl#176: First pass replacing pyrr with pyglm, untested. Dumpin…
Browse files Browse the repository at this point in the history
…g code.
  • Loading branch information
klattimer committed Sep 1, 2023
1 parent 200f2b9 commit 2677abb
Show file tree
Hide file tree
Showing 25 changed files with 120 additions and 130 deletions.
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
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
21 changes: 10 additions & 11 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(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',
[0.5, 0.5, 0.5, 1.0]]
)
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(self.lightpos + scene_pos))
self.sun.render(self.sun_prog)

# --- PASS 3: Debug ---
Expand Down
21 changes: 10 additions & 11 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(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',
[0.5, 0.5, 0.5, 1.0]]
)
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(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
14 changes: 5 additions & 9 deletions examples/advanced/voxel_cubes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@

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


class CubeVoxel(CameraWindow):
Expand Down Expand Up @@ -129,11 +127,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',
-self._size[0] * 2)
)
mat = camera_matrix * translate
self.voxel_wireframe_prog["m_proj"].write(projection_matrix)
Expand All @@ -144,14 +141,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',
-self._size[0] * 2)
)
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(45, width / height, 1, 50)


if __name__ == '__main__':
Expand Down
18 changes: 9 additions & 9 deletions examples/drag_drop_file_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import moderngl
import moderngl_window

from pyrr import Matrix44
import glm


class Cubes(moderngl_window.WindowConfig):
Expand All @@ -35,35 +35,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 top left
view = Matrix44.from_translation((-5, 2, -10), dtype='f4')
view = glm.translate(glm.vec3(-5, 2, -10))
self.box_top_left.draw(self.projection, view * rot)

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

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

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

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

# Box bottom right
view = Matrix44.from_translation((5, -2, -10), dtype='f4')
view = glm.translate(glm.vec3(5, -2, -10))
self.box_bottom_right.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(45, width / height, 1, 50, dtype='f4')

def _load_texture(self, path):
tex = self.load_texture_2d(os.path.relpath(path, self.resource_dir))
Expand Down
4 changes: 2 additions & 2 deletions examples/geometry_bbox.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pyrr import Matrix44
import glm

import moderngl_window
from moderngl_window import geometry
Expand All @@ -18,7 +18,7 @@ def __init__(self, **kwargs):
self.prog['color'].value = (1, 1, 1)
self.prog['bb_min'].value = (-2, -2, -2)
self.prog['bb_max'].value = (2, 2, 2)
self.prog['m_model'].write(Matrix44.from_translation([0.0, 0.0, -8.0], dtype='f4'))
self.prog['m_model'].write(glm.translate(glm.vec3[0.0, 0.0, -8.0]))

def render(self, time: float, frame_time: float):
self.ctx.clear()
Expand Down
6 changes: 3 additions & 3 deletions examples/geometry_cube.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from pyrr import Matrix44, Vector3
import glm

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

rotation = Matrix44.from_eulers((time, time, time), dtype='f4')
translation = Matrix44.from_translation((0.0, 0.0, -3.5), dtype='f4')
rotation = glm.mat4(glm.quat(glm.vec3(time, time, time)))
translation = glm.translate(glm.vec3(0.0, 0.0, -3.5))
modelview = translation * rotation

self.prog['m_proj'].write(self.camera.projection.matrix)
Expand Down
4 changes: 2 additions & 2 deletions examples/geometry_cube_instanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pathlib import Path

import numpy
from pyrr import Matrix44
import glm
import moderngl
import moderngl_window
from moderngl_window import geometry
Expand All @@ -26,7 +26,7 @@ def __init__(self, **kwargs):
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(Matrix44.identity(dtype='f4'))
self.prog['m_model'].write(glm.mat4())

# Generate per instance data represeting a grid of cubes
N = 100
Expand Down
Loading

0 comments on commit 2677abb

Please sign in to comment.