Exemplo n.º 1
0
 def test_lighting_with_surface_in_shadow(self, background):
     eyev = Vector(0, 0, -1)
     normalv = Vector(0, 0, -1)
     light = PointLight(Point(0, 0, -10), Color(1, 1, 1))
     in_shadow = True
     result = background['m'].lighting(Sphere(), light, background['position'], eyev, normalv, in_shadow)
     assert result == Color(0.1, 0.1, 0.1)
Exemplo n.º 2
0
 def test_hit_when_intersection_occurs_on_inside(self):
     r = Ray(Point(0, 0, 0), Vector(0, 0, 1))
     shape = Sphere()
     i = Intersection(1, shape)
     comps = i.prepare_computations(r)
     assert comps.point == Point(0, 0, 1)
     assert comps.eyev == Vector(0, 0, -1)
     assert comps.inside
     assert comps.normalv == Vector(0, 0, -1)
Exemplo n.º 3
0
 def test_precomputing_state_of_intersection(self):
     r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
     shape = Sphere()
     i = Intersection(4, shape)
     comps = i.prepare_computations(r)
     assert comps.t == i.t
     assert comps.object == i.object
     assert comps.point == Point(0, 0, -1)
     assert comps.eyev == Vector(0, 0, -1)
     assert comps.normalv == Vector(0, 0, -1)
Exemplo n.º 4
0
 def test_lighting_with_pattern_applied(self):
     m = Material()
     m.pattern = StripePattern(Color.white(), Color.black())
     m.ambient = 1
     m.diffuse = 0
     m.specular = 0
     eyev = Vector(0, 0, -1)
     normalv = Vector(0, 0, -1)
     light = PointLight(Point(0, 0, -10), Color.white())
     c1 = m.lighting(Sphere(), light, Point(0.9, 0, 0), eyev, normalv, False)
     c2 = m.lighting(Sphere(), light, Point(1.1, 0, 0), eyev, normalv, False)
     assert c1 == Color.white()
     assert c2 == Color.black()
Exemplo n.º 5
0
 def test_schlick_approximation_with_small_angle_and_n2_greater_than_n1(
         self, glass_sphere):
     shape = glass_sphere()
     r = Ray(Point(0, 0.99, -2), Vector(0, 0, 1))
     xs = Intersections(Intersection(1.8589, shape))
     comps = xs[0].prepare_computations(r, xs)
     reflectance = comps.schlick()
     assert reflectance == pytest.approx(0.48873, EPSILON)
Exemplo n.º 6
0
 def test_schlick_approximation_with_perpendicular_viewing_angle(
         self, glass_sphere):
     shape = glass_sphere()
     r = Ray(Point(0, 0, 0), Vector(0, 1, 0))
     xs = Intersections(Intersection(-1, shape), Intersection(1, shape))
     comps = xs[1].prepare_computations(r, xs)
     reflectance = comps.schlick()
     assert reflectance == pytest.approx(0.04)
Exemplo n.º 7
0
 def test_hit_should_offset_the_point(self):
     r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
     shape = Sphere()
     shape.transformation = translation(0, 0, 1)
     i = Intersection(5, shape)
     comps = i.prepare_computations(r)
     assert comps.over_point.z < -EPSILON / 2
     assert comps.point.z > comps.over_point.z
Exemplo n.º 8
0
 def test_refracted_color_with_opaque_surface(self, default_world):
     w = default_world
     shape = w.objects[0]
     r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
     xs = Intersections(Intersection(4, shape), Intersection(6, shape))
     comps = xs[0].prepare_computations(r, xs)
     c = w.refracted_color(comps, 5)
     assert c == Color.black()
Exemplo n.º 9
0
 def test_shading_intersection(self, default_world):
     w = default_world
     r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
     shape = w.objects[0]
     i = Intersection(4, shape)
     comps = i.prepare_computations(r)
     c = w.shade_hit(comps)
     assert c == Color(0.38066, 0.47583, 0.2855)
