demos/render_nff.py
author Radek Brich <radek.brich@devl.cz>
Tue, 29 Apr 2008 23:31:08 +0200
branchpyrit
changeset 90 f6a72eb99631
parent 75 20dee9819b17
child 95 ca7d4c665531
permissions -rwxr-xr-x
rename Python module from 'raytracer' to 'pyrit' improve Python binding: - new objects: Container, Octree, KdTree, Shape, Pixmap, Sampler, DefaultSampler - all shapes are now subclasses of Shape - clean, remove redundant Getattr's - Raytracer render method now just wraps C++ method without doing any additional work adjust all demos for changes in Python binding, remove PIL dependency add demo_PIL.py as a example of pyrit + PIL usage render_nff.py now either loads file given as a argument or reads input from stdin otherwise fix bug in pixmap float to char conversion
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
#!/usr/bin/python
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
# read nff data from standart input and render image to render_nff.png
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
# see http://tog.acm.org/resources/SPD/
73
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
     5
# cylinders are not implemented
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
90
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
     7
from pyrit import *
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
from math import pi
90
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
     9
import sys
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
rt = Raytracer()
90
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    12
top = KdTree()
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    13
rt.setTop(top)
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    14
rt.setCamera(Camera())
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    15
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
imagesize = (800, 600)
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
mat = Material(colour=(1.0, 1.0, 1.0))
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
f = sys.stdin
90
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    21
fbase = "render_nff"
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    22
if len(sys.argv) > 1:
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    23
	f = open(sys.argv[1])
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    24
	fbase = sys.argv[1].rsplit('.',1)[0]
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    25
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
while True:
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
	line = f.readline()
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
	if line == "":
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
		break;
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
	ln = line.split()
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
	if ln[0] == 'v':	# Viewpoint location
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
		# from
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
		ln = f.readline().split()
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
		assert ln[0] == 'from'
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
		eye = (float(ln[1]), float(ln[2]), float(ln[3]))
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
		# at
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
		ln = f.readline().split()
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
		assert ln[0] == 'at'
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
		lookat = (float(ln[1]), float(ln[2]), float(ln[3]))
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
		# up
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
		ln = f.readline().split()
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
		assert ln[0] == 'up'
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
		up = (float(ln[1]), float(ln[2]), float(ln[3]))
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
		# angle
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
		ln = f.readline().split()
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
		assert ln[0] == 'angle'
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
		angle = float(ln[1])
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
		# hither
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
		ln = f.readline().split()
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
		assert ln[0] == 'hither'
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
		hither = float(ln[1])
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
		# resolution
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
		ln = f.readline().split()
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
		assert ln[0] == 'resolution'
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
		imagesize = (int(ln[1]), int(ln[2]))
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
		# set camera as specified
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
		cam = Camera(eye=eye, lookat=lookat, up=up)
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
		cam.setAngle(angle/180*pi)
75
20dee9819b17 unify capitalization of method names in C++ and Python
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    59
		rt.setCamera(cam)
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
	elif ln[0] == 'b':	# Background color
75
20dee9819b17 unify capitalization of method names in C++ and Python
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    61
		rt.setBgColour((float(ln[1]), float(ln[2]), float(ln[3])))
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
	elif ln[0] == 'l':	# Light
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    63
		pos = (float(ln[1]), float(ln[2]), float(ln[3]))
75
20dee9819b17 unify capitalization of method names in C++ and Python
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    64
		rt.addLight(Light(position=pos))
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
	elif ln[0] == 'f':	# Fill color and shading parameters
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
		colour = (float(ln[1]), float(ln[2]), float(ln[3]))
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
		mat = Material(colour=colour)
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
		mat.setPhong(0,float(ln[4]),float(ln[5]),float(ln[6]))
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    69
		mat.setTransmissivity(float(ln[7]),float(ln[8]))
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
	elif ln[0] == 's':	# Sphere
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
		center = (float(ln[1]), float(ln[2]), float(ln[3]))
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
		radius = float(ln[4])
75
20dee9819b17 unify capitalization of method names in C++ and Python
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    73
		rt.addShape(Sphere(centre=center, radius=radius, material=mat))
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
	elif ln[0] == 'p':	# Polygon
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
		vertex_count = int(ln[1])
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
		vertices = []
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
		for i in range(vertex_count):
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    78
			ln = f.readline().split()
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    79
			vertex = (float(ln[0]), float(ln[1]), float(ln[2]))
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    80
			vertices.append(NormalVertex(vertex))
75
20dee9819b17 unify capitalization of method names in C++ and Python
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    81
		rt.addShape(Triangle(vertices[0], vertices[1], vertices[2], mat))
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
		for i in range(vertex_count)[3:]:
75
20dee9819b17 unify capitalization of method names in C++ and Python
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    83
			rt.addShape(Triangle(vertices[0], vertices[i-1], vertices[i], mat))
73
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    84
	elif ln[0] == 'pp':	# Polygonal patch
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    85
		mat.setSmooth(True)
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    86
		vertex_count = int(ln[1])
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    87
		vertices = []
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    88
		for i in range(vertex_count):
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    89
			ln = f.readline().split()
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    90
			vertex = (float(ln[0]), float(ln[1]), float(ln[2]))
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    91
			normal = (float(ln[3]), float(ln[4]), float(ln[5]))
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    92
			vertices.append(NormalVertex(vertex, normal))
75
20dee9819b17 unify capitalization of method names in C++ and Python
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    93
		rt.addShape(Triangle(vertices[0], vertices[1], vertices[2], mat))
73
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    94
		for i in range(vertex_count)[3:]:
75
20dee9819b17 unify capitalization of method names in C++ and Python
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    95
			rt.addShape(Triangle(vertices[0], vertices[i-1], vertices[i], mat))
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 73
diff changeset
    96
	elif ln[0] == '#':	# Comment
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 73
diff changeset
    97
		pass
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    98
	else:
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    99
		print "Not implemented:", line
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   100
f.close()
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
90
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   102
top.optimize()
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   103
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   104
sampler = DefaultSampler(imagesize)
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   105
rt.setSampler(sampler)
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   106
rt.render()
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   107
sampler.getPixmap().writePNG(fbase+'.png')