Skip to content

Commit e6d7e76

Browse files
committed
Upside down file reading
1 parent bc262c0 commit e6d7e76

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

manim/renderer/opengl_renderer.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from ..mobject.opengl_mobject import OpenGLMobject, OpenGLPoint
2424
from PIL import Image
2525
from manim import config
26+
from ..scene.scene_file_writer import SceneFileWriter
2627

2728

2829
class OpenGLCamera(OpenGLMobject):
@@ -34,7 +35,7 @@ def __init__(
3435
euler_angles=[0, 0, 0],
3536
focal_distance=2,
3637
light_source_position=[-10, 10, 10],
37-
**kwargs
38+
**kwargs,
3839
):
3940
self.use_z_index = True
4041
self.frame_rate = 60
@@ -185,7 +186,7 @@ def __init__(self):
185186
self.num_plays = 0
186187
self.skip_animations = False
187188

188-
self.window = Window(size=(854, 480))
189+
self.window = Window(size=(1280, 720))
189190

190191
self.camera = OpenGLCamera()
191192

@@ -214,7 +215,6 @@ def update_depth_test(self, context, shader_wrapper):
214215

215216
def get_pixel_shape(self):
216217
return self.frame_buffer_object.viewport[2:4]
217-
# return (self.pixel_width, self.pixel_height)
218218

219219
def refresh_perspective_uniforms(self, camera_frame):
220220
pw, ph = self.get_pixel_shape()
@@ -338,7 +338,10 @@ def set_shader_uniforms(self, shader, shader_wrapper):
338338
pass
339339

340340
def init_scene(self, scene):
341-
self.file_writer = None
341+
self.file_writer = SceneFileWriter(
342+
self,
343+
scene.__class__.__name__,
344+
)
342345

343346
def play(self, scene, *args, **kwargs):
344347
if len(args) == 0:
@@ -349,7 +352,14 @@ def play(self, scene, *args, **kwargs):
349352
if scene.compile_animation_data(*args, **kwargs):
350353
self.animation_start_time = time.time()
351354
self.animation_elapsed_time = 0
355+
356+
temp_name = f"media/temp_{self.num_plays}.mp4"
357+
self.file_writer.begin_animation(
358+
not self.skip_animations, file_path=temp_name
359+
)
352360
scene.play_internal()
361+
self.file_writer.end_animation(not self.skip_animations)
362+
353363
self.num_plays += 1
354364

355365
def render(self, scene, frame_offset, moving_mobjects):
@@ -362,6 +372,9 @@ def update_frame():
362372

363373
window_background_color = (0.2, 0.2, 0.2, 1)
364374
update_frame()
375+
376+
self.file_writer.write_frame(self)
377+
365378
while self.animation_elapsed_time < frame_offset:
366379
update_frame()
367380

@@ -370,3 +383,19 @@ def scene_finished(self, scene):
370383

371384
def save_static_frame_data(self, scene, static_mobjects):
372385
pass
386+
387+
def get_raw_frame_buffer_object_data(self, dtype="f1"):
388+
# Copy blocks from the fbo_msaa to the drawn fbo using Blit
389+
# pw, ph = self.get_pixel_shape()
390+
# gl.glBindFramebuffer(gl.GL_READ_FRAMEBUFFER, self.fbo_msaa.glo)
391+
# gl.glBindFramebuffer(gl.GL_DRAW_FRAMEBUFFER, self.fbo.glo)
392+
# gl.glBlitFramebuffer(
393+
# 0, 0, pw, ph, 0, 0, pw, ph, gl.GL_COLOR_BUFFER_BIT, gl.GL_LINEAR
394+
# )
395+
num_channels = 4
396+
ret = self.frame_buffer_object.read(
397+
viewport=self.frame_buffer_object.viewport,
398+
components=num_channels,
399+
dtype=dtype,
400+
)
401+
return ret

manim/scene/scene_file_writer.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def add_sound(self, sound_file, time=None, gain=None, **kwargs):
238238
self.add_audio_segment(new_segment, time, **kwargs)
239239

240240
# Writers
241-
def begin_animation(self, allow_write=False):
241+
def begin_animation(self, allow_write=False, file_path=None):
242242
"""
243243
Used internally by manim to stream the animation to FFMPEG for
244244
displaying or writing to a file.
@@ -249,7 +249,7 @@ def begin_animation(self, allow_write=False):
249249
Whether or not to write to a video file.
250250
"""
251251
if config["write_to_movie"] and allow_write:
252-
self.open_movie_pipe()
252+
self.open_movie_pipe(file_path=file_path)
253253

254254
def end_animation(self, allow_write=False):
255255
"""
@@ -264,7 +264,7 @@ def end_animation(self, allow_write=False):
264264
if config["write_to_movie"] and allow_write:
265265
self.close_movie_pipe()
266266

267-
def write_frame(self, frame):
267+
def write_frame(self, frame_or_renderer):
268268
"""
269269
Used internally by Manim to write a frame to
270270
the FFMPEG input buffer.
@@ -274,12 +274,19 @@ def write_frame(self, frame):
274274
frame : np.array
275275
Pixel array of the frame.
276276
"""
277-
if config["write_to_movie"]:
278-
self.writing_process.stdin.write(frame.tostring())
279-
if config["save_pngs"]:
280-
path, extension = os.path.splitext(self.image_file_path)
281-
Image.fromarray(frame).save(f"{path}{self.frame_count}{extension}")
282-
self.frame_count += 1
277+
if config["use_opengl_renderer"]:
278+
renderer = frame_or_renderer
279+
self.writing_process.stdin.write(
280+
renderer.get_raw_frame_buffer_object_data()
281+
)
282+
else:
283+
frame = frame_or_renderer
284+
if config["write_to_movie"]:
285+
self.writing_process.stdin.write(frame.tostring())
286+
if config["save_pngs"]:
287+
path, extension = os.path.splitext(self.image_file_path)
288+
Image.fromarray(frame).save(f"{path}{self.frame_count}{extension}")
289+
self.frame_count += 1
283290

284291
def save_final_image(self, image):
285292
"""
@@ -333,18 +340,22 @@ def finish(self):
333340
else:
334341
self.clean_cache()
335342

336-
def open_movie_pipe(self):
343+
def open_movie_pipe(self, file_path=None):
337344
"""
338345
Used internally by Manim to initialise
339346
FFMPEG and begin writing to FFMPEG's input
340347
buffer.
341348
"""
342-
file_path = self.partial_movie_files[self.renderer.num_plays]
349+
if file_path is None:
350+
file_path = self.partial_movie_files[self.renderer.num_plays]
343351
self.partial_movie_file_path = file_path
344352

345353
fps = config["frame_rate"]
346-
height = config["pixel_height"]
347-
width = config["pixel_width"]
354+
if config["use_opengl_renderer"]:
355+
width, height = self.renderer.get_pixel_shape()
356+
else:
357+
height = config["pixel_height"]
358+
width = config["pixel_width"]
348359

349360
command = [
350361
FFMPEG_BIN,

0 commit comments

Comments
 (0)