demos/dragon.py
author Radek Brich <radek.brich@devl.cz>
Wed, 05 Dec 2007 18:54:23 +0100
branchpyrit
changeset 24 d0d76e8a5203
parent 19 4e0955fca797
child 25 b8232edee786
permissions -rwxr-xr-x
new C++ demo: realtime_dragon.cc class Container moved to its own source file (from kdtree.*) new directory for models and Makefile which downloads and prepares them fixed python module path in some forgotten demos ShapeList moved to scene.h
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
#!/usr/bin/python
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
# this demo needs dragon model from
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
# http://graphics.stanford.edu/data/3Dscanrep/
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
import sys
24
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents: 19
diff changeset
     7
sys.path.append(open('ModulePath').read().strip())
18
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
from raytracer import Raytracer, Light, Sphere, Triangle, Material
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
import Image
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
def LoadStanfordPlyFile(rt, mat, filename, scale):
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
	vertices = []
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
	fp = file(filename)
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
	# read header
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
	tokens = (0,)
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
	while (tokens[0] != "end_header"):
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
		tokens = fp.readline().split()
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
		if (tokens[0] == "element"):
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
			if (tokens[1] == "vertex"):
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
				vertex_num = int(tokens[2])
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
			if (tokens[1] == "face"):
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
				face_num = int(tokens[2])
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
	# read vertices
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
	while (vertex_num):
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
		tokens = fp.readline().split()
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
		v = [scale*float(x) for x in tokens[0:3]]
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    29
		v[0] = -v[0]
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    30
		v[1] = v[1]-3.6
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    31
		v[2] = -v[2]
18
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
		vertices.append(tuple(v))
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
		vertex_num -= 1
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
	# read faces
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
	while (face_num):
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
		tokens = fp.readline().split()
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
		if (tokens[0] != "3"):
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
			print "ply warning: faces of %d vertices not supported" % tokens[0]
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
		f = [vertices[int(x)] for x in tokens[1:4]]
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
		face = Triangle(f[0], f[1], f[2], mat)
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
		rt.addshape(face)
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
		face_num -= 1
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
rt = Raytracer()
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
mat = Material(colour=(0.9, 0.9, 0.9))
24
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents: 19
diff changeset
    47
LoadStanfordPlyFile(rt, mat, "../models/dragon/dragon_vrip_res4.ply", 29.0)
18
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
    49
light = Light(position=(-5.0, 2.0, 8.0), colour=(0.9, 0.3, 0.6))
18
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
rt.addlight(light)
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
imagesize = (800, 600)
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
data = rt.render(imagesize)
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
img = Image.fromstring("RGB", imagesize, data)
25b7c445cf61 new demo: dragon.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
img.save('dragon.png')