demos/buddha.py
author Radek Brich <radek.brich@devl.cz>
Fri, 30 Nov 2007 00:44:51 +0100
branchpyrit
changeset 21 79b516a3803d
parent 19 4e0955fca797
child 24 d0d76e8a5203
permissions -rwxr-xr-x
naive color driven sub-sampling slightly optimized KdTree::nearest_intersection fixed bug in Box::intersect fixed stripes on spheres in spheres_ao.py (was caused by AO distance) new KdTree property: max_depth minor changes in realtime.py
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]]
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    29
		v[1] = v[1]-3
13
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
		vertices.append(tuple(v))
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
		vertex_num -= 1
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
	# read faces
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
	while (face_num):
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
		tokens = fp.readline().split()
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
		if (tokens[0] != "3"):
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
			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
    38
		f = [vertices[int(x)] for x in tokens[1:4]]
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
		face = Triangle(f[0], f[1], f[2], mat)
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
		rt.addshape(face)
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
		face_num -= 1
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
rt = Raytracer()
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
mat = Material(colour=(0.9, 0.9, 0.9))
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    45
LoadStanfordPlyFile(rt, mat ,"happy_vrip_res2.ply", 20.0)
13
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    47
light = Light(position=(-5.0, 2.0, 8.0), colour=(0.9, 0.3, 0.6))
13
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
rt.addlight(light)
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
imagesize = (800, 600)
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
data = rt.render(imagesize)
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
img = Image.fromstring("RGB", imagesize, data)
fbd1d2f7d94e new python demo: buddha
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
img.save('buddha.png')