]> Dogcows Code - chaz/rasterize/blob - animate.lua
add phong interpolation (lighting)
[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 = {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 M 0.3 0.3 0.3 5
51 c 0.7 0.3 0.2 0.8 0.2 0.1 0.9 0.2 0.2
52 t -2 -1 -2
53 s 2 2 2
54 g budda.raw
55 c 0.1 0.2 0.7 0.1 0.3 0.9 0.2 0.1 0.8
56 t 2 -1.5 2
57 s 10 10 10
58 g bunny.raw
59 M 0.2 0.2 0.2 1
60 c 0.9 0.8 0.9 0.8 0.7 0.9 0.9 0.8 0.7
61 t -2 -1 2
62 s 10 10 10
63 g teapot2.raw
64 M 1 1 1 128
65 c 0 1 0 0 1 0 0 1 0
66 t 0 -1 0
67 s 0.6 0.6 0.6
68 ]]
69
70 -- Set the number of concurrent renderings (for multi-core machines).
71 local jobs = 6
72
73
74 -- end of configuration
75 ---------------------------------------------------------------------------
76
77 local fmt = string.format
78 local write = function(...) io.write(fmt(...)) end
79
80 function vec_add(a, b)
81 return {x = a.x + b.x, y = a.y + b.y, z = a.z + b.z}
82 end
83
84 function vec_scale(v, s)
85 return {x = v.x * s, y = v.y * s, z = v.z * s}
86 end
87
88 function render(i)
89 while i do
90 local filename = fmt("frames/anim%04d.bmp", i)
91 local command = fmt("./rasterize -o %s >/dev/null", filename)
92 local out = io.popen(command, "w")
93 local e = eye(i)
94 local l = look(i)
95 write("\27[80D\27[2Kframe\t %4d / %d", i + 1, frames)
96 out:write(fmt([[
97 U3
98 %d %d
99 %f %f %f
100 %f %f %f
101 0 1 0
102 1.57 %f 0.1 1000
103 %s
104 X
105 ]], size.w, size.h, e.x, e.y, e.z, l.x, l.y, l.z, size.w/size.h, scene))
106 i = coroutine.yield()
107 out:close()
108 end
109 end
110
111 print("Animating scene...")
112
113 local threads = {}
114 for i = 1,jobs do
115 table.insert(threads, coroutine.wrap(render))
116 end
117
118 os.execute("rm -rf frames && mkdir frames >/dev/null 2>&1")
119 for i = 0,(frames-1) do
120 threads[1 + (i % jobs)](i)
121 end
122 for _,thread in ipairs(threads) do thread(null) end
123 print()
124
125 if os.execute("ffmpeg -i frames/anim%04d.bmp -b 1024k -y -an scene.avi") == 0 then
126 print("Animation written to scene.avi.")
127 end
128
This page took 0.036405 seconds and 4 git commands to generate.