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)
def test_lighting_with_surface_in_shadow(self): eyev = Vector(0, 0, -1) normalv = Vector(0, 0, -1) light = PointLight(Point(0, 0, -10), Color(1, 1, 1)) inShadow = True result = self.m.lighting(eyev, normalv, self.pos, light, inShadow) self.assertEqual(result, Color(0.1, 0.1, 0.1))
def test_inside_intersection_shading(): w = World.default() w.light = PointLight(Point(0, 0.25, 0), Color(1, 1, 1)) r = Ray(Point(0, 0, 0), Vector(0, 0, 1)) shape = w.objects[1] i = Intersection(0.5, shape) comps = i.prepare_computation(r) c = w.shade_hit(comps) assert c == Color(0.90498, 0.90498, 0.90498)
def test_lighting_eye_in_path(): # eye in reflection path s = Sphere() m = Material() p = Point(0, 0, 0) eyev = Vector(0, -math.sqrt(2) / 2, -math.sqrt(2) / 2) normalv = Vector(0, 0, -1) light = PointLight(Point(0, 10, -10), Color(1, 1, 1)) res = m.lighting(s, light, p, eyev, normalv) assert res == Color(1.6364, 1.6364, 1.6364)
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)
def test_lighting_behind_surface(): # eye behind surface s = Sphere() m = Material() p = Point(0, 0, 0) eyev = Vector(0, 0, -1) normalv = Vector(0, 0, -1) light = PointLight(Point(0, 0, 10), Color(1, 1, 1)) res = m.lighting(s, light, p, eyev, normalv) assert res == Color(0.1, 0.1, 0.1)
def test_lighting_eye_offset(): # eye between light and surface, 45 offset s = Sphere() m = Material() p = Point(0, 0, 0) eyev = Vector(0, math.sqrt(2) / 2, -math.sqrt(2) / 2) normalv = Vector(0, 0, -1) light = PointLight(Point(0, 0, -10), Color(1, 1, 1)) res = m.lighting(s, light, p, eyev, normalv) assert res == Color(1.0, 1.0, 1.0)
def test_lighting_eye_opposite(): # eye opposite surface, 45 offset s = Sphere() m = Material() p = Point(0, 0, 0) eyev = Vector(0, 0, -1) normalv = Vector(0, 0, -1) light = PointLight(Point(0, 10, -10), Color(1, 1, 1)) res = m.lighting(s, light, p, eyev, normalv) assert res == Color(0.7364, 0.7364, 0.7364)
def test_lighting_eye_between(): # eye between light and surface s = Sphere() m = Material() p = Point(0, 0, 0) eyev = Vector(0, 0, -1) normalv = Vector(0, 0, -1) light = PointLight(Point(0, 0, -10), Color(1, 1, 1)) res = m.lighting(s, light, p, eyev, normalv) assert res == Color(1.9, 1.9, 1.9)
def test_surface_shadow(): s = Sphere() m = Material() p = Point(0, 0, 0) eyev = Vector(0, 0, -1) normalv = Vector(0, 0, -1) light = PointLight(Point(0, 0, -10), Color(1, 1, 1)) in_shadow = True result = m.lighting(s, light, p, eyev, normalv, in_shadow) assert result == Color(0.1, 0.1, 0.1)
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
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)
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()
def default(cls): world = cls() world.light = PointLight(Point(-10, 10, -10), Color(1, 1, 1)) m = Material() m.color = Color(0.8, 1.0, 0.6) m.diffuse = 0.7 m.specular = 0.2 s1 = Sphere() s1.set_material(m) s2 = Sphere() s2.set_transform(Scaling(0.5, 0.5, 0.5)) world.objects.extend([s1, s2]) return world
def test_shadow_shade_hit(): w = World() w.light = PointLight(Point(0, 0, -10), Color(1, 1, 1)) s1 = Sphere() s2 = Sphere() s2.set_transform(Translation(0, 0, 10)) w.objects.extend([s1, s2]) r = Ray(Point(0, 0, 5), Vector(0, 0, 1)) i = Intersection(4, s2) comps = i.prepare_computation(r) c = w.shade_hit(comps) print(c) assert c == Color(0.1, 0.1, 0.1)
def test_mutually_reflective_color_at(): w = World() w.light = PointLight(Point(0, 0, 0), Color(1, 1, 1)) lower = Plane() lower.material.reflective = 1 lower.set_transform(Translation(0, -1, 0)) w.objects.append(lower) upper = Plane() upper.material.reflective = 1 upper.set_transform(Translation(0, 1, 0)) w.objects.append(upper) r = Ray(Point(0, 0, 0), Vector(0, 1, 0)) assert w.color_at(r) is not None
def test_lighting_pattern(): s = Sphere() m = Material() m.pattern = StripePattern(Color(1, 1, 1), Color(0, 0, 0)) 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(1, 1, 1)) c1 = m.lighting(s, light, Point(0.9, 0, 0), eyev, normalv, False) c2 = m.lighting(s, light, Point(1.1, 0, 0), eyev, normalv, False) assert c1 == Color(1, 1, 1) assert c2 == Color(0, 0, 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
def test_default_world(): light = PointLight(Point(-10, 10, -10), Color(1, 1, 1)) s1 = Sphere() m = Material() m.color = Color(0.8, 1.0, 0.6) m.diffuse = 0.7 m.specular = 0.2 s1.set_material(m) s2 = Sphere() t = Scaling(0.5, 0.5, 0.5) s2.set_transform(t) w = World.default() assert w.light == light assert s1 in w.objects assert s2 in w.objects
def test_lighting_with_eye_between_light_and_surface_eye_offset_45(self, background): eyev = Vector(0, sqrt(2) / 2, -sqrt(2) / 2) normalv = Vector(0, 0, -1) light = PointLight(Point(0, 0, -10), Color(1, 1, 1)) result = background['m'].lighting(Sphere(), light, background['position'], eyev, normalv) assert result == Color(1.0, 1.0, 1.0)
def build_light(_data: dict) -> PointLight: return PointLight(point_from_tuple(_data['at']), Color(*_data['intensity']))
from raytracer.parser import Parser from raytracer.world import World from raytracer.camera import Camera from raytracer.lights import PointLight from raytracer.base import Point, Color, ViewTransform, Vector f = open("tests/obj_files/face.obj", "r") p = Parser(f) w = World() w.objects.append(p.obj_to_group()) w.light = PointLight(Point(10, 5, 5), Color(1, 1, 1)) c = Camera(10, 10, 0.785) c.transform = ViewTransform(Point(-6, 6, -10), Point(6, 0, 6), Vector(-0.45, 1, 0)) canvas = c.render(w) with open("images/triangl.ppm", "w") as f: f.write(canvas.to_ppm()) # This is wayyy too slow we're gonna need a triangle mesh or something
def test_light_attributes(): position = Point(0, 0, 0) intensity = Color(1, 1, 1) light = PointLight(position, intensity) assert light.position == position assert light.intensity == intensity
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.materials import Material from raytracer.lights import PointLight WALL_SIZE = 7 CANVAS_SIZE = 100 PIXEL_SIZE = WALL_SIZE / CANVAS_SIZE HALF = WALL_SIZE / 2 canvas = Canvas(CANVAS_SIZE, CANVAS_SIZE) s = Sphere() m = Material(color=Color(1, 0.2, 1)) s.material = m light_postion = Point(-10, 5, -10) light_color = Color(1, 1, 1) light = PointLight(light_postion, light_color) ray_origin = Point(0, 0, -5) wall_z = 10 for y in range(0, CANVAS_SIZE): world_y = HALF - PIXEL_SIZE * y for x in range(0, CANVAS_SIZE): world_x = -HALF + PIXEL_SIZE * x position = Point(world_x, world_y, wall_z) v = position - ray_origin r = Ray(ray_origin, v.normalize()) xs = s.intersects(r) hit = xs.hit() if hit: point = r.position(hit.t)
right = Sphere() right.set_transform(Translation(1.5, 0.5, -0.5)) right.set_transform(Scaling(0.5, 0.5, 0.5)) right.material.color = Color(0.5, 1, 0.1) right.material.diffuse = 0.7 right.material.specular = 0.3 left = Sphere() left.set_transform(Translation(-1.5, 0.33, -0.75)) left.set_transform(Scaling(0.33, 0.33, 0.33)) left.material.color = Color(1, 0.8, 0.1) left.material.diffuse = 0.7 left.material.specular = 0.3 world.objects.append(middle) world.objects.append(right) world.objects.append(left) world.light = PointLight(Point(-10, 10, -10), Color(1, 1, 1)) camera = Camera( 200, 100, math.pi / 3, ViewTransform(Point(0, 1.5, -5), Point(0, 1, 0), Vector(0, 1, 0)), ) canvas = camera.render(world) with open("second_scene.ppm", "w") as ppm_file: ppm_file.write(canvas.to_ppm())
def test_lighting_eye_in_path_of_reflection_vector(self, background): eyev = Vector(0, -sqrt(2) / 2, -sqrt(2) / 2) normalv = Vector(0, 0, -1) light = PointLight(Point(0, 10, -10), Color(1, 1, 1)) result = background['m'].lighting(Sphere(), light, background['position'], eyev, normalv) assert result == Color(1.6364, 1.6364, 1.6364)
def test_lighting_light_behind_surface(self, background): eyev = Vector(0, 0, -1) normalv = Vector(0, 0, -1) light = PointLight(Point(0, 0, 10), Color(1, 1, 1)) result = background['m'].lighting(Sphere(), light, background['position'], eyev, normalv) assert result == 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 test_lighting_eye_opposite_surface_light_offset_45(self, background): eyev = Vector(0, 0, -1) normalv = Vector(0, 0, -1) light = PointLight(Point(0, 10, -10), Color(1, 1, 1)) result = background['m'].lighting(Sphere(), light, background['position'], eyev, normalv) assert result == Color(0.7364, 0.7364, 0.7364)