|
1 #!/usr/bin/python |
|
2 |
|
3 # read nff data from standart input and render image to render_nff.png |
|
4 # see http://tog.acm.org/resources/SPD/ |
|
5 # only spheres and triangles are implemented |
|
6 |
|
7 from raytracer import Raytracer, Camera, Light, Material, Sphere, NormalVertex, Triangle |
|
8 from math import pi |
|
9 import sys, Image |
|
10 |
|
11 rt = Raytracer() |
|
12 imagesize = (800, 600) |
|
13 |
|
14 mat = Material(colour=(1.0, 1.0, 1.0)) |
|
15 |
|
16 f = sys.stdin |
|
17 while True: |
|
18 line = f.readline() |
|
19 if line == "": |
|
20 break; |
|
21 ln = line.split() |
|
22 if ln[0] == 'v': # Viewpoint location |
|
23 # from |
|
24 ln = f.readline().split() |
|
25 assert ln[0] == 'from' |
|
26 eye = (float(ln[1]), float(ln[2]), float(ln[3])) |
|
27 # at |
|
28 ln = f.readline().split() |
|
29 assert ln[0] == 'at' |
|
30 lookat = (float(ln[1]), float(ln[2]), float(ln[3])) |
|
31 # up |
|
32 ln = f.readline().split() |
|
33 assert ln[0] == 'up' |
|
34 up = (float(ln[1]), float(ln[2]), float(ln[3])) |
|
35 # angle |
|
36 ln = f.readline().split() |
|
37 assert ln[0] == 'angle' |
|
38 angle = float(ln[1]) |
|
39 # hither |
|
40 ln = f.readline().split() |
|
41 assert ln[0] == 'hither' |
|
42 hither = float(ln[1]) |
|
43 # resolution |
|
44 ln = f.readline().split() |
|
45 assert ln[0] == 'resolution' |
|
46 imagesize = (int(ln[1]), int(ln[2])) |
|
47 # set camera as specified |
|
48 cam = Camera(eye=eye, lookat=lookat, up=up) |
|
49 cam.setAngle(angle/180*pi) |
|
50 rt.setcamera(cam) |
|
51 elif ln[0] == 'b': # Background color |
|
52 rt.setbgcolour((float(ln[1]), float(ln[2]), float(ln[3]))) |
|
53 elif ln[0] == 'l': # Light |
|
54 pos = (float(ln[1]), float(ln[2]), float(ln[3])) |
|
55 rt.addlight(Light(position=pos)) |
|
56 elif ln[0] == 'f': # Fill color and shading parameters |
|
57 colour = (float(ln[1]), float(ln[2]), float(ln[3])) |
|
58 mat = Material(colour=colour) |
|
59 mat.setPhong(0,float(ln[4]),float(ln[5]),float(ln[6])) |
|
60 mat.setTransmissivity(float(ln[7]),float(ln[8])) |
|
61 elif ln[0] == 's': # Sphere |
|
62 center = (float(ln[1]), float(ln[2]), float(ln[3])) |
|
63 radius = float(ln[4]) |
|
64 rt.addshape(Sphere(centre=center, radius=radius, material=mat)) |
|
65 elif ln[0] == 'p': # Polygon |
|
66 vertex_count = int(ln[1]) |
|
67 vertices = [] |
|
68 for i in range(vertex_count): |
|
69 ln = f.readline().split() |
|
70 vertex = (float(ln[0]), float(ln[1]), float(ln[2])) |
|
71 vertices.append(NormalVertex(vertex)) |
|
72 rt.addshape(Triangle(vertices[0], vertices[1], vertices[2], mat)) |
|
73 for i in range(vertex_count)[3:]: |
|
74 rt.addshape(Triangle(vertices[0], vertices[i-1], vertices[i], mat)) |
|
75 else: |
|
76 print "Not implemented:", line |
|
77 f.close() |
|
78 |
|
79 data = rt.render(imagesize) |
|
80 img = Image.fromstring("RGB", imagesize, data) |
|
81 img.save('render_nff.png') |