From 5d34cee014e465105a2dee028beba7ead0b08c85 Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Mon, 8 Jun 2020 17:55:41 -0700 Subject: [PATCH] Move frame resizing to get_gl_Position so that the screen rotation matrix remains det 1 --- manimlib/camera/camera.py | 45 ++++++++----------- manimlib/shaders/get_gl_Position.glsl | 5 ++- manimlib/shaders/image_vert.glsl | 2 +- .../shaders/quadratic_bezier_fill_geom.glsl | 2 +- .../shaders/quadratic_bezier_stroke_geom.glsl | 2 +- .../scale_and_shift_point_for_frame.glsl | 2 +- manimlib/shaders/simple_vert.glsl | 2 +- manimlib/shaders/surface_vert.glsl | 2 +- manimlib/shaders/textured_surface_vert.glsl | 2 +- 9 files changed, 28 insertions(+), 36 deletions(-) diff --git a/manimlib/camera/camera.py b/manimlib/camera/camera.py index 6e7bdfff..51ef8d81 100644 --- a/manimlib/camera/camera.py +++ b/manimlib/camera/camera.py @@ -26,10 +26,7 @@ from manimlib.utils.space_ops import quaternion_mult class CameraFrame(Mobject): CONFIG = { - # TODO, package these togeher - # Also, interpolation doesn't yet do anything with them. - "width": FRAME_WIDTH, - "height": FRAME_HEIGHT, + "frame_shape": (FRAME_WIDTH, FRAME_HEIGHT), "center_point": ORIGIN, # Theta, phi, gamma "euler_angles": [0, 0, 0], @@ -37,7 +34,10 @@ class CameraFrame(Mobject): } def init_points(self): - self.points = np.array([self.center_point]) + self.points = np.array([UR, UL, DL, DR]) + self.set_width(self.frame_shape[0], stretch=True) + self.set_height(self.frame_shape[1], stretch=True) + self.move_to(self.center_point) self.euler_angles = np.array(self.euler_angles, dtype='float64') def to_default_state(self): @@ -53,8 +53,6 @@ class CameraFrame(Mobject): result[:3, 3] = -self.get_center().T # Rotate based on camera orientation result[:3, :3] = np.dot(self.get_inverse_camera_rotation_matrix(), result[:3, :3]) - # Scale to have height 2 (matching the height of the box [-1, 1]^2) - result *= 2 / self.height return result def get_inverse_camera_rotation_matrix(self): @@ -98,28 +96,21 @@ class CameraFrame(Mobject): self.euler_angles[2] += dgamma return self - def scale(self, scale_factor, **kwargs): - # TODO, handle about_point and about_edge? - self.height *= scale_factor - self.width *= scale_factor - return self + # def scale(self, scale_factor, **kwargs): + # # TODO, handle about_point and about_edge? + # self.frame_dimensions *= scale_factor + # return self - def set_height(self, height): - self.height = height - return self + # def set_height(self, height, stretch=False): + # self.height = height + # return self - def set_width(self, width): - self.width = width - return self + # def set_width(self, width, stretch=False): + # self.width = width + # return self - def get_height(self): - return self.height - - def get_width(self): - return self.width - - def get_center(self): - return self.points[0] + def get_shape(self): + return (self.get_width(), self.get_height()) def get_focal_distance(self): return self.focal_distance @@ -389,7 +380,7 @@ class Camera(object): transformed_light = np.dot(transform, [*light, 1])[:3] mapping = { 'to_screen_space': tuple(transform.T.flatten()), - 'aspect_ratio': (pw / ph), # AR based on pixel shape + 'frame_shape': self.frame.get_shape(), 'focal_distance': self.frame.get_focal_distance(), 'anti_alias_width': anti_alias_width, 'light_source_position': tuple(transformed_light), diff --git a/manimlib/shaders/get_gl_Position.glsl b/manimlib/shaders/get_gl_Position.glsl index 071ebb52..3b3a023b 100644 --- a/manimlib/shaders/get_gl_Position.glsl +++ b/manimlib/shaders/get_gl_Position.glsl @@ -1,9 +1,10 @@ // Assumes the following uniforms exist in the surrounding context: -// uniform float aspect_ratio; +// uniform vec2 frame_shape; // uniform float focal_distance; vec4 get_gl_Position(vec3 point){ - point.x /= aspect_ratio; + point.x *= 2 / frame_shape.x; + point.y *= 2 / frame_shape.y; point.z /= focal_distance; point.xy /= max(1 - point.z, 0); // Todo, does this discontinuity add weirdness? Theoretically, by this point, diff --git a/manimlib/shaders/image_vert.glsl b/manimlib/shaders/image_vert.glsl index f7e48932..b27e89c5 100644 --- a/manimlib/shaders/image_vert.glsl +++ b/manimlib/shaders/image_vert.glsl @@ -1,6 +1,6 @@ #version 330 -uniform float aspect_ratio; +uniform vec2 frame_shape; uniform float anti_alias_width; uniform mat4 to_screen_space; uniform float focal_distance; diff --git a/manimlib/shaders/quadratic_bezier_fill_geom.glsl b/manimlib/shaders/quadratic_bezier_fill_geom.glsl index c3cb90a9..be501aa2 100644 --- a/manimlib/shaders/quadratic_bezier_fill_geom.glsl +++ b/manimlib/shaders/quadratic_bezier_fill_geom.glsl @@ -5,7 +5,7 @@ layout (triangle_strip, max_vertices = 5) out; uniform float anti_alias_width; // Needed for get_gl_Position -uniform float aspect_ratio; +uniform vec2 frame_shape; uniform float focal_distance; uniform vec3 light_source_position; diff --git a/manimlib/shaders/quadratic_bezier_stroke_geom.glsl b/manimlib/shaders/quadratic_bezier_stroke_geom.glsl index 73b246fb..a8c41ad8 100644 --- a/manimlib/shaders/quadratic_bezier_stroke_geom.glsl +++ b/manimlib/shaders/quadratic_bezier_stroke_geom.glsl @@ -4,7 +4,7 @@ layout (triangles) in; layout (triangle_strip, max_vertices = 5) out; // Needed for get_gl_Position -uniform float aspect_ratio; +uniform vec2 frame_shape; uniform float focal_distance; uniform float anti_alias_width; uniform vec3 light_source_position; diff --git a/manimlib/shaders/scale_and_shift_point_for_frame.glsl b/manimlib/shaders/scale_and_shift_point_for_frame.glsl index 4c13eced..616ca622 100644 --- a/manimlib/shaders/scale_and_shift_point_for_frame.glsl +++ b/manimlib/shaders/scale_and_shift_point_for_frame.glsl @@ -1,5 +1,5 @@ // Assumes the following uniforms exist in the surrounding context: -// uniform float aspect_ratio; +// uniform vec2 frame_shape; // TODO, rename vec3 get_gl_Position(vec3 point){ diff --git a/manimlib/shaders/simple_vert.glsl b/manimlib/shaders/simple_vert.glsl index c5d8c546..e9f0d0e9 100644 --- a/manimlib/shaders/simple_vert.glsl +++ b/manimlib/shaders/simple_vert.glsl @@ -1,6 +1,6 @@ #version 330 -uniform float aspect_ratio; +uniform vec2 frame_shape; uniform float anti_alias_width; uniform mat4 to_screen_space; uniform float focal_distance; diff --git a/manimlib/shaders/surface_vert.glsl b/manimlib/shaders/surface_vert.glsl index 1536e809..93a7dca9 100644 --- a/manimlib/shaders/surface_vert.glsl +++ b/manimlib/shaders/surface_vert.glsl @@ -1,6 +1,6 @@ #version 330 -uniform float aspect_ratio; +uniform vec2 frame_shape; uniform float anti_alias_width; uniform mat4 to_screen_space; uniform float focal_distance; diff --git a/manimlib/shaders/textured_surface_vert.glsl b/manimlib/shaders/textured_surface_vert.glsl index dbb0518b..b39b7620 100644 --- a/manimlib/shaders/textured_surface_vert.glsl +++ b/manimlib/shaders/textured_surface_vert.glsl @@ -1,6 +1,6 @@ #version 330 -uniform float aspect_ratio; +uniform vec2 frame_shape; uniform float anti_alias_width; uniform mat4 to_screen_space; uniform float focal_distance;