def test_pattern_with_both_object_and_pattern_transformation(self): obj = Sphere() obj.transformation = scaling(2, 2, 2) pattern = test_pattern() pattern.transformation = translation(0.5, 1, 1.5) c = pattern.pattern_at_shape(obj, Point(2.5, 3, 3.5)) assert c == Color(0.75, 0.5, 0.25)
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
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_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)
from raytracer.patterns import * from raytracer.matrices import scaling, rotation_y, rotation_x, translation, view_transform from raytracer.scene import World from raytracer.shapes import Sphere, Plane from raytracer.tuples import Color, Point, Vector if __name__ == '__main__': floor = Plane() floor.material.color = Color(.1, 1, .1) floor.material.reflective = 0.9 floor.material.transparency = 1 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
from raytracer.lights import PointLight from raytracer.materials import Material from raytracer.matrices import scaling, rotation_y, rotation_x, translation, view_transform from raytracer.scene import World from raytracer.shapes import Sphere, Plane from raytracer.tuples import Color, Point, Vector if __name__ == '__main__': floor = Plane() floor.material = Material() floor.material.color = Color(1, 0.9, 0.9) floor.material.specular = 0 middle = Sphere() middle.transformation = translation(-0.5, 1, 0.5) middle.material = Material() middle.material.color = Color(0.5, 0, 0) middle.material.diffuse = 0.7 middle.material.specular = 0.3 middle.material.reflective = 1 right = Sphere() right.transformation = translation(1.5, 0.5, -0.5) * scaling(0.5, 0.5, 0.5) right.material = Material() right.material.color = Color(0.2, 0.2, 0.8) right.material.diffuse = 0.7 right.material.specular = 0.3 right.material.reflective = .5 left = Sphere()
def test_pattern_with_object_transformation(self): obj = Sphere() obj.transformation = scaling(2, 2, 2) pattern = test_pattern() c = pattern.pattern_at_shape(obj, Point(2, 3, 4)) assert c == Color(1, 1.5, 2)
from raytracer.matrices import scaling, rotation_z, shearing from raytracer.rays import Ray from raytracer.shapes import Sphere from raytracer.tuples import Point, Color if __name__ == '__main__': ray_origin = Point(0, 0, -5) wall_z = 10 wall_size = 7.0 canvas_pixels = 100 pixel_size = wall_size / canvas_pixels half = wall_size / 2 canvas = Canvas(canvas_pixels, canvas_pixels) red = Color(1, 0, 0) sphere = Sphere() sphere.transformation = scaling(1, .5, 1).shear(1, 0, 0, 0, 0, 0) for y in range(canvas_pixels): world_y = half - pixel_size * y for x in range(canvas_pixels): world_x = -half + pixel_size * x position = Point(world_x, world_y, wall_z) ray = Ray(ray_origin, (position - ray_origin).normalize()) if sphere.intersect(ray).hit(): canvas.write_pixel(x, y, red) write_ppm_to_file(canvas.to_ppm(), f'..{sep}..{sep}resources{sep}circle.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.materials import Material from raytracer.matrices import scaling, rotation_y, rotation_x, translation, view_transform from raytracer.scene import World from raytracer.shapes import Sphere from raytracer.tuples import Color, Point, Vector if __name__ == '__main__': floor = Sphere() floor.transformation = scaling(10, 0.01, 10) floor.material = Material() floor.material.color = Color(1, 0.9, 0.9) floor.material.specular = 0 left_wall = Sphere() left_wall.transformation = translation(0, 0, 5) * \ rotation_y(-pi / 4) * rotation_x(pi / 2) * \ scaling(10, 0.01, 10) left_wall.material = floor.material right_wall = Sphere() right_wall.transformation = translation(0, 0, 5) * \ rotation_y(pi / 4) * rotation_x(pi / 2) * \ scaling(10, 0.01, 10) right_wall.material = floor.material middle = Sphere()