Example #1
0
class Tracer:
    _world = World()
    _camera = Camera()
    _image = Image(1,1)
    
    def __init__(self, world = None, camera = None):
        self._world = world
        self._camera = camera
        self._drawn = False
        self._image = None
    
    def set_camera(self, camera = Camera()):
        self._camera = camera
        return self
    
    def set_world(self, world = World()):
        self._world = world
        return self
    
    def draw(self, passes = 8):
        if self._world == None:
            self.set_world()
        if self._camera == None:
            self.set_camera()
        ppu = self._camera.ppu
        width = int(ppu * self._camera.width)
        height = int(ppu * self._camera.height)
        self._image = Image(width, height)
        c = Color()
        t_0 = time.time()
        for y in range(height):
            lt0 = time.time()
            print 'drawing line', y + 1, 'of', height
            for x in range(width):
                c.set_rgb(0.0,0.0,0.0)
                for p in range(passes):
                    c = c + self._world.sample(self._camera.get_ray(x,y))
                self._image.set_pixel(x,y,c.dim(1.0 / passes))
            lt1 = time.time()
            ltime = lt1 - lt0
            ttime = lt1 - t_0
            lleft = height - 1 - y
            mleft1 = ltime * lleft / 60
            mleft2 = ttime / (y + 1) * lleft / 60
            print 'line took {0:.3} seconds.'.format(ltime),
            print '{0:.5} to {1:.5} minutes left'.format(mleft1, mleft2)
        self._drawn = True
        return self
    
    def write(self, filename = _default_filename, gamma = None):
        if not self._drawn:
            raise self._image
        if not gamma == None:
            self._image.gamma(gamma)
        print 'encoding as {}...'.format(filename)
        self._image.toPNG().write(filename)
        print '        ALL DONE!'
        return self
Example #2
0
class Stereo:
    world = World()
    camera_l = Camera()
    camera_r = Camera()
    image = Image(1, 1)

    def set_camera(self, camera=Camera()):
        if camera == None:
            camera = Camera()
        crp = camera.o.dup().add(camera.right, 0.1)
        crf = camera.focus.dup().add(camera.right, 0.1)
        clp = camera.o.dup().add(camera.right, -0.1)
        clf = camera.focus.dup().add(camera.right, -0.1)
        self.camera_r = camera.dup().set_focus(crf).set_position(crp)
        self.camera_l = camera.dup().set_focus(clf).set_position(clp)
        return self

    def set_world(self, world=World()):
        self.world = world
        return self

    def __init__(self, world=None, camera=None):
        self.world = world
        self.set_camera(camera)
        self._drawn = False
        self.image = None

    def draw(self, passes=8):
        if self.world == None:
            self.set_world()
        ppu = self.camera_r.ppu
        w = int(ppu * self.camera_r.width)
        width = 2 * w
        height = int(ppu * self.camera_r.height)
        self.image = Image(width, height)
        c = Color()
        for y in range(height):
            print 'drawing line', y + 1, 'of', height
            for x in range(w):
                # draw pixel from left camera
                c.set_rgb(0.0, 0.0, 0.0)
                for p in range(passes):
                    c = c + self.world.sample(self.camera_l.get_ray(x, y))
                self.image.set_pixel(x, y, c.dim(1.0 / passes))
                # draw pixel from right camera
                c.set_rgb(0.0, 0.0, 0.0)
                for p in range(passes):
                    c = c + self.world.sample(self.camera_r.get_ray(x, y))
                self.image.set_pixel(x + w, y, c.dim(1.0 / passes))

        self._drawn = True
        return self

    def write(self, filename=_default_filename, gamma=None):
        if not self._drawn:
            raise self.image
        if not gamma == None:
            self.image.gamma(gamma)
        print 'encoding as {}...'.format(filename)
        self.image.toPNG().write(filename)
        print '        ALL DONE!'
        return self