Beispiel #1
0
def default_world():
    w = World()
    w.light_source = PointLight(Point(-10, 10, -10), Color.white())
    s1 = Sphere()
    s1.material.color = Color(0.8, 1.0, 0.6)
    s1.material.diffuse = 0.7
    s1.material.specular = 0.2
    s2 = Sphere()
    s2.transformation = scaling(0.5, 0.5, 0.5)
    w.add(s1, s2)
    return w
Beispiel #2
0
    def test_color_at_with_mutually_reflective_surfaces(self):
        w = World()
        w.light_source = PointLight(Point(0, 0, 0), Color.white())
        lower = Plane()
        lower.material.reflective = 1
        lower.transformation = translation(0, -1, 0)
        upper = Plane()
        upper.material.reflective = 1
        upper.transformation = translation(0, 1, 0)
        w.add(lower, upper)
        r = Ray(Point(0, 0, 0), Vector(0, 1, 0))

        # Avoid Infinite Recursion
        w.color_at(r)
        assert True
Beispiel #3
0
 def test_shade_hit_is_given_an_intersection_in_shadow(self):
     w = World()
     w.light_source = PointLight(Point(0, 0, -10), Color.white())
     s1 = Sphere()
     s2 = Sphere()
     s2.transformation = translation(0, 0, 10)
     w.add(s1, s2)
     r = Ray(Point(0, 0, 5), Vector(0, 0, 1))
     i = Intersection(4, s2)
     comps = i.prepare_computations(r)
     c = w.shade_hit(comps)
     assert c == Color(0.1, 0.1, 0.1)
from math import pi
from os import sep
from raytracer.camera import Camera
from raytracer.canvas import write_ppm_to_file
from raytracer.lights import PointLight
from raytracer.matrices import *
from raytracer.obj_file import parse_obj_file
from raytracer.scene import World
from raytracer.tuples import Color, Point, Vector


def teapot():
    parser = parse_obj_file(f'..{sep}resources{sep}Sting-Sword-lowpoly.obj')
    return parser.obj_to_group()


if __name__ == '__main__':
    world = World()
    world.add(teapot())
    world.light_source = PointLight(Point(-5, 5, -5), Color.white())

    camera = Camera(150, 100, pi / 3)
    camera.transformation = view_transform(Point(0, 1.5, -10), Point(0, 1, 0),
                                           Vector(0, 1, 0))

    canvas = camera.render(world)

    write_ppm_to_file(canvas.to_ppm(),
                      f'..{sep}..{sep}resources{sep}sting.ppm')
def hexagon_side():
    side = Group()
    side.add_children(hexagon_corner(), hexagon_edge())
    return side


def hexagon():
    _hex = Group()
    for n in range(6):
        side = hexagon_side()
        side.transformation = rotation_y(n * pi / 3)
        _hex.add_children(side)
    _hex.transformation = rotation_x(-pi / 6) * translation(0, 1, 0)
    return _hex


if __name__ == '__main__':
    world = World()
    world.add(hexagon())
    world.light_source = PointLight(Point(-5, 5, -5), Color.white())

    camera = Camera(160, 120, pi / 3)
    camera.transformation = view_transform(Point(0, 1.5, -10), Point(0, 1, 0),
                                           Vector(0, 1, 0))

    canvas = camera.render(world)

    write_ppm_to_file(canvas.to_ppm(),
                      f'..{sep}..{sep}resources{sep}hexagon.ppm')
