Repository: ManimCommunity/manim
File: manim/renderer/opengl_renderer.py (Python)

Code before:
        self.pressed_keys = set()

        # Initialize texture map.
        self.path_to_texture_id = {}

    def init_scene(self, scene):
        self.partial_movie_files = []
        self.file_writer = self._file_writer_class(
            self,
            scene.__class__.__name__,
        )
        self.scene = scene
        if not hasattr(self, "window"):
            if config["preview"]:
                from .opengl_renderer_window import Window

                self.window = Window(self)
                self.context = self.window.ctx
                self.frame_buffer_object = self.context.detect_framebuffer()
            else:
                context_settings = {}

                try:
                    from IPython import get_ipython

                    if "google.colab" in str(get_ipython()):
                        context_settings["backend"] = "egl"
                except ModuleNotFoundError:
                    pass

                self.window = None
                self.context = moderngl.create_context(
                    standalone=True, **context_settings
                )
                self.frame_buffer_object = self.get_frame_buffer_object(self.context, 0)
                self.frame_buffer_object.use()
            self.context.enable(moderngl.BLEND)
            self.context.blend_func = (
                moderngl.SRC_ALPHA,
                moderngl.ONE_MINUS_SRC_ALPHA,
                moderngl.ONE,
                moderngl.ONE,
            )

    def get_pixel_shape(self):
        if hasattr(self, "frame_buffer_object"):
            return self.frame_buffer_object.viewport[2:4]
        else:
            return None

    def refresh_perspective_uniforms(self, camera):

Review comment (suggestion):
Changing the backend to egl in more cases would (I think) also allow OpenGL rendering to be used in other remote settings (e.g., https://try.manim.community).

```suggestion
                    if get_ipython() is not None:
```

Code after:
        self.pressed_keys = set()

        # Initialize texture map.
        self.path_to_texture_id = {}

    def init_scene(self, scene):
        self.partial_movie_files = []
        self.file_writer = self._file_writer_class(
            self,
            scene.__class__.__name__,
        )
        self.scene = scene
        if not hasattr(self, "window"):
            if config["preview"]:
                from .opengl_renderer_window import Window

                self.window = Window(self)
                self.context = self.window.ctx
                self.frame_buffer_object = self.context.detect_framebuffer()
            else:
                self.window = None
                try:
                    self.context = moderngl.create_context(standalone=True)
                except Exception:
                    self.context = moderngl.create_context(
                        standalone=True, backend="egl"
                    )
                self.frame_buffer_object = self.get_frame_buffer_object(self.context, 0)
                self.frame_buffer_object.use()
            self.context.enable(moderngl.BLEND)
            self.context.blend_func = (
                moderngl.SRC_ALPHA,
                moderngl.ONE_MINUS_SRC_ALPHA,
                moderngl.ONE,
                moderngl.ONE,
            )

    def get_pixel_shape(self):
        if hasattr(self, "frame_buffer_object"):
            return self.frame_buffer_object.viewport[2:4]
        else:
            return None

    def refresh_perspective_uniforms(self, camera):
        pw, ph = self.get_pixel_shape()
        fw, fh = camera.get_shape()
        # TODO, this should probably be a mobject uniform, with
        # the camera taking care of the conversion factor
        anti_alias_width = self.anti_alias_width / (ph / fh)
        # Orient light
        rotation = camera.inverse_rotation_matrix