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_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
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')
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')
if isinstance(_data['material'], str): shape.material = constants[_data['material']] else: 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)