author | Radek Brich <radek.brich@devl.cz> |
Sat, 08 Dec 2007 12:37:45 +0100 | |
branch | pyrit |
changeset 28 | ffe83ca074f3 |
parent 26 | 9073320e9f4c |
child 29 | 574c34441a1c |
permissions | -rwxr-xr-x |
26 | 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 |
||
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
9 |
from raytracer import Raytracer, Light, Sphere, Triangle, NormalVertex, Material |
26 | 10 |
import Image |
11 |
||
12 |
def LoadStanfordPlyFile(rt, mat, filename, scale): |
|
13 |
vertices = [] |
|
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
14 |
normals = [] |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
15 |
vertex_face_num = [] |
26 | 16 |
fp = file(filename) |
17 |
# read header |
|
18 |
tokens = (0,) |
|
19 |
while (tokens[0] != "end_header"): |
|
20 |
tokens = fp.readline().split() |
|
21 |
if (tokens[0] == "element"): |
|
22 |
if (tokens[1] == "vertex"): |
|
23 |
vertex_num = int(tokens[2]) |
|
24 |
if (tokens[1] == "face"): |
|
25 |
face_num = int(tokens[2]) |
|
26 |
||
27 |
# read vertices |
|
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
28 |
num = vertex_num |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
29 |
while (num): |
26 | 30 |
tokens = fp.readline().split() |
31 |
v = [scale*float(x) for x in tokens[0:3]] |
|
32 |
v[0] = -v[0]-1 |
|
33 |
v[1] = v[1]-3 |
|
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
34 |
vertices.append(NormalVertex(tuple(v))) |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
35 |
normals.append([0.,0.,0.]) |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
36 |
vertex_face_num.append(0) |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
37 |
num -= 1 |
26 | 38 |
|
39 |
# read faces |
|
40 |
while (face_num): |
|
41 |
tokens = fp.readline().split() |
|
42 |
if (tokens[0] != "3"): |
|
43 |
print "ply warning: faces of %d vertices not supported" % tokens[0] |
|
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
44 |
v = [vertices[int(x)] for x in tokens[1:4]] |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
45 |
face = Triangle(v[0], v[2], v[1], mat) |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
46 |
n = face.getNormal() |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
47 |
for x in tokens[1:4]: |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
48 |
for i in range(3): |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
49 |
normals[int(x)][i] += n[i] |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
50 |
vertex_face_num[int(x)] += 1 |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
51 |
face.setSmooth() |
26 | 52 |
rt.addshape(face) |
53 |
face_num -= 1 |
|
54 |
||
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
55 |
# interpolate normals at vertices |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
56 |
num = 0 |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
57 |
while (num < vertex_num): |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
58 |
if (vertex_face_num[num] > 0): |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
59 |
for i in range(3): |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
60 |
normals[num][i] /= vertex_face_num[num] |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
61 |
vertices[num].setNormal(tuple(normals[num])) |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
62 |
num += 1 |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
63 |
|
26 | 64 |
rt = Raytracer() |
65 |
mat = Material(colour=(0.9, 0.9, 0.9)) |
|
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
66 |
LoadStanfordPlyFile(rt, mat, "../models/bunny/bun_zipper.ply", 29.0) |
26 | 67 |
|
68 |
light = Light(position=(-5.0, 2.0, 10.0), colour=(0.9, 0.3, 0.6)) |
|
69 |
rt.addlight(light) |
|
70 |
||
71 |
light2 = Light(position=(4.0, -3.0, 10.0), colour=(0.2, 0.9, 0.5)) |
|
72 |
rt.addlight(light2) |
|
73 |
||
74 |
imagesize = (800, 600) |
|
75 |
data = rt.render(imagesize) |
|
76 |
img = Image.fromstring("RGB", imagesize, data) |
|
77 |
img.save('bunny.png') |