From 850a3ac48046e3630524c67ecac5e2f26d499b3f Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Tue, 8 Oct 2019 20:55:57 +0200 Subject: Improve mouse monitoring, add 3d view shift support --- channel_3d_gl_interop.py | 29 ++++++---------------- channel_3d_volumetric_rendering_gl_interop.py | 35 +++++++++++++++------------ ldc_3d_gl_interop.py | 14 +++++------ utility/mouse.py | 25 ++++++++++++------- utility/projection.py | 7 ++++++ 5 files changed, 58 insertions(+), 52 deletions(-) diff --git a/channel_3d_gl_interop.py b/channel_3d_gl_interop.py index 569580d..535d595 100644 --- a/channel_3d_gl_interop.py +++ b/channel_3d_gl_interop.py @@ -18,7 +18,7 @@ from geometry.cylinder import Cylinder from utility.projection import Projection, Rotation from utility.opengl import MomentsVertexBuffer -from utility.mouse import MouseDragMonitor +from utility.mouse import MouseDragMonitor, MouseScrollMonitor lattice_x = 256 lattice_y = 64 @@ -273,24 +273,11 @@ def on_display(): glutSwapBuffers() -mouse_monitor = MouseDragMonitor( - GLUT_LEFT_BUTTON, - drag_callback = lambda dx, dy: rotation.update(0.005*dy, 0.005*dx), - zoom_callback = lambda zoom: projection.update_distance(5*zoom)) - -def on_keyboard(key, x, y): - global rotation - - x = { - b'w': -numpy.pi/10, - b's': numpy.pi/10 - }.get(key, 0.0) - z = { - b'a': numpy.pi/10, - b'd': -numpy.pi/10 - }.get(key, 0.0) - - rotation.update(x,z) +mouse_monitors = [ + MouseDragMonitor(GLUT_LEFT_BUTTON, lambda dx, dy: rotation.update(0.005*dy, 0.005*dx)), + MouseDragMonitor(GLUT_RIGHT_BUTTON, lambda dx, dy: rotation.shift(0.25*dx, 0.25*dy)), + MouseScrollMonitor(lambda zoom: projection.update_distance(5*zoom)) +] def on_timer(t): glutTimerFunc(t, on_timer, t) @@ -298,8 +285,8 @@ def on_timer(t): glutDisplayFunc(on_display) glutReshapeFunc(lambda w, h: projection.update_ratio(w, h)) -glutMouseFunc(mouse_monitor.on_mouse) -glutMotionFunc(mouse_monitor.on_mouse_move) +glutMouseFunc(lambda *args: list(map(lambda m: m.on_mouse(*args), mouse_monitors))) +glutMotionFunc(lambda *args: list(map(lambda m: m.on_mouse_move(*args), mouse_monitors))) glutTimerFunc(10, on_timer, 10) glutMainLoop() diff --git a/channel_3d_volumetric_rendering_gl_interop.py b/channel_3d_volumetric_rendering_gl_interop.py index 91a470e..7c78d9f 100644 --- a/channel_3d_volumetric_rendering_gl_interop.py +++ b/channel_3d_volumetric_rendering_gl_interop.py @@ -18,7 +18,7 @@ from geometry.cylinder import Cylinder from utility.projection import Projection, Rotation from utility.opengl import MomentsTexture -from utility.mouse import MouseDragMonitor +from utility.mouse import MouseDragMonitor, MouseScrollMonitor lattice_x = 256 lattice_y = 64 @@ -46,11 +46,17 @@ def get_cavity_material_map(g): (Box(2*g.size_x//20, 2.5*g.size_x//20, 0, g.size_y, 0, g.size_z), 5), (Sphere(2.5*g.size_x//20, g.size_y//2, g.size_z//2, 10), 1), - # obstacle - (Cylinder(6*g.size_x//20, 0, 1*g.size_z//5, 4, l = g.size_y), 5), - (Cylinder(6*g.size_x//20, 0, 2*g.size_z//5, 4, l = g.size_y), 5), - (Cylinder(6*g.size_x//20, 0, 3*g.size_z//5, 4, l = g.size_y), 5), - (Cylinder(6*g.size_x//20, 0, 4*g.size_z//5, 4, l = g.size_y), 5), + (Box(6.0*g.size_x//20, 6.5*g.size_x//20, 0, g.size_y, 0, g.size_z), 5), + (Sphere(6.5*g.size_x//20, 2*g.size_y//3, 2*g.size_z//3, 10), 1), + (Sphere(6.5*g.size_x//20, 1*g.size_y//3, 1*g.size_z//3, 10), 1), + + (Box(10.0*g.size_x//20, 10.5*g.size_x//20, 0, g.size_y, 0, g.size_z), 5), + (Sphere(10.5*g.size_x//20, 1*g.size_y//3, 2*g.size_z//3, 10), 1), + (Sphere(10.5*g.size_x//20, 2*g.size_y//3, 1*g.size_z//3, 10), 1), + + (Box(14.0*g.size_x//20, 14.5*g.size_x//20, 0, g.size_y, 0, g.size_z), 5), + (Sphere(14.5*g.size_x//20, 2*g.size_y//3, 2*g.size_z//3, 10), 1), + (Sphere(14.5*g.size_x//20, 1*g.size_y//3, 1*g.size_z//3, 10), 1), (lambda x, y, z: x == 0 or x == g.size_x-1 or y == 0 or y == g.size_y-1 or @@ -200,9 +206,7 @@ vec3 trace(vec3 pos, vec3 ray) { if (length(sample_pos) < sqrt(3.1)) { const vec4 data = texture(moments, sample_pos); if (data[3] != 1.0) { - const vec3 c = curl(pos + t*ray_length*ray); - - color += delta * blueBlackRedPalette(0.5+clamp(c.y,-1.0,1.0)); + color += delta * palette(length(data.yzw) / $inflow); } else { const vec3 n = data.xyz; // recover surface normal const float brightness = clamp(dot(n, ray), 0, 1); @@ -292,10 +296,11 @@ def on_display(): glutSwapBuffers() -mouse_monitor = MouseDragMonitor( - GLUT_LEFT_BUTTON, - drag_callback = lambda dx, dy: rotation.update(0.005*dy, 0.005*dx), - zoom_callback = lambda zoom: projection.update_distance(5*zoom)) +mouse_monitors = [ + MouseDragMonitor(GLUT_LEFT_BUTTON, lambda dx, dy: rotation.update(0.005*dy, 0.005*dx)), + MouseDragMonitor(GLUT_RIGHT_BUTTON, lambda dx, dy: rotation.shift(0.25*dx, 0.25*dy)), + MouseScrollMonitor(lambda zoom: projection.update_distance(5*zoom)) +] def on_timer(t): glutTimerFunc(t, on_timer, t) @@ -303,8 +308,8 @@ def on_timer(t): glutDisplayFunc(on_display) glutReshapeFunc(lambda w, h: projection.update_ratio(w, h)) -glutMouseFunc(mouse_monitor.on_mouse) -glutMotionFunc(mouse_monitor.on_mouse_move) +glutMouseFunc(lambda *args: list(map(lambda m: m.on_mouse(*args), mouse_monitors))) +glutMotionFunc(lambda *args: list(map(lambda m: m.on_mouse_move(*args), mouse_monitors))) glutTimerFunc(10, on_timer, 10) glutMainLoop() diff --git a/ldc_3d_gl_interop.py b/ldc_3d_gl_interop.py index a858665..a8f6f8d 100644 --- a/ldc_3d_gl_interop.py +++ b/ldc_3d_gl_interop.py @@ -14,7 +14,7 @@ from OpenGL.GL import shaders from utility.projection import Projection, Rotation from utility.opengl import MomentsVertexBuffer -from utility.mouse import MouseDragMonitor +from utility.mouse import MouseDragMonitor, MouseScrollMonitor lattice_x = 64 lattice_y = 96 @@ -192,10 +192,10 @@ def on_display(): glutSwapBuffers() -mouse_monitor = MouseDragMonitor( - GLUT_LEFT_BUTTON, - drag_callback = lambda dx, dy: rotation.update(0.005*dy, 0.005*dx), - zoom_callback = lambda zoom: projection.update_distance(5*zoom)) +mouse_monitors = [ + MouseDragMonitor(GLUT_LEFT_BUTTON, lambda dx, dy: rotation.update(0.005*dy, 0.005*dx)), + MouseScrollMonitor(lambda zoom: projection.update_distance(5*zoom)) +] def on_timer(t): glutTimerFunc(t, on_timer, t) @@ -203,8 +203,8 @@ def on_timer(t): glutDisplayFunc(on_display) glutReshapeFunc(lambda w, h: projection.update_ratio(w, h)) -glutMouseFunc(mouse_monitor.on_mouse) -glutMotionFunc(mouse_monitor.on_mouse_move) +glutMouseFunc(lambda *args: list(map(lambda m: m.on_mouse(*args), mouse_monitors))) +glutMotionFunc(lambda *args: list(map(lambda m: m.on_mouse_move(*args), mouse_monitors))) glutTimerFunc(10, on_timer, 10) glutMainLoop() diff --git a/utility/mouse.py b/utility/mouse.py index 29d660d..5d7dd5d 100644 --- a/utility/mouse.py +++ b/utility/mouse.py @@ -1,11 +1,10 @@ from OpenGL.GLUT import * class MouseDragMonitor: - def __init__(self, button, drag_callback, zoom_callback): + def __init__(self, button, callback): self.button = button self.active = False - self.drag_callback = drag_callback - self.zoom_callback = zoom_callback + self.callback = callback def on_mouse(self, button, state, x, y): if button == self.button: @@ -13,15 +12,23 @@ class MouseDragMonitor: self.last_x = x self.last_y = y - if button == 3: - self.zoom_callback(-1.0) - elif button == 4: - self.zoom_callback(1.0) - def on_mouse_move(self, x, y): if self.active: delta_x = self.last_x - x delta_y = y - self.last_y self.last_x = x self.last_y = y - self.drag_callback(delta_x, delta_y) + self.callback(delta_x, delta_y) + +class MouseScrollMonitor: + def __init__(self, callback): + self.callback = callback + + def on_mouse(self, button, state, x, y): + if button == 3: + self.callback(-1.0) + elif button == 4: + self.callback(1.0) + + def on_mouse_move(self, x, y): + pass diff --git a/utility/projection.py b/utility/projection.py index 335e488..1750fac 100644 --- a/utility/projection.py +++ b/utility/projection.py @@ -39,6 +39,13 @@ class Rotation: self.rotation_x = quaternion.Quaternion() self.update(x,z) + def shift(self, x, z): + self.matrix = numpy.matmul( + self.matrix, + matrix44.create_from_translation([x,0,z]) + ) + self.inverse_matrix = numpy.linalg.inv(self.matrix) + def update(self, x, z): rotation_x = quaternion.Quaternion(quaternion.create_from_eulers([x,0,0])) rotation_z = self.rotation_x.conjugate.cross( -- cgit v1.2.3