demos/triangles_sphere.py
branchpyrit
changeset 0 3547b885df7e
child 1 e74bf781067e
equal deleted inserted replaced
-1:000000000000 0:3547b885df7e
       
     1 #!/usr/bin/python2.4
       
     2 
       
     3 import sys
       
     4 sys.path.append("../")
       
     5 
       
     6 from raytracer import Raytracer, Light, Sphere, Triangle, Material
       
     7 import Image
       
     8 
       
     9 def LoadWavefrontObjFile(rt, mat, filename):
       
    10 	vertices = []
       
    11 	fp = file(filename)
       
    12 	while True:
       
    13 		ln = fp.readline()
       
    14 		if ln == "":
       
    15 			break;
       
    16 		ln = ln.split()
       
    17 		if ln[0] == "v":
       
    18 			v = [1.5*float(x) for x in ln[1:4]]
       
    19 			vertices.append(tuple(v))
       
    20 		if ln[0] == "f":
       
    21 			f = [vertices[int(x)-1] for x in ln[1:4]]
       
    22 			face = Triangle(f[0], f[1], f[2], mat)
       
    23 			rt.addshape(face)
       
    24 
       
    25 rt = Raytracer()
       
    26 mat = Material(colour=(0.9, 0.9, 0.9))
       
    27 LoadWavefrontObjFile(rt, mat, "sphere.obj")
       
    28 
       
    29 light1 = Light(position=(0.0, 2.0, -5.0), colour=(0.9, 0.3, 0.6))
       
    30 light1.castshadows(False);
       
    31 rt.addlight(light1)
       
    32 
       
    33 light2 = Light(position=(-2.0, -5.0, -7.0), colour=(0.7, 1.0, 0.3))
       
    34 light2.castshadows(False);
       
    35 rt.addlight(light2)
       
    36 
       
    37 imagesize = (800, 600)
       
    38 data = rt.render(imagesize)
       
    39 img = Image.fromstring("RGB", imagesize, data)
       
    40 img.save('triangles_sphere.png')