예제 #1
0
 def test_render_world_with_camera(self, default_world):
     w = default_world
     c = Camera(11, 11, pi / 2)
     _from = Point(0, 0, -5)
     to = Point(0, 0, 0)
     up = Vector(0, 2, 0)
     c.transformation = view_transform(_from, to, up)
     image = c.render(w)
     assert image.pixel_at(5, 5) == Color(0.38066, 0.47583, 0.2855)
예제 #2
0
 def test_arbitrary_view_transformation(self):
     _from = Point(1, 3, 2)
     to = Point(4, -2, 8)
     up = Vector(1, 1, 0)
     t = view_transform(_from, to, up)
     assert t == Matrix([[-0.50709, 0.50709, 0.67612, -2.36643],
                         [0.76772, 0.60609, 0.12122, -2.82843],
                         [-0.35857, 0.59761, -0.71714, 0.0],
                         [0.0, 0.0, 0.0, 1.0]])
예제 #3
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')
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')
예제 #5
0
 def test_view_transformation_moves_the_world(self):
     _from = Point(0, 0, 8)
     to = Point(0, 0, 0)
     up = Vector(0, 1, 0)
     t = view_transform(_from, to, up)
     assert t == translation(0, 0, -8)
예제 #6
0
 def test_view_transformation_looking_in_z_positive_direction(self):
     _from = Point(0, 0, 0)
     to = Point(0, 0, 1)
     up = Vector(0, 1, 0)
     t = view_transform(_from, to, up)
     assert t == scaling(-1, 1, -1)  # reflecting across z and x
예제 #7
0
 def test_view_transformation_for_default_orientation(self):
     _from = Point(0, 0, 0)
     to = Point(0, 0, -1)
     up = Vector(0, 1, 0)
     t = view_transform(_from, to, up)
     assert t == Matrix.identity()