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