#!/usr/bin/env lua -- -- CS5600 University of Utah -- Charles McGarvey -- mcgarvey@eng.utah.edu -- -- This program automates the process of creating simple fly-by animations. -- The rasters are saved in the `frames' directory, and if ffmpeg is -- installed, the frames will also be combined into a video file. This script -- must be called from the same directory where the rasterize program is. -- Set the width and height of the viewport. local size = {w = 640, h = 480} -- Set the number of frames to be rendered for the animation. local frames = 360 -- Define the code to calculate where the camera is, in world coordinates. local eye = function(frame) -- just rotate around the center of the scene on the XZ plane local center = vec_new(0, 1, 0) local distance = 4 local start = math.pi local t = start + 2 * math.pi * frame / frames local v = vec_new(math.cos(t), 0, math.sin(t)) return vec_add(vec_scale(v, distance), center) end -- Define the code to calculate where the focal point of the scene is. local look = function(frame) -- keep the camera focused on the buddha return vec_new(2, 1, 2) end -- Define the actual objects of the scene that will be rendered, in the -- extended u3d format. local scene = [[ L 0 1000000 0 1 1 1 1 1 1 L 0 0 1000000 1 1 1 1 1 1 g triangle.raw c 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 t 2 0 -2 s 1 1 1 g dragon.raw M 0.3 0.3 0.3 5 c 0.7 0.3 0.2 0.8 0.2 0.1 0.9 0.2 0.2 t -2 -1 -2 s 2 2 2 g budda.raw c 0.1 0.2 0.7 0.1 0.3 0.9 0.2 0.1 0.8 t 2 -1.5 2 s 10 10 10 g bunny.raw M 0.2 0.2 0.2 1 c 0.9 0.8 0.9 0.8 0.7 0.9 0.9 0.8 0.7 t -2 -1 2 s 10 10 10 g teapot2.raw M 1 1 1 128 c 0 1 0 0 1 0 0 1 0 t 0 -1 0 s 0.6 0.6 0.6 ]] -- Set the number of concurrent renderings (for multi-core machines). local jobs = 6 -- end of configuration --------------------------------------------------------------------------- local fmt = string.format local write = function(...) io.write(fmt(...)) end function vec_new(x, y, z) return {x = x, y = y, z = z} end function vec_add(a, b) return vec_new(a.x + b.x, a.y + b.y, a.z + b.z) end function vec_scale(v, s) return vec_new(v.x * s, v.y * s, v.z * s) end function render(i) while i do local filename = fmt("frames/anim%04d.bmp", i) local command = fmt("./rasterize -o %s >/dev/null", filename) 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) out:write(fmt([[ U3 %d %d %f %f %f %f %f %f 0 1 0 1.57 %f 0.1 1000 %s X ]], size.w, size.h, e.x, e.y, e.z, l.x, l.y, l.z, size.w/size.h, scene)) i = coroutine.yield() out:close() end end print("Animating scene...") local threads = {} for i = 1,jobs do table.insert(threads, coroutine.wrap(render)) end os.execute("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 print() if os.execute("ffmpeg -i frames/anim%04d.bmp -b 1024k -y -an scene.avi") == 0 then print("Animation written to scene.avi.") end