From 7d7c5055d4e2d4ad30ffa37fc1f8f6a725530f76 Mon Sep 17 00:00:00 2001 From: Tristan Schulz Date: Mon, 26 Dec 2022 01:47:17 +0100 Subject: [PATCH] Fixed opengl Surface with reordering initialization process (#3089) * Fixed opengl Surface with disabling refresh of shader wrapper id in constructor * Reordered Elements in constructor of opengl_vectorized_mobject to fix initialization dependency issues --- manim/mobject/opengl/opengl_mobject.py | 5 ++++- manim/mobject/opengl/opengl_surface.py | 3 ++- manim/mobject/opengl/opengl_vectorized_mobject.py | 12 ++++++------ tests/opengl/test_opengl_surface.py | 14 ++++++++++++++ 4 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 tests/opengl/test_opengl_surface.py diff --git a/manim/mobject/opengl/opengl_mobject.py b/manim/mobject/opengl/opengl_mobject.py index 864326497e..69a472f444 100644 --- a/manim/mobject/opengl/opengl_mobject.py +++ b/manim/mobject/opengl/opengl_mobject.py @@ -2627,12 +2627,15 @@ def set_color_by_xyz_func( # For shader data def refresh_shader_wrapper_id(self): - self.shader_wrapper.refresh_id() + self.get_shader_wrapper().refresh_id() return self def get_shader_wrapper(self): from manim.renderer.shader_wrapper import ShaderWrapper + # if hasattr(self, "__shader_wrapper"): + # return self.__shader_wrapper + self.shader_wrapper = ShaderWrapper( vert_data=self.get_shader_data(), vert_indices=self.get_shader_vert_indices(), diff --git a/manim/mobject/opengl/opengl_surface.py b/manim/mobject/opengl/opengl_surface.py index 30641ca2f1..64cbd43f5b 100644 --- a/manim/mobject/opengl/opengl_surface.py +++ b/manim/mobject/opengl/opengl_surface.py @@ -98,6 +98,7 @@ def __init__( # can crop up in the shaders. self.epsilon = epsilon + self.triangle_indices = None super().__init__( color=color, opacity=opacity, @@ -127,7 +128,7 @@ def init_points(self): # - Those generated by values nudged by du # - Those generated by values nudged by dv point_lists = [] - for (du, dv) in [(0, 0), (self.epsilon, 0), (0, self.epsilon)]: + for du, dv in [(0, 0), (self.epsilon, 0), (0, self.epsilon)]: uv_grid = np.array([[[u + du, v + dv] for v in v_range] for u in u_range]) point_grid = np.apply_along_axis(lambda p: self.uv_func(*p), 2, uv_grid) point_lists.append(point_grid.reshape((nu * nv, dim))) diff --git a/manim/mobject/opengl/opengl_vectorized_mobject.py b/manim/mobject/opengl/opengl_vectorized_mobject.py index 2c12462b0a..8dc174e12d 100644 --- a/manim/mobject/opengl/opengl_vectorized_mobject.py +++ b/manim/mobject/opengl/opengl_vectorized_mobject.py @@ -137,6 +137,12 @@ def __init__( self.needs_new_triangulation = True self.triangulation = np.zeros(0, dtype="i4") self.orientation = 1 + self.fill_data = None + self.stroke_data = None + self.fill_shader_wrapper = None + self.stroke_shader_wrapper = None + self.init_shader_data() + super().__init__(**kwargs) self.refresh_unit_normal() @@ -145,12 +151,6 @@ def __init__( if stroke_color: self.stroke_color = Color(stroke_color) - self.fill_data = None - self.stroke_data = None - self.fill_shader_wrapper = None - self.stroke_shader_wrapper = None - self.init_shader_data() - def get_group_class(self): return OpenGLVGroup diff --git a/tests/opengl/test_opengl_surface.py b/tests/opengl/test_opengl_surface.py new file mode 100644 index 0000000000..d6897691d4 --- /dev/null +++ b/tests/opengl/test_opengl_surface.py @@ -0,0 +1,14 @@ +import numpy as np + +from manim.mobject.opengl.opengl_surface import OpenGLSurface +from manim.mobject.opengl.opengl_three_dimensions import OpenGLSurfaceMesh + + +def test_surface_initialization(using_opengl_renderer): + surface = OpenGLSurface( + lambda u, v: (u, v, u * np.sin(v) + v * np.cos(u)), + u_range=(-3, 3), + v_range=(-3, 3), + ) + + mesh = OpenGLSurfaceMesh(surface)