]> Dogcows Code - chaz/rasterize/blob - animate.lua
refactor triangle group into a separate class
[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 = 360
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 = vec_new(0, 1, 0)
24 local distance = 4
25 local start = math.pi
26 local t = start + 2 * math.pi * frame / frames
27 local v = vec_new(math.cos(t), 0, math.sin(t))
28 return vec_add(vec_scale(v, distance), center)
29 end
30
31 -- Define the code to calculate where the focal point of the scene is.
32 local look = function(frame)
33 -- keep the camera focused on the buddha
34 return vec_new(2, 1, 2)
35 end
36
37 -- Define the actual objects of the scene that will be rendered, in the
38 -- extended u3d format.
39 local scene = [[
40 L 0 1000000 0 1 1 1 1 1 1
41 L 0 0 1000000 1 1 1 1 1 1
42 g triangle.raw
43 c 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1
44 t 2 0 -2
45 s 1 1 1
46 g dragon.raw
47 M 0.3 0.3 0.3 5
48 c 0.7 0.3 0.2 0.8 0.2 0.1 0.9 0.2 0.2
49 t -2 -1 -2
50 s 2 2 2
51 g budda.raw
52 c 0.1 0.2 0.7 0.1 0.3 0.9 0.2 0.1 0.8
53 t 2 -1.5 2
54 s 10 10 10
55 g bunny.raw
56 M 0.2 0.2 0.2 1
57 c 0.9 0.8 0.9 0.8 0.7 0.9 0.9 0.8 0.7
58 t -2 -1 2
59 s 10 10 10
60 g teapot2.raw
61 M 1 1 1 128
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_new(x, y, z)
78 return {x = x, y = y, z = z}
79 end
80
81 function vec_add(a, b)
82 return vec_new(a.x + b.x, a.y + b.y, a.z + b.z)
83 end
84
85 function vec_scale(v, s)
86 return vec_new(v.x * s, v.y * s, v.z * s)
87 end
88
89 function render(i)
90 while i do
91 local filename = fmt("frames/anim%04d.bmp", i)
92 local command = fmt("./rasterize -o %s >/dev/null", filename)
93 local out = io.popen(command, "w")
94 local e = eye(i)
95 local l = look(i)
96 write("\27[80D\27[2Kframe\t %4d / %d", i + 1, frames)
97 out:write(fmt([[
98 U3
99 %d %d
100 %f %f %f
101 %f %f %f
102 0 1 0
103 1.57 %f 0.1 1000
104 %s
105 X
106 ]], size.w, size.h, e.x, e.y, e.z, l.x, l.y, l.z, size.w/size.h, scene))
107 i = coroutine.yield()
108 out:close()
109 end
110 end
111
112 print("Animating scene...")
113
114 local threads = {}
115 for i = 1,jobs do
116 table.insert(threads, coroutine.wrap(render))
117 end
118
119 os.execute("rm -rf frames && mkdir frames >/dev/null 2>&1")
120 for i = 0,(frames-1) do
121 threads[1 + (i % jobs)](i)
122 end
123 for _,thread in ipairs(threads) do thread(null) end
124 print()
125
126 if os.execute("ffmpeg -i frames/anim%04d.bmp -b 1024k -y -an scene.avi") == 0 then
127 print("Animation written to scene.avi.")
128 end
129
This page took 0.035165 seconds and 4 git commands to generate.