|
1 #!/usr/bin/python |
|
2 |
|
3 # this demo needs bunny model from |
|
4 # http://graphics.stanford.edu/data/3Dscanrep/ |
|
5 |
|
6 import sys |
|
7 sys.path.append(open('ModulePath').read().strip()) |
|
8 |
|
9 from raytracer import Raytracer, Light, Sphere, Triangle, Material |
|
10 import Image |
|
11 |
|
12 def LoadStanfordPlyFile(rt, mat, filename, scale): |
|
13 vertices = [] |
|
14 fp = file(filename) |
|
15 # read header |
|
16 tokens = (0,) |
|
17 while (tokens[0] != "end_header"): |
|
18 tokens = fp.readline().split() |
|
19 if (tokens[0] == "element"): |
|
20 if (tokens[1] == "vertex"): |
|
21 vertex_num = int(tokens[2]) |
|
22 if (tokens[1] == "face"): |
|
23 face_num = int(tokens[2]) |
|
24 |
|
25 # read vertices |
|
26 while (vertex_num): |
|
27 tokens = fp.readline().split() |
|
28 v = [scale*float(x) for x in tokens[0:3]] |
|
29 v[0] = -v[0]-1 |
|
30 v[1] = v[1]-3 |
|
31 vertices.append(tuple(v)) |
|
32 vertex_num -= 1 |
|
33 |
|
34 # read faces |
|
35 while (face_num): |
|
36 tokens = fp.readline().split() |
|
37 if (tokens[0] != "3"): |
|
38 print "ply warning: faces of %d vertices not supported" % tokens[0] |
|
39 f = [vertices[int(x)] for x in tokens[1:4]] |
|
40 face = Triangle(f[0], f[2], f[1], mat) |
|
41 rt.addshape(face) |
|
42 face_num -= 1 |
|
43 |
|
44 rt = Raytracer() |
|
45 mat = Material(colour=(0.9, 0.9, 0.9)) |
|
46 LoadStanfordPlyFile(rt, mat, "../models/bunny/bun_zipper_res3.ply", 29.0) |
|
47 |
|
48 light = Light(position=(-5.0, 2.0, 10.0), colour=(0.9, 0.3, 0.6)) |
|
49 rt.addlight(light) |
|
50 |
|
51 light2 = Light(position=(4.0, -3.0, 10.0), colour=(0.2, 0.9, 0.5)) |
|
52 rt.addlight(light2) |
|
53 |
|
54 imagesize = (800, 600) |
|
55 data = rt.render(imagesize) |
|
56 img = Image.fromstring("RGB", imagesize, data) |
|
57 img.save('bunny.png') |