Beispiel #6
0
    floor.material.refractive_index = 1.333

    under = Sphere()
    under.transformation = translation(0, -.5, 0) * scaling(.25, .25, .25)
    under.material.color = Color(0, .8, 0)
    under.material.refractive_index = 1.5
    under.material.ambient = .8

    above = Sphere()
    above.transformation = translation(1, .5, 0) * scaling(.5, .5, .5)
    above.material.color = Color(.2, 0, 0)
    above.material.diffuse = .8
    above.material.ambient = .5

    bottom = Plane()
    bottom.transformation = translation(0, -1.25, 0)
    bottom.material.pattern = StripePattern(Color.black(), Color(0, .2, .2))
    bottom.material.diffuse = .25
    bottom.material.ambient = .25

    world = World()
    world.add(floor, under, above, bottom)
    world.light_source = PointLight(Point(-10, 10, -10), Color.white())

    camera = Camera(300, 200, pi / 3)
    camera.transformation = view_transform(Point(0, 1.5, -5), Point(0, 1, 0), Vector(0, 1, 0))

    canvas = camera.render(world)

    write_ppm_to_file(canvas.to_ppm(), f'..{sep}..{sep}resources{sep}reflect_refract.ppm')
    middle.material.color = Color(0.5, 0, 0)
    middle.material.diffuse = 0.7
    middle.material.specular = 0.3
    middle.material.reflective = 1

    right = Sphere()
    right.transformation = translation(1.5, 0.5, -0.5) * scaling(0.5, 0.5, 0.5)
    right.material = Material()
    right.material.color = Color(0.2, 0.2, 0.8)
    right.material.diffuse = 0.7
    right.material.specular = 0.3
    right.material.reflective = .5

    left = Sphere()
    left.transformation = translation(-1.5, 0.33, -0.75) * scaling(0.33, 0.33, 0.33)
    left.material = Material()
    left.material.color = Color(1, 0.8, 0.1)
    left.material.diffuse = 0.7
    left.material.specular = 0.3

    world = World()
    world.add(floor, middle, left, right)
    world.light_source = PointLight(Point(-10, 10, -10), Color.white())

    camera = Camera(240, 120, pi / 3)
    camera.transformation = view_transform(Point(0, 1.5, -5), Point(0, 1, 0), Vector(0, 1, 0))

    canvas = camera.render(world)

    write_ppm_to_file(canvas.to_ppm(), f'..{sep}..{sep}resources{sep}reflective.ppm')
from raytracer.lights import PointLight
from raytracer.patterns import *
from raytracer.matrices import scaling, rotation_x, rotation_y, rotation_z, translation, view_transform
from raytracer.scene import World
from raytracer.shapes import Cylinder, Plane
from raytracer.tuples import Color, Point, Vector

if __name__ == '__main__':
    floor = Plane()
    floor.material.color = Color(.8, .8, .8)
    floor.material.reflective = .2

    cylinder = Cylinder(closed=True)
    cylinder.maximum = 0.25
    cylinder.material.color = Color(.8, 0, 0)
    cylinder.material.reflective = .6
    cylinder.transformation = translation(0, 1, 0)

    world = World()
    world.add(floor, cylinder)
    world.light_source = PointLight(Point(-10, 10, -10), Color.white())

    camera = Camera(160, 120, pi / 3)
    camera.transformation = view_transform(Point(0, 1.5, -5), Point(
        0, 1, 0), Vector(0, 1, 0)) * rotation_y(-pi / 4) * rotation_x(-pi / 6)

    canvas = camera.render(world)

    write_ppm_to_file(canvas.to_ppm(),
                      f'..{sep}..{sep}resources{sep}cylinders.ppm')
Beispiel #9
0
    shape.transformation = build_transformation(_data, 'transform')
    if isinstance(_data['material'], str):
        shape.material = constants[_data['material']]
    else:
        shape.material = build_material({'value': _data['material']})
    return shape


if __name__ == '__main__':
    with open(f'..{sep}resources{sep}cover.yaml') as f:
        data = load(f)
        pprint.pprint(data)

    camera = build_camera(data[0])

    world = World()
    world.light_source = build_light(data[1])

    # skip second light source... raytracer doesn't implement this yet

    for element in data[3:]:
        if 'define' in element:
            if element['define'].endswith('material'):
                constants[element['define']] = build_material(element)
            else:
                constants[element['define']] = build_transformation(
                    element, 'value')
        else:
            world.add(build_shape(element))

    canvas = camera.render(world)
Beispiel #10
0
 def test_create_world(self):
     w = World()
     assert len(w.objects) == 0
     assert w.light_source is None