demos/buddha.py
author Radek Brich <radek.brich@devl.cz>
Sun, 25 Nov 2007 22:22:40 +0100
branchpyrit
changeset 17 5176ba000a67
parent 13 fbd1d2f7d94e
child 19 4e0955fca797
permissions -rwxr-xr-x
fix last leak as reported by valgrind
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
#!/usr/bin/python
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
# this demo needs buddha model from
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
# http://graphics.stanford.edu/data/3Dscanrep/
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
import sys
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
sys.path.append("..")
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
from raytracer import Raytracer, Light, Sphere, Triangle, Material
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
import Image
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
def LoadStanfordPlyFile(rt, mat, filename, scale):
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
	vertices = []
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
	fp = file(filename)
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
	# read header
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
	tokens = (0,)
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
	while (tokens[0] != "end_header"):
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
		tokens = fp.readline().split()
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
		if (tokens[0] == "element"):
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
			if (tokens[1] == "vertex"):
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
				vertex_num = int(tokens[2])
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
			if (tokens[1] == "face"):
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
				face_num = int(tokens[2])
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
	# read vertices
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
	while (vertex_num):
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
		tokens = fp.readline().split()
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
		v = [scale*float(x) for x in tokens[0:3]]
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
		v[0] = -v[0]
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
		v[1] = v[1]-5
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
		v[2] = -v[2]
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
		vertices.append(tuple(v))
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
		vertex_num -= 1
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
	# read faces
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
	while (face_num):
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
		tokens = fp.readline().split()
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
		if (tokens[0] != "3"):
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
			print "ply warning: faces of %d vertices not supported" % tokens[0]
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
		f = [vertices[int(x)] for x in tokens[1:4]]
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
		face = Triangle(f[0], f[1], f[2], mat)
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
		rt.addshape(face)
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
		face_num -= 1
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
rt = Raytracer()
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
mat = Material(colour=(0.9, 0.9, 0.9))
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
LoadStanfordPlyFile(rt, mat, "happy_vrip_res2.ply", 30.0)
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
light = Light(position=(-5.0, 2.0, -8.0), colour=(0.9, 0.3, 0.6))
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
rt.addlight(light)
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
imagesize = (800, 600)
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
data = rt.render(imagesize)
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
img = Image.fromstring("RGB", imagesize, data)
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
img.save('buddha.png')