Exemplo n.º 10
0
 def test_color_with_intersection_behind_ray(self, default_world):
     w = default_world
     outer = w.objects[0]
     outer.material.ambient = 1
     inner = w.objects[1]
     inner.material.ambient = 1
     r = Ray(Point(0, 0, 0.75), Vector(0, 0, -1))
     c = w.color_at(r)
     assert c == inner.material.color
Exemplo n.º 11
0
 def test_intersect_world_with_ray(self, default_world):
     w = default_world
     r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
     xs = w.intersect(r)
     assert xs.count == 4
     assert xs[0].t == 4
     assert xs[1].t == 4.5
     assert xs[2].t == 5.5
     assert xs[3].t == 6
Exemplo n.º 12
0
 def test_under_point_is_offset_below_the_surface(self, glass_sphere):
     r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
     shape = glass_sphere()
     shape.transformation = translation(0, 0, 1)
     i = Intersection(5, shape)
     xs = Intersections(i)
     comps = i.prepare_computations(r, xs)
     assert comps.under_point.z > EPSILON / 2
     assert comps.point.z < comps.under_point.z
Exemplo n.º 13
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)
Exemplo n.º 14
0
 def test_schlick_approximation_under_total_internal_reflection(
         self, glass_sphere):
     shape = glass_sphere()
     r = Ray(Point(0, 0, sqrt(2) / 2), Vector(0, 1, 0))
     xs = Intersections(Intersection(-sqrt(2) / 2, shape),
                        Intersection(sqrt(2) / 2, shape))
     comps = xs[1].prepare_computations(r, xs)
     reflectance = comps.schlick()
     assert reflectance == 1.0
Exemplo n.º 15
0
 def test_reflected_color_for_nonreflective_material(self, default_world):
     w = default_world
     r = Ray(Point(0, 0, 0), Vector(0, 0, 1))
     shape = w.objects[1]
     shape.material.ambient = 1
     i = Intersection(1, shape)
     comps = i.prepare_computations(r)
     color = w.reflected_color(comps)
     assert color == Color.black()
Exemplo n.º 16
0
 def test_shading_intersection_from_inside(self, default_world):
     w = default_world
     w.light_source = PointLight(Point(0, 0.25, 0), Color.white())
     r = Ray(Point(0, 0, 0), Vector(0, 0, 1))
     shape = w.objects[1]
     i = Intersection(0.5, shape)
     comps = i.prepare_computations(r)
     c = w.shade_hit(comps)
     assert c == Color(0.90498, 0.90498, 0.90498)
Exemplo n.º 17
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]])
Exemplo n.º 18
0
 def test_refracted_color_at_max_recursive_depth(self, default_world):
     w = default_world
     shape = w.objects[0]
     shape.material.transparency = 1.0
     shape.material.refractive_index = 1.5
     r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
     xs = Intersections(Intersection(4, shape), Intersection(6, shape))
     comps = xs[0].prepare_computations(r, xs)
     c = w.refracted_color(comps, 0)
     assert c == Color.black()
Exemplo n.º 19
0
 def test_reflected_color_at_max_recursive_depth(self, default_world):
     w = default_world
     shape = Plane()
     shape.material.reflective = 0.5
     shape.transformation = translation(0, -1, 0)
     w.add(shape)
     r = Ray(Point(0, 0, -3), Vector(0, -sqrt(2) / 2, sqrt(2) / 2))
     i = Intersection(sqrt(2), shape)
     comps = i.prepare_computations(r)
     color = w.reflected_color(comps, 0)
     assert color == Color.black()
Exemplo n.º 20
0
 def test_shade_hit_with_reflective_material(self, default_world):
     w = default_world
     shape = Plane()
     shape.material.reflective = 0.5
     shape.transformation = translation(0, -1, 0)
     w.add(shape)
     r = Ray(Point(0, 0, -3), Vector(0, -sqrt(2) / 2, sqrt(2) / 2))
     i = Intersection(sqrt(2), shape)
     comps = i.prepare_computations(r)
     color = w.shade_hit(comps)
     assert color == Color(0.87676, 0.92434, 0.82917)
