Repository navigation
Conversation
behackl
force-pushed
the
refactor/unified-mobjects
branch
from
November 23, 2026 21:06
a804f3f to
b07ec68
Compare
The OpenGL renderer is about to use the same camera classes as the Cairo renderer. Under it, ConvertToOpenGL turns the camera frame (a ScreenRectangle) into an OpenGL mobject, like the scene's other mobjects, which is what users who move the frame relative to their mobjects need. The frame's fast center computation relied on VMobject internals; it now steps aside for an OpenGL frame. ThreeDCamera's light source was a Point, which is broken as an OpenGL mobject; it is now a plain Mobject with one point, which ConvertToOpenGL leaves alone, so both renderers read the same light source.
The OpenGL renderer had its own camera, OpenGLCamera, itself an OpenGL mobject with euler angles, a focal distance relative to the frame height and a separate view matrix for custom-shader meshes. Scenes branched on it in a dozen places (ThreeDScene, ZoomedScene, interaction, the Manager, hashing). The OpenGL renderer now takes camera_class or camera like the Cairo renderer and computes its shader uniforms from Camera, MovingCamera or ThreeDCamera: frame center, the ThreeDCamera rotation matrix, the frame shape divided by the zoom, and the focal distance, which is what ThreeDCamera.project_points does on the CPU. Cameras without rotation draw orthographically. Custom-shader meshes get view and projection matrices from the same camera, so they stay in line with mobjects. OpenGLCamera is removed (clean break; the OpenGL mobject classes follow later in this series). - ThreeDScene runs its ThreeDCamera code on both renderers. OpenGL mobject classes still get their fixed-in-frame and fixed-orientation uniforms set until fixing in frame is a field of every mobject. - Interactive mode drives the semantic camera: left drag rotates a ThreeDCamera (pans a flat camera), right drag and shift-move pan, scrolling zooms the frame, r restores the camera saved when interactive_embed starts. - OpenGL scenes now use the frame height derived from the pixel aspect ratio, as Cairo does; OpenGLCamera used config.frame_height. That moves edges by a fraction of a pixel: the OpenGL control data of test_Circle is regenerated. test_FixedMobjects3D never added its mobjects; it now builds a ThreeDScene with fixed mobjects. - The camera guide describes one camera for both renderers. - New tests: both renderers place 3D and moving-camera content in the same place; mouse controls move the camera.
Transform, Animation, AnimationGroup, FadeTransform and the transform-matching animations picked their OpenGL code paths (and OpenGL helper groups) whenever config.renderer was OpenGL. The next step lets the OpenGL renderer draw mobjects of the common classes, so the renderer no longer tells which classes an animation deals with. They now branch on whether the mobjects are of the OpenGL classes. The branches go with those classes later in this series. A test pins that the camera frame of a MovingCameraScene animates under the OpenGL renderer, which an intermediate version of this series broke.
The OpenGL renderer is learning to draw the common mobject classes. While it does, MANIM_OPENGL_COMMON_MOBJECTS=1 turns ConvertToOpenGL off: OpenGL scenes then contain the same mobjects as Cairo scenes. The switch is a development aid for this series and goes when the OpenGL classes do. Code that branched on config.renderer but depends on the mobject classes (VGroup's accepted types, text and TeX colour initialisation, SVG preprocessing for triangulation, surfaces, boolean operations, the scene's family and add/remove bookkeeping, the mouse points, ...) now asks uses_opengl_mobjects(), which is the old condition unless the switch is on. Scenes still keep Object3D meshes apart on the OpenGL renderer, and the static/moving split and interaction checks still follow the renderer. Nothing changes without the switch.
With the common mobject classes (the switch from the previous commit), the OpenGL renderer draws extract() snapshots, in the same order as Cairo and with the camera's sort for 3D. - Geometry: cubic curves are converted to quadratic pieces on the CPU, vectorized over all paths that changed, with an error bound of 0.1 px (sqrt(3)/36 |P3 - 3P2 + 3P1 - P0| / k^3 for k pieces). The result is cached per mobject by the version of the snapshot's "geometry" slot (points and point tolerance), so a changed tolerance splits subpaths anew. - Strokes: the existing quadratic stroke shader, with its antialiasing filtering both edges of the stroke (sub-pixel lines were half opaque at their center, 50% too heavy). Background stroke, fill, stroke, as Cairo. - Fill: by winding number, ported from the earlier fill experiment: two triangles per piece accumulate signed coverage in a multisampled float buffer, resolved once per group. No triangulation; holes, open subpaths and self-intersections follow from the winding. Opacity is accumulated separately, weighted by coverage, so each path's opacity is applied once and paths of different opacity can share a group. - Colours follow Cairo's semantics: a paint's colors are stops of a linear gradient between its two points, and the shading of a LegacyShaded material applies as on Cairo. Both renderers get them from one function, manim.renderer._paint, so neither renderer package imports the other. - Batching: consecutive paths whose drawing cannot interact on screen (projected pixel boxes; stroke-only paths always) are drawn together in three passes; the fill is clipped to the group's pixel box. Against Cairo on reference content (text, equations, holes, open subpaths, gradients, joined strokes, thin lines, overlapping translucent shapes), ink is within 5% and the mean pixel difference below 1%. The stroke antialiasing fix also applies to the OpenGL mobject classes, which share the shader: the OpenGL control data of test_FixedMobjects3D is regenerated. Known gaps: round and square caps, fixed-in-frame mobjects, point clouds and images (next steps). A group's fill is limited to its rectangle of pixels by the viewport of a triangle that covers it, for clearing the accumulation buffer and for resolving it, and the scissor test is not used. With the scissor test, Apple's software renderer (macOS without a GPU, as on CI runners) lost whole fills for some rectangles on the multisampled float buffer: two squares side by side came out as none, one or two depending on their distance. A test draws them at four such distances. Graphical tests drawn with OpenGL tolerate ten times as many differing values as Cairo's (0.01 % of a frame): GPUs, Mesa's llvmpipe and softpipe and Apple's software renderer antialias a few edge pixels differently (40 values by 2/255 for FixedMobjects3D on llvmpipe).
With the common mobject classes, the OpenGL renderer now draws every snapshot payload, in extraction order (consecutive paths still batched): - Point clouds as square points of whole pixels, stroke_width wide and scaled with the resolution, as the Cairo renderer draws them. - Images as a textured quad on their four corners. Textures are cached per mobject by the version of the snapshot's "pixels" slot, and never for nodes without a key: image classes that compute their pixels are uploaded anew every frame; nearest resampling maps to nearest filtering, the others to mipmapped linear filtering (Cairo resamples bicubically, so smooth images differ slightly at their edges). - Camera views (ImageMobjectFromCamera, used by MultiCamera and ZoomedScene): each view's camera draws the scene into an offscreen texture first, with its own drawing state, and the main pass composites the texture at the view's corners. The view's own mobject is excluded, as on Cairo. The drawing code takes a DrawTarget (framebuffer, size, camera, view, uniforms) instead of reading the renderer's main frame, and the renderer computes camera uniforms for any view (camera_uniforms). Tests compare point clouds, nearest and smooth images and a ZoomedScene with Cairo, and redraw an image class that computes its pixels.
Mobjects fixed in frame or in their orientation carry that in their snapshot (RNode.fixed_in_frame, fixed_orientation_center); the Cairo renderer draws them as before. The OpenGL renderer now does too: fixed nodes are drawn with the uniforms of a camera at its default frame, and fixed-orientation nodes are shifted by project(center) - center, as on Cairo. A title stays put while a MovingCamera pans, on both renderers. Tests compare a ThreeDScene with a fixed title and label, and a panned frame with a fixed square, with Cairo.
The OpenGL mobject classes had smooth, lit surfaces (OpenGLSurface, OpenGLTexturedSurface) that the common classes lack; Surface is a group of polygons. These are their replacement, on the common Mobject. MeshMobject is a triangle mesh: points, triangles, one colour or one per vertex, an optional texture with uv coordinates, a material and depth_test, all fields. set_color_by_xyz_func colours the vertices on the CPU (the old OpenGL method took GLSL code). Meshes transform into meshes with the same triangles, and Create shows their triangles in order. get_wireframe returns the edges as lines. ParametricSurface builds a mesh from a function of (u, v): a smooth grid, or with flat=True one quad per face so faces can have their own colour (set_fill_by_checkerboard). It has set_fill_by_value, and Create grows it along v with the triangles kept, so it can be transformed while partial. A texture spans the parameter domain, u left to right and v bottom to top. How a mesh is lit is its material (manim.material): Pbr() by default, glTF metallic-roughness, or Unlit(); set_material() changes it. Pbr to Pbr animates by its parameters and ends on the target itself; between Unlit and Pbr the material switches when the progress reaches 1. renderer/_lighting.py has the one shading function for every renderer: point lights and an ambient light in linear RGB, with glTF's units, a Lambert diffuse and a normalized Blinn-Phong highlight, two-sided, finite for any input. The OpenGL mesh shader computes the same per pixel (shaders/include/lighting.glsl); a test compares its pixels with the function. Meshes are lit by a ThreeDCamera's light, of the default intensity (fully lighting a white surface facing it at the default distance), with ambient light 0.25; under other cameras and when fixed in frame they show their colours as they are. Snapshots carry meshes as RMesh (slots: geometry, rgbas, uvs, texture, material). The OpenGL renderer draws them with depth testing and with a depth pre-pass for translucent meshes so only their front layer shows; vertex normals and textures are cached by slot version. The Cairo renderer raises CapabilityError for them for now. A surface's checkerboard and wireframe are made from its current points, not from its function again: a surface that was moved or deformed keeps its shape. Transforming a path into a mesh, or a mesh into a path, raises a ValueError that says so. Interpolating between meshes with the same colours keeps the colours' version.
manim/mobject/opengl/opengl_geometry.py is not imported anywhere in the library; the OpenGL renderer draws the regular geometry classes, which ConvertToOpenGL rebases onto OpenGLVMobject. Its only user was one test, which now uses Triangle under the OpenGL renderer and still covers rotating an OpenGL mobject about a view of its own vertices. The shaders default/, image/, test/, design.frag, design_2.frag and simple_vert.glsl are not loaded by the library or the tests. design_3.frag, manim_coords/ and vertex_colors/ were only loaded by example_scenes/opengl.py: the plane mesh helper there now passes its shader source inline, and the two scenes that only demonstrated loading a packaged shader by name are removed (the inline variants remain). This is the first step of removing the duplicated mobject hierarchy: less code to account for in the inventory of OpenGL-only API.
Both renderers now draw the same mobject classes, so the second hierarchy and the machinery that swapped it in go: - manim/mobject/opengl (OpenGLMobject, OpenGLVMobject, OpenGLPMobject, OpenGLSurface, OpenGLTexturedSurface, OpenGLImageMobject, OpenGLSurfaceMesh, TrueDot, DotCloud, ...), the ConvertToOpenGL metaclass, the base-class rewriting in the renderer setter of ManimConfig, uses_opengl_mobjects() and the development switch MANIM_OPENGL_COMMON_MOBJECTS, and manim.mobject.utils (get_mobject_class() and friends); - every branch on the mobject classes or on uses_opengl_mobjects() in animations, mobjects, scenes and the camera; methods that only existed for the OpenGL classes (init_points aliases, quadratic arc points, the quadratic SVG path builder and its long_lines / should_subdivide_sharp_curves / should_remove_null_curves options); - the OpenGL renderer's shader-wrapper drawing (render_mobject, texture ids by path), the projection shader path and its config options use_projection_fill_shaders / use_projection_stroke_shaders; - the raw GPU meshes that bypassed mobjects: manim.opengl, Object3D, Mesh and FullScreenQuad, manim.utils.opengl, Scene.meshes and update_meshes, the renderer's mesh view/projection matrices and the evaluation guard against GPU meshes. MeshMobject draws meshes on both renderers; Shader stays as an internal of the OpenGL renderer. Custom GLSL in scenes is no longer possible. PMobject.rgbas, kept in the OpenGL classes' data dict while ConvertToOpenGL could rebase point clouds, is a field now, so point clouds get a change key like every other drawn mobject. Behavior changes: - Sphere and Torus have the same default resolution on both renderers. - The ThreeDCamera light source is a Point again (it was a plain Mobject while OpenGL classes existed), so it can be animated. - example_scenes/opengl.py's SurfaceExample uses ParametricSurface, textures and the ThreeDScene camera; its raw mesh and shader scenes are removed. Tests: tests/opengl tested the OpenGL classes, or repeated module tests under the OpenGL renderer, which no longer changes any mobject; it is removed, keeping the brace tip test as a module test. Its tests of the renderer itself (background colour, dry runs to PNG, adding and removing mobjects) move to test_scene_rendering/opengl; the config, sound and notebook naming tests have Cairo twins that test the same code. Tests of the metaclass, .data, rebased point-cloud storage and raw GPU meshes go. The OpenGL lifecycle test checks image textures of snapshot drawing instead of texture ids. The OpenGL control data for test_Circle and test_FixedMobjects3D is regenerated: it was rendered from the OpenGL classes; the new frames show Cairo's semantics (fill and stroke style, fixed-orientation placement).
The surface, textured_surface, true_dot, quadratic_bezier_fill and vectorized_mobject_fill/stroke shaders drew the removed classes. The remaining shaders no longer need the fixed-in-frame and fixed-orientation uniforms (snapshot drawing places those nodes through the camera uniforms), and strokes are not lit, so get_gl_Position and position_point_into_frame lose their branches. The old lighting (finalize_color, add_light and the light_source_position uniform) goes: the mesh shader shades with include/lighting.glsl.
A section of the upgrading guide lists the removed OpenGL classes, functions and options with their replacements and the visible differences. The OpenGL FAQ, the plugin guide's renderer compatibility note, the camera guide (camera views now work on OpenGL) and the deep dive (Scene.add has one code path) are updated.
…e its drawing modules private Review of #5173, for the commits of this branch: imports from manim.utils.versioned_fields, the OpenGL drawing places nodes by RNode.placement, and renderer/opengl/drawing.py and winding_fill.py, renderer internals, become _drawing.py and _winding_fill.py. The removed OpenGL mobject modules leave the list of modules without a reference page.
behackl
force-pushed
the
refactor/unified-mobjects
branch
from
November 24, 2026 23:18
b07ec68 to
5657208
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #5173.
Overview
--renderer=openglused to swap the base classes of every mobject for a second hierarchy (OpenGLMobject,OpenGLVMobject, ...) through theConvertToOpenGLmetaclass. With this PR the OpenGL renderer draws the render snapshots of #5173, so a scene contains the same mobjects under both renderers and looks the same. The second hierarchy, the metaclass andOpenGLCameraare deleted: about 11,000 lines of library code.Smooth lit surfaces, the one thing only the OpenGL classes had, arrive as
MeshMobjectandParametricSurfaceon the commonMobject.Breaking changes
OpenGL*mobject classes are removed, withTrueDotandDotCloud. Use the common classes (Mobject,VMobject,PMobject,ImageMobject, ...);OpenGLSurfaceandOpenGLTexturedSurfacebecomeParametricSurface. The upgrading guide has the full table.OpenGLCamerais removed. OpenGL scenes useCamera,MovingCameraandThreeDCamera.manim.opengl,Object3D,Mesh,FullScreenQuad,Scene.meshes,manim.utils.opengl.manim.mobject.utils(get_mobject_class()and friends) is removed.use_projection_fill_shadersanduse_projection_stroke_shadersare removed.set_color_by_xyz_functakes a Python function; it took GLSL code.Visible changes (OpenGL only)
z_indexand camera behaviour follow Cairo. Against Cairo on reference content, ink is within 5 % and the mean pixel difference below 1 %.ZoomedScene,MultiCameraandfix_in_frame()work on OpenGL.SphereandTorushave Cairo's default resolution.ThreeDCameraor pans a flat one, scrolling zooms.Cairo output is unchanged. Two OpenGL control files are regenerated (
Circle,FixedMobjects3D): they now show Cairo's fill and stroke semantics.New
MeshMobject: a triangle mesh with a colour per mesh or per vertex, an optional texture,depth_testand amaterial(Pbr()by default, orUnlit()).ParametricSurface: a mesh from a function of(u, v), smooth or with one quad per face (flat=True).renderer/_lighting.py, glTF metallic-roughness parameters); the OpenGL mesh shader computes the same per pixel.In this PR meshes are drawn by OpenGL; Cairo draws them from #5175 on.
Docs
For reviewers
12 commits. Most of the diff is deletion in "Remove the OpenGL mobject classes". The new drawing code is in
renderer/opengl/drawing.pyandwinding_fill.py.tests/opengl/is removed; its tests of the renderer itself moved totests/test_scene_rendering/opengl/.