Move Texture handling and vao creation outside of Camera

This commit is contained in:
Grant Sanderson
2023-01-25 12:10:39 -08:00
parent 3299741359
commit c94d8fd3b0
5 changed files with 102 additions and 99 deletions

View File

@ -4,6 +4,7 @@ import os
import re
from functools import lru_cache
import moderngl
from PIL import Image
from manimlib.utils.directories import get_shader_dir
from manimlib.utils.file_ops import find_file
@ -14,6 +15,34 @@ if TYPE_CHECKING:
from typing import Sequence, Optional
ID_TO_TEXTURE: dict[int, moderngl.Texture] = dict()
@lru_cache()
def image_path_to_texture(path: str, ctx: moderngl.Context) -> moderngl.Texture:
im = Image.open(path).convert("RGBA")
return ctx.texture(
size=im.size,
components=len(im.getbands()),
data=im.tobytes(),
)
def get_texture_id(texture: moderngl.Texture) -> int:
tid = 0
while tid in ID_TO_TEXTURE:
tid += 1
ID_TO_TEXTURE[tid] = texture
texture.use(location=tid)
return tid
def release_texture(texture_id: int):
texture = ID_TO_TEXTURE.pop(texture_id, None)
if texture is not None:
texture.release()
@lru_cache()
def get_shader_program(
ctx: moderngl.context.Context,