Exemplo n.º 21
0
 def test_reflected_color_for_reflective_material(self, default_world):
     w = default_world
     shape = Plane()
     shape.material.reflective = 0.5
     shape.transformation = translation(0, -1, 0)
     w.add(shape)
     r = Ray(Point(0, 0, -3), Vector(0, -sqrt(2) / 2, sqrt(2) / 2))
     i = Intersection(sqrt(2), shape)
     comps = i.prepare_computations(r)
     color = w.reflected_color(comps)
     assert color == Color(0.19033, 0.23791, 0.14274)
Exemplo n.º 22
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)
Exemplo n.º 23
0
 def test_refracted_color_under_total_internal_reflection(self, default_world):
     w = default_world
     shape = w.objects[0]
     shape.material.transparency = 1.0
     shape.material.refractive_index = 1.5
     r = Ray(Point(0, 0, sqrt(2) / 2), Vector(0, 1, 0))
     xs = Intersections(Intersection(-sqrt(2) / 2, shape), Intersection(sqrt(2) / 2, shape))
     # NOTE: this time you're inside the sphere, so you need
     # to look at the second intersection, xs[1], not xs[0]
     comps = xs[1].prepare_computations(r, xs)
     c = w.refracted_color(comps, 5)
     assert c == Color.black()
Exemplo n.º 24
0
 def test_refracted_color_with_refracted_ray(self, default_world):
     w = default_world
     a = w.objects[0]
     a.material.ambient = 1.0
     a.material.pattern = test_pattern()
     b = w.objects[1]
     b.material.transparency = 1.0
     b.material.refractive_index = 1.5
     r = Ray(Point(0, 0, 0.1), Vector(0, 1, 0))
     xs = Intersections(Intersection(-0.9899, a), Intersection(-0.4899, b),
                        Intersection(0.4899, b), Intersection(0.9899, a))
     comps = xs[2].prepare_computations(r, xs)
     c = w.refracted_color(comps, 5)
     assert c == Color(0, 0.99888, 0.04722)
Exemplo n.º 25
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
Exemplo n.º 26
0
 def test_shade_hit_with_transparent_material(self, default_world):
     w = default_world
     floor = Plane()
     floor.transformation = translation(0, -1, 0)
     floor.material.transparency = 0.5
     floor.material.refractive_index = 1.5
     ball = Sphere()
     ball.material.color = Color(1, 0, 0)
     ball.material.ambient = 0.5
     ball.transformation = translation(0, -3.5, -0.5)
     w.add(floor, ball)
     r = Ray(Point(0, 0, -3), Vector(0, -sqrt(2) / 2, sqrt(2) / 2))
     xs = Intersections(Intersection(sqrt(2), floor))
     comps = xs[0].prepare_computations(r, xs)
     color = w.shade_hit(comps, 5)
     assert color == Color(0.93642, 0.68642, 0.68642)
Exemplo n.º 27
0
 def test_finding_n1_and_n2_at_various_intersections(
         self, glass_sphere, index, n1, n2):
     a = glass_sphere()
     a.transformation = scaling(2, 2, 2)
     a.material.refractive_index = 1.5
     b = glass_sphere()
     b.transformation = translation(0, 0, -0.25)
     b.material.refractive_index = 2.0
     c = glass_sphere()
     c.transformation = translation(0, 0, 0.25)
     c.material.refractive_index = 2.5
     r = Ray(Point(0, 0, -4), Vector(0, 0, 1))
     xs = Intersections(Intersection(2, a), Intersection(2.75, b),
                        Intersection(3.25, c), Intersection(4.75, b),
                        Intersection(5.25, c), Intersection(6, a))
     comps = xs[index].prepare_computations(r, xs)
     assert comps.n1 == n1
     assert comps.n2 == n2
Exemplo n.º 28
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 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 test_create_ray(self):
     origin = Point(1, 2, 3)
     direction = Vector(4, 5, 6)
     r = Ray(origin, direction)
     assert r.origin == origin
     assert r.direction == direction