demos/bunny.py
author Radek Brich <radek.brich@devl.cz>
Sat, 08 Dec 2007 12:37:45 +0100
branchpyrit
changeset 28 ffe83ca074f3
parent 26 9073320e9f4c
child 29 574c34441a1c
permissions -rwxr-xr-x
smooth triangles (aka Phong shading) extend Python binding to support vertex normals and smooth triangles make bunny.py and realtime_dragon smooth, and fix other demos for new triangle constructor add Vector::operator/=
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
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
     9
from raytracer import Raytracer, Light, Sphere, Triangle, NormalVertex, Material
26
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 = []
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    14
	normals = []
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    15
	vertex_face_num = []
26
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
	fp = file(filename)
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
	# read header
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
	tokens = (0,)
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
	while (tokens[0] != "end_header"):
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
		tokens = fp.readline().split()
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
		if (tokens[0] == "element"):
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
			if (tokens[1] == "vertex"):
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
				vertex_num = int(tokens[2])
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
			if (tokens[1] == "face"):
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
				face_num = int(tokens[2])
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
	# read vertices
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    28
	num = vertex_num
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    29
	while (num):
26
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
		tokens = fp.readline().split()
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
		v = [scale*float(x) for x in tokens[0:3]]
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
		v[0] = -v[0]-1
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
		v[1] = v[1]-3
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    34
		vertices.append(NormalVertex(tuple(v)))
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    35
		normals.append([0.,0.,0.])
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    36
		vertex_face_num.append(0)
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    37
		num -= 1
26
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
	# read faces
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
	while (face_num):
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
		tokens = fp.readline().split()
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
		if (tokens[0] != "3"):
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
			print "ply warning: faces of %d vertices not supported" % tokens[0]
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    44
		v = [vertices[int(x)] for x in tokens[1:4]]
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    45
		face = Triangle(v[0], v[2], v[1], mat)
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    46
		n = face.getNormal()
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    47
		for x in tokens[1:4]:
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    48
			for i in range(3):
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    49
				normals[int(x)][i] += n[i]
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    50
			vertex_face_num[int(x)] += 1
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    51
		face.setSmooth()
26
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
		rt.addshape(face)
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
		face_num -= 1
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    55
	# interpolate normals at vertices
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    56
	num = 0
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    57
	while (num < vertex_num):
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    58
		if (vertex_face_num[num] > 0):
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    59
			for i in range(3):
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    60
				normals[num][i] /= vertex_face_num[num]
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    61
		vertices[num].setNormal(tuple(normals[num]))
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    62
		num += 1
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    63
26
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
rt = Raytracer()
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
mat = Material(colour=(0.9, 0.9, 0.9))
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    66
LoadStanfordPlyFile(rt, mat, "../models/bunny/bun_zipper.ply", 29.0)
26
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
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
    69
rt.addlight(light)
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
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
    72
rt.addlight(light2)
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
imagesize = (800, 600)
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
data = rt.render(imagesize)
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
img = Image.fromstring("RGB", imagesize, data)
9073320e9f4c new demo: bunny.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
img.save('bunny.png')