Push pixel_coords_to_space_coords to Window

This commit is contained in:
Grant Sanderson 2021-08-24 11:26:43 -07:00
parent 8645894255
commit c635f19f2a
2 changed files with 12 additions and 12 deletions

View file

@ -316,17 +316,6 @@ class Camera(object):
self.frame.set_height(frame_height)
self.frame.set_width(frame_width)
def pixel_coords_to_space_coords(self, px, py, relative=False):
pw, ph = self.fbo.size
fw, fh = self.get_frame_shape()
fc = self.get_frame_center()
if relative:
return 2 * np.array([px / pw, py / ph, 0])
else:
# Only scale wrt one axis
scale = fh / ph
return fc + scale * np.array([(px - pw / 2), (py - ph / 2), 0])
# Rendering
def capture(self, *mobjects, **kwargs):
self.refresh_perspective_uniforms()

View file

@ -1,3 +1,4 @@
import numpy as np
import moderngl_window as mglw
from moderngl_window.context.pyglet.window import Window as PygletWindow
from moderngl_window.timers.clock import Timer
@ -59,7 +60,17 @@ class Window(PygletWindow):
# Delegate event handling to scene
def pixel_coords_to_space_coords(self, px, py, relative=False):
return self.scene.camera.pixel_coords_to_space_coords(px, py, relative)
pw, ph = self.size
fw, fh = self.scene.camera.get_frame_shape()
fc = self.scene.camera.get_frame_center()
if relative:
return np.array([px / pw, py / ph, 0])
else:
return np.array([
fc[0] + px * fw / pw - fw / 2,
fc[1] + py * fh / ph - fh / 2,
0
])
def on_mouse_motion(self, x, y, dx, dy):
super().on_mouse_motion(x, y, dx, dy)