demos/plyreader.py
author Radek Brich <radek.brich@devl.cz>
Fri, 14 Dec 2007 10:34:31 +0100
branchpyrit
changeset 38 5d043eeb09d9
parent 29 574c34441a1c
child 41 c1080cb5bd6d
permissions -rw-r--r--
realtime_dragon demo: now fullsize model + octree realtime_bunny demo: bigger resolution Box, Sphere: implemented AABB intersection new stop condition for octree building (when number of shapes in children >= 6x shapes in parent node) fixes for octree traversal
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
from raytracer import Triangle, NormalVertex
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
def LoadStanfordPlyFile(rt, filename, mat, smooth, scale=(1,1,1), trans=(0,0,0)):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
	if (type(scale) == float or type(scale) == int):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
		scale = (scale,)*3
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
	vertices = []
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
	normals = []
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
	vertex_face_num = []
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
	fp = file(filename)
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
	# read header
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
	tokens = (0,)
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
	while (tokens[0] != "end_header"):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
		tokens = fp.readline().split()
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
		if (tokens[0] == "element"):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
			if (tokens[1] == "vertex"):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
				vertex_num = int(tokens[2])
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
			if (tokens[1] == "face"):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
				face_num = int(tokens[2])
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
	# read vertices
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
	num = vertex_num
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
	while (num):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
		tokens = fp.readline().split()
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
		v = [float(x) for x in tokens[0:3]]
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
		v[0] = scale[0]*v[0] + trans[0]
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
		v[1] = scale[1]*v[1] + trans[1]
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
		v[2] = scale[2]*v[2] + trans[2]
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
		vertices.append(NormalVertex(tuple(v)))
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
		normals.append([0.,0.,0.])
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
		vertex_face_num.append(0)
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
		num -= 1
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
	# read faces
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
	while (face_num):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
		tokens = fp.readline().split()
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
		if (tokens[0] != "3"):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
			print "ply warning: faces of %d vertices not supported" % tokens[0]
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
		v = [vertices[int(x)] for x in tokens[1:4]]
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
		face = Triangle(v[0], v[2], v[1], mat)
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
		n = face.getNormal()
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
		for x in tokens[1:4]:
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
			for i in range(3):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
				normals[int(x)][i] += n[i]
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
			vertex_face_num[int(x)] += 1
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
		if (smooth):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
			face.setSmooth()
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
		rt.addshape(face)
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
		face_num -= 1
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
	# interpolate normals at vertices
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
	num = 0
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
	while (num < vertex_num):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
		if (vertex_face_num[num] > 0):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
			for i in range(3):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
				normals[num][i] /= vertex_face_num[num]
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
		vertices[num].setNormal(tuple(normals[num]))
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
		num += 1