demos/bunny.py
branchpyrit
changeset 28 ffe83ca074f3
parent 26 9073320e9f4c
child 29 574c34441a1c
equal deleted inserted replaced
27:e9bb83c2b8b9 28:ffe83ca074f3
     4 # http://graphics.stanford.edu/data/3Dscanrep/
     4 # http://graphics.stanford.edu/data/3Dscanrep/
     5 
     5 
     6 import sys
     6 import sys
     7 sys.path.append(open('ModulePath').read().strip())
     7 sys.path.append(open('ModulePath').read().strip())
     8 
     8 
     9 from raytracer import Raytracer, Light, Sphere, Triangle, Material
     9 from raytracer import Raytracer, Light, Sphere, Triangle, NormalVertex, Material
    10 import Image
    10 import Image
    11 
    11 
    12 def LoadStanfordPlyFile(rt, mat, filename, scale):
    12 def LoadStanfordPlyFile(rt, mat, filename, scale):
    13 	vertices = []
    13 	vertices = []
       
    14 	normals = []
       
    15 	vertex_face_num = []
    14 	fp = file(filename)
    16 	fp = file(filename)
    15 	# read header
    17 	# read header
    16 	tokens = (0,)
    18 	tokens = (0,)
    17 	while (tokens[0] != "end_header"):
    19 	while (tokens[0] != "end_header"):
    18 		tokens = fp.readline().split()
    20 		tokens = fp.readline().split()
    21 				vertex_num = int(tokens[2])
    23 				vertex_num = int(tokens[2])
    22 			if (tokens[1] == "face"):
    24 			if (tokens[1] == "face"):
    23 				face_num = int(tokens[2])
    25 				face_num = int(tokens[2])
    24 
    26 
    25 	# read vertices
    27 	# read vertices
    26 	while (vertex_num):
    28 	num = vertex_num
       
    29 	while (num):
    27 		tokens = fp.readline().split()
    30 		tokens = fp.readline().split()
    28 		v = [scale*float(x) for x in tokens[0:3]]
    31 		v = [scale*float(x) for x in tokens[0:3]]
    29 		v[0] = -v[0]-1
    32 		v[0] = -v[0]-1
    30 		v[1] = v[1]-3
    33 		v[1] = v[1]-3
    31 		vertices.append(tuple(v))
    34 		vertices.append(NormalVertex(tuple(v)))
    32 		vertex_num -= 1
    35 		normals.append([0.,0.,0.])
       
    36 		vertex_face_num.append(0)
       
    37 		num -= 1
    33 
    38 
    34 	# read faces
    39 	# read faces
    35 	while (face_num):
    40 	while (face_num):
    36 		tokens = fp.readline().split()
    41 		tokens = fp.readline().split()
    37 		if (tokens[0] != "3"):
    42 		if (tokens[0] != "3"):
    38 			print "ply warning: faces of %d vertices not supported" % tokens[0]
    43 			print "ply warning: faces of %d vertices not supported" % tokens[0]
    39 		f = [vertices[int(x)] for x in tokens[1:4]]
    44 		v = [vertices[int(x)] for x in tokens[1:4]]
    40 		face = Triangle(f[0], f[2], f[1], mat)
    45 		face = Triangle(v[0], v[2], v[1], mat)
       
    46 		n = face.getNormal()
       
    47 		for x in tokens[1:4]:
       
    48 			for i in range(3):
       
    49 				normals[int(x)][i] += n[i]
       
    50 			vertex_face_num[int(x)] += 1
       
    51 		face.setSmooth()
    41 		rt.addshape(face)
    52 		rt.addshape(face)
    42 		face_num -= 1
    53 		face_num -= 1
    43 
    54 
       
    55 	# interpolate normals at vertices
       
    56 	num = 0
       
    57 	while (num < vertex_num):
       
    58 		if (vertex_face_num[num] > 0):
       
    59 			for i in range(3):
       
    60 				normals[num][i] /= vertex_face_num[num]
       
    61 		vertices[num].setNormal(tuple(normals[num]))
       
    62 		num += 1
       
    63 
    44 rt = Raytracer()
    64 rt = Raytracer()
    45 mat = Material(colour=(0.9, 0.9, 0.9))
    65 mat = Material(colour=(0.9, 0.9, 0.9))
    46 LoadStanfordPlyFile(rt, mat, "../models/bunny/bun_zipper_res3.ply", 29.0)
    66 LoadStanfordPlyFile(rt, mat, "../models/bunny/bun_zipper.ply", 29.0)
    47 
    67 
    48 light = Light(position=(-5.0, 2.0, 10.0), colour=(0.9, 0.3, 0.6))
    68 light = Light(position=(-5.0, 2.0, 10.0), colour=(0.9, 0.3, 0.6))
    49 rt.addlight(light)
    69 rt.addlight(light)
    50 
    70 
    51 light2 = Light(position=(4.0, -3.0, 10.0), colour=(0.2, 0.9, 0.5))
    71 light2 = Light(position=(4.0, -3.0, 10.0), colour=(0.2, 0.9, 0.5))