def test_lighting_with_the_light_behind_the_surface(material, position): eyev = vector(0, 0, -1) normalv = vector(0, 0, -1) light = PointLight(point(0, 0, 10), color(1, 1, 1)) result = material.lighting(light, position, eyev, normalv, False) assert (np.array_equal(color(0.1, 0.1, 0.1), result))
def test_lighting_with_the_surface_in_shadow(material, position): eyev = vector(0, 0, -1) normalv = vector(0, 0, -1) light = PointLight(point(0, 0, -10), color(1, 1, 1)) in_shadow = True result = material.lighting(light, position, eyev, normalv, in_shadow) assert (np.allclose(color(0.1, 0.1, 0.1), result))
def test_lighting_with_the_eye_opposite_surface_light_offest_45_degrees( material, position): eyev = vector(0, 0, -1) normalv = vector(0, 0, -1) light = PointLight(point(0, 10, -10), color(1, 1, 1)) result = material.lighting(light, position, eyev, normalv, False) assert (np.allclose(color(0.7364, 0.7364, 0.7364), result))
def test_lighting_with_the_eye_in_the_path_of_the_reflection_vector( material, position): 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)) result = material.lighting(light, position, eyev, normalv, False) assert (np.allclose(color(1.6364, 1.6364, 1.6364), result))
def test_lighting_with_the_eye_between_the_light_and_the_surface_and_wyw_offset_45_degrees( material, position): 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)) result = material.lighting(light, position, eyev, normalv, False) assert (np.array_equal(color(1.0, 1.0, 1.0), result))
def test_shading_an_intersection_from_the_inside(): w = default_world() w._light = PointLight(point(0, 0.25, 0), color(1, 1, 1)) r = Ray(point(0, 0, 0), vector(0, 0, 1)) shape = w[1] i = Intersection(0.5, shape) comps = i.prepare_computations(r) c = w.shade_hit(comps) assert(np.allclose(color(0.90498, 0.90498, 0.90498), c))
def test_shade_hit_is_given_an_intersection_in_shadow(): light = PointLight(point(0, 0, -10), color(1, 1, 1)) s1 = Sphere() s2 = Sphere(transformation=translation(0, 0, 10)) w = World(light, 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(np.allclose(color(0.1, 0.1, 0.1), c))
def default_world(): light = PointLight(point(-10, 10, -10), white) m = Material(color=color(0.8, 1.0, 0.6), diffuse=0.7, specular=0.2) s1 = Sphere(material=m) s2 = Sphere(transformation=scaling(0.5, 0.5, 0.5)) return World(light, s1, s2)
def test_shading_an_intersection(): w = default_world() r = Ray(point(0, 0, -5), vector(0, 0, 1)) shape = w[0] i = Intersection(4, shape) comps = i.prepare_computations(r) c = w.shade_hit(comps) assert(np.allclose(color(0.38066, 0.47583, 0.2855), c, atol=0.0001))
def test_rendering_a_world_with_a_camera(): w = default_world() from_ = point(0, 0, -5) to = point(0, 0, 0) up = vector(0, 1, 0) c = Camera(11, 11, math.pi/2, transform=view_transformation(from_, to, up)) image = c.render(w) assert(np.allclose(image.pixel_at(5, 5), color( 0.38066, 0.47583, 0.2855), atol=0.00001))
def test_constructing_the_PPM_pixel_data(): canvas = Canvas(5, 3) c1 = color(1.5, 0, 0) c2 = color(0, 0.5, 0) c3 = color(-0.5, 0, 1) canvas.write_pixel(0, 0, c1) canvas.write_pixel(2, 1, c2) canvas.write_pixel(4, 2, c3) actualPPM = canvas.to_PPM() expectedPPM = """P3 5 3 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 """ assert (expectedPPM == actualPPM)
def test_the_color_when_the_ray_hits(): w = default_world() r = Ray(point(0, 0, -5), vector(0, 0, 1)) c = w.color_at(r) assert(np.allclose(color(0.38066, 0.47583, 0.2855), c, atol=0.0001))
from pytracer.spheres import Sphere from pytracer.transformations import scaling, rotation_y, rotation_x, translation, concat, view_transformation from pytracer.materials import Material from pytracer.colors import color from pytracer.camera import Camera from pytracer.tuples import point, vector from pytracer.lights import PointLight from pytracer.world import World import math floor_material = Material(color=color(1, 0.9, 0.9), specular=0) floor = Sphere(transformation=scaling(10, 0.01, 10), material=floor_material) left_wall_transformation = concat(translation(0, 0, 5), rotation_y(-math.pi / 4), rotation_x(math.pi / 2), scaling(10, 0.01, 10)) left_wall = Sphere(transformation=left_wall_transformation, material=floor_material) right_wall_transform = concat(translation(0, 0, 5), rotation_y(math.pi / 4), rotation_x(math.pi / 2), scaling(10, 0.01, 10)) right_wall = Sphere(transformation=right_wall_transform, material=floor_material) middle_transform = translation(-0.5, 1, 0.5) middle_material = Material(color=color(0.1, 1, 0.5), diffuse=0.7, specular=0.3) middle = Sphere(material=middle_material, transformation=middle_transform) right_transform = concat(translation(1.5, 0.5, -0.5), scaling(0.5, 0.5, 0.5)) right_material = Material(color=color(0.5, 1, 0.1), diffuse=0.7, specular=0.3)