new demo: bunny.py pyrit
authorRadek Brich <radek.brich@devl.cz>
Fri, 07 Dec 2007 14:59:14 +0100
branchpyrit
changeset 26 9073320e9f4c
parent 25 b8232edee786
child 27 e9bb83c2b8b9
new demo: bunny.py
demos/bunny.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/bunny.py	Fri Dec 07 14:59:14 2007 +0100
@@ -0,0 +1,57 @@
+#!/usr/bin/python
+
+# this demo needs bunny model from
+# http://graphics.stanford.edu/data/3Dscanrep/
+
+import sys
+sys.path.append(open('ModulePath').read().strip())
+
+from raytracer import Raytracer, Light, Sphere, Triangle, Material
+import Image
+
+def LoadStanfordPlyFile(rt, mat, filename, scale):
+	vertices = []
+	fp = file(filename)
+	# read header
+	tokens = (0,)
+	while (tokens[0] != "end_header"):
+		tokens = fp.readline().split()
+		if (tokens[0] == "element"):
+			if (tokens[1] == "vertex"):
+				vertex_num = int(tokens[2])
+			if (tokens[1] == "face"):
+				face_num = int(tokens[2])
+
+	# read vertices
+	while (vertex_num):
+		tokens = fp.readline().split()
+		v = [scale*float(x) for x in tokens[0:3]]
+		v[0] = -v[0]-1
+		v[1] = v[1]-3
+		vertices.append(tuple(v))
+		vertex_num -= 1
+
+	# read faces
+	while (face_num):
+		tokens = fp.readline().split()
+		if (tokens[0] != "3"):
+			print "ply warning: faces of %d vertices not supported" % tokens[0]
+		f = [vertices[int(x)] for x in tokens[1:4]]
+		face = Triangle(f[0], f[2], f[1], mat)
+		rt.addshape(face)
+		face_num -= 1
+
+rt = Raytracer()
+mat = Material(colour=(0.9, 0.9, 0.9))
+LoadStanfordPlyFile(rt, mat, "../models/bunny/bun_zipper_res3.ply", 29.0)
+
+light = Light(position=(-5.0, 2.0, 10.0), colour=(0.9, 0.3, 0.6))
+rt.addlight(light)
+
+light2 = Light(position=(4.0, -3.0, 10.0), colour=(0.2, 0.9, 0.5))
+rt.addlight(light2)
+
+imagesize = (800, 600)
+data = rt.render(imagesize)
+img = Image.fromstring("RGB", imagesize, data)
+img.save('bunny.png')