]> Dogcows Code - chaz/rasterize/blob - animate.lua
make the animate script even easier to understand
[chaz/rasterize] / animate.lua
1 #!/usr/bin/env lua
2
3 --
4 -- CS5600 University of Utah
5 -- Charles McGarvey
6 -- mcgarvey@eng.utah.edu
7 --
8
9 -- This program automates the process of creating simple fly-by animations.
10 -- The rasters are saved in the `frames' directory, and if ffmpeg is
11 -- installed, the frames will also be combined into a video file. This script
12 -- must be called from the same directory where the rasterize program is.
13
14 -- Set the width and height of the viewport.
15 local size = {w = 640, h = 480}
16
17 -- Set the number of frames to be rendered for the animation.
18 local frames = 512
19
20 -- Define the code to calculate where the camera is, in world coordinates.
21 local eye = function(frame)
22 -- just rotate around the center of the scene on the XZ plane
23 local center = {x = 0, y = 1, z = 0}
24 local distance = 4
25 local start = math.pi
26 local t = start + 2 * math.pi * frame / frames
27 local v = {
28 x = math.cos(t),
29 y = 0,
30 z = math.sin(t),
31 }
32 return vec_add(vec_scale(v, distance), center)
33 end
34
35 -- Define the code to calculate where the focal point of the scene is.
36 local look = function(frame)
37 return {x = 2, y = 1, z = 2} -- keep focused on the buddha
38 end
39
40 -- Define the actual objects of the scene that will be rendered, in the
41 -- extended u3d format.
42 local scene = [[
43 L 0 1000000 0 1 1 1 1 1 1
44 L 0 0 1000000 1 1 1 1 1 1
45 g triangle.raw
46 c 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1
47 t 2 0 -2
48 s 1 1 1
49 g dragon.raw
50 c 0.7 0.3 0.2 0.8 0.2 0.1 0.9 0.2 0.2
51 t -2 -1 -2
52 s 2 2 2
53 g budda.raw
54 c 0.1 0.2 0.7 0.1 0.3 0.9 0.2 0.1 0.8
55 t 2 -1.5 2
56 s 10 10 10
57 g bunny.raw
58 c 0.9 0.8 0.9 0.8 0.7 0.9 0.9 0.8 0.7
59 t -2 -1 2
60 s 10 10 10
61 g teapot2.raw
62 c 0 1 0 0 1 0 0 1 0
63 t 0 -1 0
64 s 0.6 0.6 0.6
65 ]]
66
67 -- Set the number of concurrent renderings (for multi-core machines).
68 local jobs = 6
69
70
71 -- end of configuration
72 ---------------------------------------------------------------------------
73
74 local fmt = string.format
75 local write = function(...) io.write(fmt(...)) end
76
77 function vec_add(a, b)
78 return {x = a.x + b.x, y = a.y + b.y, z = a.z + b.z}
79 end
80
81 function vec_scale(v, s)
82 return {x = v.x * s, y = v.y * s, z = v.z * s}
83 end
84
85 function render(i)
86 while i do
87 local filename = fmt("frames/anim%04d.bmp", i)
88 local command = fmt("./rasterize -o %s >/dev/null", filename)
89 local out = io.popen(command, "w")
90 local e = eye(i)
91 local l = look(i)
92 write("\27[80D\27[2Kframe\t %4d / %d", i + 1, frames)
93 out:write(fmt([[
94 U3
95 %d %d
96 %f %f %f
97 %f %f %f
98 0 1 0
99 1.57 %f 0.1 1000
100 %s
101 X
102 ]], size.w, size.h, e.x, e.y, e.z, l.x, l.y, l.z, size.w/size.h, scene))
103 i = coroutine.yield()
104 out:close()
105 end
106 end
107
108 print("Animating scene...")
109
110 local threads = {}
111 for i = 1,jobs do
112 table.insert(threads, coroutine.wrap(render))
113 end
114
115 os.execute("rm -rf frames && mkdir frames >/dev/null 2>&1")
116 for i = 0,(frames-1) do
117 threads[1 + (i % jobs)](i)
118 end
119 for _,thread in ipairs(threads) do thread(null) end
120 print()
121
122 if os.execute("ffmpeg -i frames/anim%04d.bmp -b 1024k -y -an scene.avi") == 0 then
123 print("Animation written to scene.avi.")
124 end
125
This page took 0.037044 seconds and 4 git commands to generate.