author | Radek Brich <radek.brich@devl.cz> |
Sat, 08 Dec 2007 12:37:45 +0100 | |
branch | pyrit |
changeset 28 | ffe83ca074f3 |
parent 25 | b8232edee786 |
child 29 | 574c34441a1c |
permissions | -rwxr-xr-x |
18 | 1 |
#!/usr/bin/python |
2 |
||
3 |
# this demo needs dragon model from |
|
4 |
# http://graphics.stanford.edu/data/3Dscanrep/ |
|
5 |
||
6 |
import sys |
|
24
d0d76e8a5203
new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
19
diff
changeset
|
7 |
sys.path.append(open('ModulePath').read().strip()) |
18 | 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]] |
|
19
4e0955fca797
added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents:
18
diff
changeset
|
29 |
v[0] = -v[0] |
4e0955fca797
added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents:
18
diff
changeset
|
30 |
v[1] = v[1]-3.6 |
4e0955fca797
added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents:
18
diff
changeset
|
31 |
v[2] = -v[2] |
18 | 32 |
vertices.append(tuple(v)) |
33 |
vertex_num -= 1 |
|
34 |
||
35 |
# read faces |
|
36 |
while (face_num): |
|
37 |
tokens = fp.readline().split() |
|
38 |
if (tokens[0] != "3"): |
|
39 |
print "ply warning: faces of %d vertices not supported" % tokens[0] |
|
40 |
f = [vertices[int(x)] for x in tokens[1:4]] |
|
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
41 |
face = Triangle(NormalVertex(f[0]), NormalVertex(f[1]), NormalVertex(f[2]), mat) |
18 | 42 |
rt.addshape(face) |
43 |
face_num -= 1 |
|
44 |
||
45 |
rt = Raytracer() |
|
46 |
mat = Material(colour=(0.9, 0.9, 0.9)) |
|
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
47 |
LoadStanfordPlyFile(rt, mat, "../models/dragon/dragon_vrip_res2.ply", 29.0) |
18 | 48 |
|
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
49 |
light1 = Light(position=(-5.0, 2.0, 8.0), colour=(0.9, 0.3, 0.2)) |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
50 |
rt.addlight(light1) |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
51 |
|
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
52 |
light2 = Light(position=(3.0, 0.0, 9.0), colour=(0.0, 1.0, 0.2)) |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
53 |
rt.addlight(light2) |
18 | 54 |
|
55 |
imagesize = (800, 600) |
|
56 |
data = rt.render(imagesize) |
|
57 |
img = Image.fromstring("RGB", imagesize, data) |
|
58 |
img.save('dragon.png') |