demos/render_nff.py
author Radek Brich <radek.brich@devl.cz>
Mon, 05 May 2008 15:31:14 +0200
branchpyrit
changeset 92 9af5c039b678
parent 90 f6a72eb99631
child 95 ca7d4c665531
permissions -rwxr-xr-x
add MSVC compiler support, make it default for Windows new header file simd.h for SSE abstraction and helpers add mselect pseudo instruction for common or(and(...), andnot(...)) replace many SSE intrinsics with new names new MemoryPool class (mempool.h) for faster KdNode allocation remove setMaxDepth() from Octree and KdTree, make max_depth const, it should be defined in constructor and never changed, change after building tree would cause error in traversal modify DefaultSampler to generate nice 2x2 packets of samples for packet tracing optimize Box and BBox::intersect_packet add precomputed invdir attribute to RayPacket scons build system: check for pthread library on Windows check for SDL generate include/config.h with variables detected by scons configuration move auxiliary files to build/ add sanity checks add writable operator[] to Vector

#!/usr/bin/python

# read nff data from standart input and render image to render_nff.png
# see http://tog.acm.org/resources/SPD/
# cylinders are not implemented

from pyrit import *
from math import pi
import sys

rt = Raytracer()
top = KdTree()
rt.setTop(top)
rt.setCamera(Camera())

imagesize = (800, 600)

mat = Material(colour=(1.0, 1.0, 1.0))

f = sys.stdin
fbase = "render_nff"
if len(sys.argv) > 1:
	f = open(sys.argv[1])
	fbase = sys.argv[1].rsplit('.',1)[0]

while True:
	line = f.readline()
	if line == "":
		break;
	ln = line.split()
	if ln[0] == 'v':	# Viewpoint location
		# from
		ln = f.readline().split()
		assert ln[0] == 'from'
		eye = (float(ln[1]), float(ln[2]), float(ln[3]))
		# at
		ln = f.readline().split()
		assert ln[0] == 'at'
		lookat = (float(ln[1]), float(ln[2]), float(ln[3]))
		# up
		ln = f.readline().split()
		assert ln[0] == 'up'
		up = (float(ln[1]), float(ln[2]), float(ln[3]))
		# angle
		ln = f.readline().split()
		assert ln[0] == 'angle'
		angle = float(ln[1])
		# hither
		ln = f.readline().split()
		assert ln[0] == 'hither'
		hither = float(ln[1])
		# resolution
		ln = f.readline().split()
		assert ln[0] == 'resolution'
		imagesize = (int(ln[1]), int(ln[2]))
		# set camera as specified
		cam = Camera(eye=eye, lookat=lookat, up=up)
		cam.setAngle(angle/180*pi)
		rt.setCamera(cam)
	elif ln[0] == 'b':	# Background color
		rt.setBgColour((float(ln[1]), float(ln[2]), float(ln[3])))
	elif ln[0] == 'l':	# Light
		pos = (float(ln[1]), float(ln[2]), float(ln[3]))
		rt.addLight(Light(position=pos))
	elif ln[0] == 'f':	# Fill color and shading parameters
		colour = (float(ln[1]), float(ln[2]), float(ln[3]))
		mat = Material(colour=colour)
		mat.setPhong(0,float(ln[4]),float(ln[5]),float(ln[6]))
		mat.setTransmissivity(float(ln[7]),float(ln[8]))
	elif ln[0] == 's':	# Sphere
		center = (float(ln[1]), float(ln[2]), float(ln[3]))
		radius = float(ln[4])
		rt.addShape(Sphere(centre=center, radius=radius, material=mat))
	elif ln[0] == 'p':	# Polygon
		vertex_count = int(ln[1])
		vertices = []
		for i in range(vertex_count):
			ln = f.readline().split()
			vertex = (float(ln[0]), float(ln[1]), float(ln[2]))
			vertices.append(NormalVertex(vertex))
		rt.addShape(Triangle(vertices[0], vertices[1], vertices[2], mat))
		for i in range(vertex_count)[3:]:
			rt.addShape(Triangle(vertices[0], vertices[i-1], vertices[i], mat))
	elif ln[0] == 'pp':	# Polygonal patch
		mat.setSmooth(True)
		vertex_count = int(ln[1])
		vertices = []
		for i in range(vertex_count):
			ln = f.readline().split()
			vertex = (float(ln[0]), float(ln[1]), float(ln[2]))
			normal = (float(ln[3]), float(ln[4]), float(ln[5]))
			vertices.append(NormalVertex(vertex, normal))
		rt.addShape(Triangle(vertices[0], vertices[1], vertices[2], mat))
		for i in range(vertex_count)[3:]:
			rt.addShape(Triangle(vertices[0], vertices[i-1], vertices[i], mat))
	elif ln[0] == '#':	# Comment
		pass
	else:
		print "Not implemented:", line
f.close()

top.optimize()

sampler = DefaultSampler(imagesize)
rt.setSampler(sampler)
rt.render()
sampler.getPixmap().writePNG(fbase+'.png')