demos/render_nff.py
author Radek Brich <radek.brich@devl.cz>
Sun, 31 May 2009 16:53:05 +0200
branchpyrit
changeset 99 f3abdaa2e8fb
parent 98 64638385798a
permissions -rwxr-xr-x
build script: updated for latest SCons, moved config.h to build/, help and clean targets does not run configure any more, fixed GCC check, added check for zlib
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
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
     3
# Usage: render_nff.py [scene.nff] [output.png]
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
     4
# reads scene from nff file and render it to PNG file
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
     5
# if no output file name is given, it makes one from input name
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
     6
# reads from stdin by default
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
     7
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
     8
# see http://tog.acm.org/resources/SPD/ for nff format description
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 96
diff changeset
     9
# cylinders are currently 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
    10
90
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    11
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
    12
from math import pi
90
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    13
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
    14
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
rt = Raytracer()
90
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    16
top = KdTree()
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    17
rt.setTop(top)
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    18
rt.setCamera(Camera())
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    19
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
    20
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
    21
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
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
    23
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
f = sys.stdin
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    25
fout = "render_nff.png"
90
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    26
if len(sys.argv) > 1:
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    27
	f = open(sys.argv[1])
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    28
	fout = sys.argv[1].rsplit('.',1)[0] + '.png'
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    29
if len(sys.argv) > 2:
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    30
	fout = sys.argv[2]
90
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    31
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
    32
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
    33
	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
    34
	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
    35
		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
    36
	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
    37
	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
    38
		# 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
    39
		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
    40
		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
    41
		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
    42
		# 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
    43
		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
    44
		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
    45
		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
    46
		# 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
    47
		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
    48
		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
    49
		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
    50
		# 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
    51
		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
    52
		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
    53
		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
    54
		# 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
    55
		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
    56
		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
    57
		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
    58
		# 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
    59
		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
    60
		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
    61
		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
    62
		# 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
    63
		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
    64
		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
    65
		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
    66
	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
    67
		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
    68
	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
    69
		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
    70
		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
    71
	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
    72
		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
    73
		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
    74
		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
    75
		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
    76
	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
    77
		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
    78
		radius = float(ln[4])
96
9eb71e76c7fd added Python binding for material.h classes
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    79
		rt.addShape(Sphere(center=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
    80
	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
    81
		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
    82
		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
    83
		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
    84
			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
    85
			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
    86
			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
    87
		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
    88
		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
    89
			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
    90
	elif ln[0] == 'pp':	# Polygonal patch
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    91
		mat.setSmooth(True)
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    92
		vertex_count = int(ln[1])
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    93
		vertices = []
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    94
		for i in range(vertex_count):
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    95
			ln = f.readline().split()
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    96
			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
    97
			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
    98
			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
    99
		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
   100
		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
   101
			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
   102
	elif ln[0] == '#':	# Comment
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 73
diff changeset
   103
		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
   104
	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
   105
		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
   106
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
   107
90
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   108
top.optimize()
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   109
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   110
sampler = DefaultSampler(imagesize)
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   111
rt.setSampler(sampler)
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   112
rt.render()
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   113
sampler.getPixmap().writePNG(fout)