4334dd8f714da36a755502833e74e648d7c51443
[chaz/rasterize] / rotate.lua
1 #!/usr/bin/env lua
2
3 --
4 -- CS5600 University of Utah
5 -- Charles McGarvey
6 -- mcgarvey@eng.utah.edu
7 --
8
9 -- Render a scene multiple times from different angles, like a camera
10 -- rotating around the object of a scene.
11
12 local size = {w = 640, h = 480}
13 local slices = 512
14
15 local center = {x = 0, y = 1, z = 0}
16 local look = {x = -2, y = -1, z = -2}
17 local distance = 4
18 local start = math.pi
19
20 local scene = [[
21 g teapot2.raw
22 c 1.0 1.0 0.0 1.0 1.0 0.0 1.0 1.0 0.0
23 t 0.0 0.0 0.0
24 s 0.6 0.6 0.6
25 g triangle.raw
26 c 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1
27 t 2.0 0.0 -2.0
28 s 1.0 1.0 1.0
29 g dragon.raw
30 c 0.7 0.3 0.2 0.8 0.2 0.1 0.9 0.2 0.2
31 t -2.0 -1.0 -2.0
32 s 2.0 2.0 2.0
33 g budda.raw
34 c 0.1 0.2 0.7 0.1 0.3 0.9 0.2 0.1 0.8
35 t 2.0 -1.5 2.0
36 s 10.0 10.0 10.0
37 g bunny.raw
38 c 0.9 0.8 0.9 0.8 0.7 0.9 0.9 0.8 0.7
39 t -2.0 -1.0 2.0
40 s 10.0 10.0 10.0
41 ]]
42
43 function vec_add(a, b)
44 return {x = a.x + b.x, y = a.y + b.y, z = a.z + b.z}
45 end
46
47 function vec_scale(v, s)
48 return {x = v.x * s, y = v.y * s, z = v.z * s}
49 end
50
51 function render(i, v)
52 while true do
53 local out = io.popen("./project2 -", "w")
54 out:write(string.format([[
55 U3
56 %d %d
57 %f %f %f
58 %f %f %f
59 0.0 1.0 0.0
60 1.57 %f 0.1 1000.0
61 %s
62 ]], size.w, size.h, v.x, v.y, v.z, look.x, look.y, look.z, size.w/size.h, scene))
63 local a, b = coroutine.yield()
64 out:close()
65 os.rename("stdin.bmp", string.format("anim%04d.bmp", i))
66 if a == null then
67 break
68 else
69 i = a
70 v = b
71 end
72 end
73 end
74
75 local threads = {}
76 local jobs = 3
77 for i = 1,jobs do
78 table.insert(threads, coroutine.wrap(render))
79 end
80
81 for i = 0,(slices-1) do
82 local t = start + 2 * i * math.pi / slices
83 local v = {
84 x = math.cos(t),
85 y = 0,
86 z = math.sin(t),
87 }
88 v = vec_add(vec_scale(v, distance), center)
89 threads[(i % jobs) + 1](i, v)
90 end
91
92 for k,v in ipairs(threads) do
93 v(null) -- clear up any leftover work
94 end
95
This page took 0.043024 seconds and 3 git commands to generate.