demos/render_nff.py
author Radek Brich <radek.brich@devl.cz>
Sat, 10 May 2008 14:29:37 +0200
branchpyrit
changeset 95 ca7d4c665531
parent 90 f6a72eb99631
child 96 9eb71e76c7fd
permissions -rwxr-xr-x
build script fixes, add ldflags build option update and enhance demos fix bug in 4x grid oversampling warn if writePNG called while compiled without libpng make shapes in ShapeList const and add many other const needed due to snowball effect slightly optimize Camera::makeRayPacket using _mm_shuffle_ps make Vector SIMD vectorization disabled by default (causes problems) fix bug in implicit reflection of transmissive surfaces, when surface's reflection parameter is set to zero
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
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    21
fout = "render_nff.png"
90
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])
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    24
	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
    25
if len(sys.argv) > 2:
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    26
	fout = sys.argv[2]
90
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    27
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
    28
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
    29
	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
    30
	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
    31
		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
    32
	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
    33
	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
    34
		# 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
		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
    36
		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
    37
		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
    38
		# 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
		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] == '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
    41
		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
    42
		# 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
		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] == '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
    45
		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
    46
		# 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
		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] == '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
    49
		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
    50
		# 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
		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] == '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
    53
		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
    54
		# 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
		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] == '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
    57
		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
    58
		# 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
    59
		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
    60
		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
    61
		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
    62
	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
    63
		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
    64
	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
    65
		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
    66
		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
    67
	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
    68
		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
    69
		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
    70
		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
    71
		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
    72
	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
    73
		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
    74
		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
    75
		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
    76
	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
    77
		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
    78
		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
    79
		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
    80
			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
    81
			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
    82
			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
    83
		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
    84
		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
    85
			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
    86
	elif ln[0] == 'pp':	# Polygonal patch
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    87
		mat.setSmooth(True)
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    88
		vertex_count = int(ln[1])
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    89
		vertices = []
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    90
		for i in range(vertex_count):
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    91
			ln = f.readline().split()
a5127346fbcd correct camera angle of view
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    92
			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
    93
			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
    94
			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
    95
		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
    96
		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
    97
			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
    98
	elif ln[0] == '#':	# Comment
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 73
diff changeset
    99
		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
   100
	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
   101
		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
   102
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
   103
90
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   104
top.optimize()
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   105
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   106
sampler = DefaultSampler(imagesize)
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   107
rt.setSampler(sampler)
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   108
rt.render()
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   109
sampler.getPixmap().writePNG(fout)