コード例 #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
コード例 #2
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)
コード例 #3
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
コード例 #4
0
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')
コード例 #5
0
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')
コード例 #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')
コード例 #7
0
    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')
コード例 #8
0
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')
コード例 #9
0
from raytracer.canvas import write_ppm_to_file
from raytracer.lights import PointLight
from raytracer.patterns import *
from raytracer.matrices import scaling, rotation_y, rotation_z, translation, view_transform
from raytracer.scene import World
from raytracer.shapes import Cube, Plane
from raytracer.tuples import Color, Point, Vector

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

    cube = Cube()
    cube.material.color = Color(.8, 0, 0)
    cube.material.reflective = .6
    cube.transformation = translation(0, 1, 0) * rotation_y(pi / 4)

    world = World()
    world.add(floor, cube)
    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))

    canvas = camera.render(world)

    write_ppm_to_file(canvas.to_ppm(),
                      f'..{sep}..{sep}resources{sep}cubes.ppm')
コード例 #10
0
from raytracer.tuples import Color, Point, Vector


def csg():
    s1 = Sphere()
    s1.transformation = translation(-0.375, 0, 0)
    s1.material.color = Color(1, 0, 0)
    s1.material.transparency = .8
    s2 = Sphere()
    s2.transformation = translation(0.375, 0, 0)
    s2.material.pattern = GradientPattern(Color(1, 0, 0), Color(1, 1, 0))
    s2.material.reflective = .8
    s2.material.transparency = .8
    c = Csg(OperationType.INTERSECTION, s1, s2)
    c.transformation = scaling(1, 1.5, 1) * rotation_y(-pi / 4)
    return c


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

    camera = Camera(300, 200, 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}csg.ppm')
コード例 #11
0
        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)

    write_ppm_to_file(canvas.to_ppm(),
                      f'..{sep}..{sep}resources{sep}book_cover.ppm')
コード例 #12
0
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 translation, view_transform
from raytracer.scene import World
from raytracer.shapes import Cone
from raytracer.tuples import Color, Point, Vector

if __name__ == '__main__':
    cone = Cone(closed=False)
    cone.maximum = 1
    cone.minimum = -1
    cone.material.color = Color(.8, 0, 0)
    cone.transformation = translation(0, 1, 0)

    world = World()
    world.add(cone)
    world.light_source = PointLight(Point(-5, 5, -5), 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))

    canvas = camera.render(world)

    write_ppm_to_file(canvas.to_ppm(), f'..{sep}..{sep}resources{sep}cone.ppm')
コード例 #13
0
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.scene import World
from raytracer.shapes import *
from raytracer.tuples import Color, Point, Vector


def triangle():
    t = Triangle(Point(0, 1, 0), Point(-1, 0, 0), Point(1, 0, 0))
    t.material.color = Color(1, 0, 0)
    return t


if __name__ == '__main__':
    world = World()
    world.add(triangle())
    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}triangle.ppm')
コード例 #14
0
    middle.material.diffuse = 0.7
    middle.material.specular = 0.3

    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.5, 1, 0.1)
    right.material.diffuse = 0.7
    right.material.specular = 0.3

    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, left_wall, right_wall, middle, left, right)
    world.light_source = PointLight(Point(-10, 10, -10), Color.white())

    camera = Camera(100, 50, 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}snapshot.ppm')