def color_at(self, ray, remaining=5): intersections = self.intersect_world(ray) a_hit = hit(intersections) if a_hit is None: return BLACK comps = prepare_computations(a_hit, ray, intersections) return self.shade_hit(comps, remaining)
def cast_this_point(canvas, canvas_x, canvas_y, world_x, world_y): spot_on_wall = Point(world_x, world_y, wall_z) ray = Ray(ray_origin, normalize(spot_on_wall - ray_origin)) xs = intersect(shape, ray) a_hit = hit(xs) if a_hit is not None: color = get_color(a_hit, ray) write_pixel(canvas, canvas_x, canvas_y, color)
def is_shadowed(world, point, light=None): if light is None: # This is pretty inefficient. It recomputes intersections below for each light light = world.light v = light.position - point distance = v.magnitude() direction = v.normalize() r = Ray(point, direction) intersections = intersect_world(world, r) h = hit(intersections) if h is not None and h.t < distance: return True else: return False
def step_impl(context, i, xs): _xs = getattr(context, xs) _i = hit(_xs) setattr(context, i, _i)
canvas_pixels = 100 pixel_size = wall_size / canvas_pixels half = wall_size / 2 canvas = Canvas(canvas_pixels, canvas_pixels) shape = Sphere() for y in range(canvas_pixels - 1): world_y = half - pixel_size * y for x in range(canvas_pixels - 1): world_x = -half + pixel_size * x position = Point(world_x, world_y, wall_z) r = Ray(ray_origin, normalize(position - ray_origin)) xs = intersect(shape, r) if hit(xs) is not None: write_pixel(canvas, x, y, RED) path = Path(__file__).parent / "chap5.png" canvas_to_png(str(path), canvas) # Copyright 2020 Bloomberg Finance L.P. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS,