From: Charles McGarvey Date: Tue, 6 Mar 2012 00:32:48 +0000 (-0700) Subject: refactor the animation script a bit X-Git-Tag: project3~3 X-Git-Url: https://git.brokenzipper.com/gitweb?a=commitdiff_plain;h=a0e6abd72f045a741fb1ecdd1c2d11281f2840b9;p=chaz%2Frasterize refactor the animation script a bit --- diff --git a/animate.lua b/animate.lua index b9fb1be..ad16db9 100755 --- a/animate.lua +++ b/animate.lua @@ -60,7 +60,7 @@ s 10 10 10 -- ImageMagick is used to scale the frames back down to size; if ImageMagick -- is not installed, the resulting frames will end up bigger than they should -- be and the aliasing problem will not be fixed. -local supersample = 4 +local supersample = 2 -- Set the width and height of the viewport. local size = {w = 640, h = 480} @@ -68,14 +68,12 @@ local size = {w = 640, h = 480} -- Set the number of concurrent renderings (for multi-core machines). local jobs = 6 +-- Set the options to be passed directly to ffmpeg. +local ffmpeg_opts = "-b 1024k" -- end of configuration --------------------------------------------------------------------------- -local fmt = string.format -local write = function(...) return io.write(fmt(...)) end -local run = function(...) return os.execute(fmt(...)) end - function vec_new(x, y, z) return {x = x, y = y, z = z} end @@ -88,6 +86,10 @@ function vec_scale(v, s) return vec_new(v.x * s, v.y * s, v.z * s) end +local fmt = string.format +local write = function(...) io.write(fmt(...)) io.flush() end +local run = function(...) return os.execute(fmt(...)) end + function render(i) while i do local w, h = size.w, size.h @@ -100,7 +102,7 @@ function render(i) local out = io.popen(command, "w") local e = eye(i) local l = look(i) - write("\27[80D\27[2Kframe\t %4d / %d", i + 1, frames) + write("\27[80D\27[2Kframe\t %4d / %d", i, frames) out:write(fmt([[ U3 %d %d @@ -119,21 +121,31 @@ X end end -print("Animating scene...") - -local threads = {} -for i = 1,jobs do - table.insert(threads, coroutine.wrap(render)) +function renderings() + local t = {} + for i = 1,jobs do + table.insert(t, coroutine.wrap(render)) + end + local iterations = frames + jobs + local frame = 0 + return function() + frame = frame + 1 + if frame <= iterations then + local i = frame + if frames < i then i = null end + return t[1 + frame % jobs], i + end + end end +print("Animating scene...") + run("rm -rf frames && mkdir frames >/dev/null 2>&1") -for i = 0,(frames-1) do - threads[1 + (i % jobs)](i) -end -for _,thread in ipairs(threads) do thread(null) end +for render,frame in renderings() do render(frame) end + print() -if run("ffmpeg -i frames/anim%04d.bmp -b 1024k -y -an scene.avi") == 0 then +if run("ffmpeg -i frames/anim%%04d.bmp %s -y -an scene.avi", ffmpeg_opts) == 0 then print("Animation written to scene.avi.") end