5 |
5 |
6 import sys |
6 import sys |
7 sys.path.append(open('ModulePath').read().strip()) |
7 sys.path.append(open('ModulePath').read().strip()) |
8 |
8 |
9 from raytracer import Raytracer, Light, Sphere, Triangle, Material |
9 from raytracer import Raytracer, Light, Sphere, Triangle, Material |
|
10 from plyreader import LoadStanfordPlyFile |
10 import Image |
11 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[1] = v[1]-3 |
|
30 vertices.append(tuple(v)) |
|
31 vertex_num -= 1 |
|
32 |
|
33 # read faces |
|
34 while (face_num): |
|
35 tokens = fp.readline().split() |
|
36 if (tokens[0] != "3"): |
|
37 print "ply warning: faces of %d vertices not supported" % tokens[0] |
|
38 f = [vertices[int(x)] for x in tokens[1:4]] |
|
39 face = Triangle(NormalVertex(f[0]), NormalVertex(f[1]), NormalVertex(f[2]), mat) |
|
40 rt.addshape(face) |
|
41 face_num -= 1 |
|
42 |
12 |
43 rt = Raytracer() |
13 rt = Raytracer() |
44 mat = Material(colour=(0.9, 0.9, 0.9)) |
14 mat = Material(colour=(0.9, 0.9, 0.9)) |
45 LoadStanfordPlyFile(rt, mat ,"../models/happy/happy_vrip_res2.ply", 20.0) |
15 LoadStanfordPlyFile(rt, "../models/happy/happy_vrip_res2.ply", |
|
16 mat, smooth=True, scale=20.0, trans=(-3,0,0)) |
46 |
17 |
47 light = Light(position=(-5.0, 2.0, 8.0), colour=(0.9, 0.3, 0.6)) |
18 light = Light(position=(-5.0, 2.0, 8.0), colour=(0.9, 0.3, 0.6)) |
48 rt.addlight(light) |
19 rt.addlight(light) |
49 |
20 |
50 imagesize = (800, 600) |
21 imagesize = (800, 600) |