def main(resolution): logging.basicConfig( level=logging.INFO, format= '%(asctime)s %(levelname)s %(filename)s:%(lineno)s %(name)s %(message)s' ) shape = ClippedShape(CrookedPlane()) shape.material = Material() shape.material.color = Color(1, .2, 1) shape.material.transparency = 1.0 shape.material.refractive_index = 1.5 objects = (shape, ) rad = 6 camera_height = .8 * rad frames = [] for t in np.arange(0, math.pi, .1 * math.pi): logging.info("New Frame %s", t) camera_position = Point(rad * math.cos(t), rad * math.sin(t), camera_height) vt = view_transform(camera_position, Point(0, 0, 0), Vector(0, 0, 1)) canvas = make_single_frame(objects, vt, resolution) frames.append(canvas._image) path = Path(__file__).parent / "chap11.gif" imageio.mimsave(str(path), frames, duration=0.25)
def main(): logging.basicConfig( level=logging.INFO, format= '%(asctime)s %(levelname)s %(filename)s:%(lineno)s %(name)s %(message)s' ) shape = ClippedShape(CrookedPlane()) shape.material = Material() shape.material.color = Color(1, .2, 1) #shape.material.pattern = gradient_pattern(BLUE, GREEN) #shape.material.pattern.transform = Scaling(4, 2, 2) objects = (shape, ) rad = 6 camera_height = .8 * rad frames = [] for t in np.arange(0, math.pi, .1 * math.pi): logging.info("New Frame %s", t) camera_position = Point(rad * math.cos(t), rad * math.sin(t), camera_height) vt = view_transform(camera_position, Point(0, 0, 0), Vector(0, 0, 1)) canvas = make_single_frame(objects, vt) frames.append(canvas._image) imageio.mimsave("examples/crooked_plane.gif", frames, duration=0.25)
def make_single_frame(objects, view_transform_, camera_resolution): lights = [ point_light(Point(10, 10, 15), WHITE), point_light(Point(-10, -10, 15), WHITE) ] world = World(objects=objects, light=lights) camera = Camera(*camera_resolution, math.pi / 3) camera.transform = view_transform_ canvas = camera.render(world) return canvas
def ray_for_pixel(self, x, y): xoffset = (x + .5) * self.pixel_size yoffset = (y + .5) * self.pixel_size world_x = self.half_width - xoffset world_y = self.half_height - yoffset pixel = self.transform.inverse() * Point(world_x, world_y, -1) origin = self.transform.inverse() * Point(0, 0, 0) direction = normalize(pixel - origin) return Ray(origin, direction)
def test_ray_misses_to_wing2(): # Given cp = DefaultCrookedPlane() ray = Ray(Point(1, 0, 0), Vector(0, 1, -1)) # When xs = cp.intersect_wing2(ray) assert xs is None
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 test_ray_intersects_wing2(): # Given cp = DefaultCrookedPlane() ray = Ray(Point(-1, 0, -1), Vector(0, 0, 1)) # When xs = cp.intersect_wing2(ray) # Then assert xs.t == 1
def test_ray_parallel_to_wing2(): # Given cp = DefaultCrookedPlane() ray = Ray(Point(-1, 1, 0), Vector(0, 1, 1)) # When xs = cp.intersect_wing2(ray) # Then assert xs is None
def test_ray_misses_stem(): # Given cp = DefaultCrookedPlane() ray = Ray(Point(0, 1, 0), Vector(1, 0, 1)) # When xs = cp.intersect_stem(ray) # Then assert xs is None
def test_ray_intersects_stem(): # Given cp = DefaultCrookedPlane() ray = Ray(Point(0, 0, -1), Vector(1, 0, 0)) # When xs = cp.intersect_stem(ray) # Then assert xs.t == 0
def test_normal_at_wing2(): # Given p = Point(-3, 5, -5) # When cp = DefaultCrookedPlane() n = cp.normal_at(p) # Then assert n == Vector(0, 1, -1)
def test_normal_at_stem(): # Given p = Point(0, .5, 1) # When cp = DefaultCrookedPlane() n = cp.normal_at(p) # Then assert n == Vector(1, 0, 0)
def main(): c = Canvas(200, 200) adjustment = Vector(c.width/2, c.height/2 ,0) radius = 3 * c.width / 8 center = Point(0, 0, 0) + adjustment c.write_pixel(int(center.x), int(center.y), RED) twelve = Point(0, radius, 0) points = [twelve] for k in range(12): R = rotation_z(k * math.pi / 6) points.append(R * twelve) points = [p + adjustment for p in points] for p in points: c.write_pixel(int(p.x), int(p.y), GREEN) path = Path(__file__).parent / "chap4.png" canvas_to_png(str(path), c)
def main(camera_resolution): logging.basicConfig( level=logging.INFO, format= '%(asctime)s %(levelname)s %(filename)s:%(lineno)s %(name)s %(message)s' ) objects = (make_floor(), make_middle(), make_right(), make_left()) light = point_light(Point(-10, 10, -10), Color(1, 1, 1)) world = World(objects=objects, light=light) camera = Camera(*camera_resolution, math.pi / 3) camera.transform = view_transform(Point(0, 1 / 5, -5), Point(0, 1, 0), Vector(0, 1, 0)) canvas = camera.render(world) path = Path(__file__).parent / "chap10.png" canvas_to_png(str(path), canvas)
def default_world(): light = point_light(Point(-10, 10, -10), Color(1, 1, 1)) s1 = Sphere() s1.material.color = Color(.8, 1.0, .6) s1.material.diffuse = .7 s1.material.specular = .2 s2 = set_transform(Sphere(), Scaling(.5, .5, .5)) return World(objects=[s1, s2], light=light)
def draw_objects(objects): logging.basicConfig( level=logging.INFO, format= '%(asctime)s %(levelname)s %(filename)s:%(lineno)s %(name)s %(message)s' ) lights = [ point_light(Point(10, 10, 15), WHITE), point_light(Point(-10, -10, 15), WHITE) ] world = World(objects=objects, light=lights) camera = Camera(100, 50, math.pi / 3) camera.transform = view_transform(Point(12, -6, 10), Point(0, 0, 0), Vector(0, 0, 1)) canvas = camera.render(world) path = Path(__file__).parent / "output.png" canvas_to_png(str(path), canvas)
def main(canvas_dimensions): logging.basicConfig( level=logging.INFO, format= '%(asctime)s %(levelname)s %(filename)s:%(lineno)s %(process)d %(name)s %(message)s' ) objects = (make_floor(), make_wall(-math.pi / 4), make_wall(math.pi / 4), make_middle(), make_right(), make_left()) light = point_light(Point(-10, 10, -10), Color(1, 1, 1)) world = World(objects=objects, light=light) camera = Camera(canvas_dimensions[0], canvas_dimensions[1], math.pi / 3) camera.transform = view_transform(Point(0, 1 / 5, -5), Point(0, 1, 0), Vector(0, 1, 0)) start = time.time() canvas = camera.render(world) stop = time.time() logging.info("Rendering took %s seconds", stop - start) path = Path(__file__).parent / "chap7.png" canvas_to_png(str(path), canvas)
def intersect(self, ray): center = Point(0, 0, 0) radius = 1 sphere_to_ray = ray.origin - center a = dot(ray.direction, ray.direction) b = 2 * dot(ray.direction, sphere_to_ray) c = dot(sphere_to_ray, sphere_to_ray) - radius discriminant = b**2 - 4 * a * c if discriminant < 0: return Intersections() t1 = (-b - math.sqrt(discriminant)) / (2 * a) t2 = (-b + math.sqrt(discriminant)) / (2 * a) return Intersections(Intersection(t1, self), Intersection(t2, self))
def main(canvas_dimensions): start = Point(0, 1, 0) velocity = normalize(Vector(1, 1.8, 1)) * 11.25 p = Projectile(start, velocity) gravity = Vector(0, -0.1, 0) wind = Vector(-0.01, 0, 0) e = Environment(gravity, wind) print(canvas_dimensions) c = Canvas(*canvas_dimensions) while p.position.y > 0: x, y = int(p.position.x), c.height - int(p.position.y) print(x, y) print(p.position) c.write_pixel(x, y, Color(0, 0, 1)) p = tick(e, p) print(f"p has landed at {p}") path = Path(__file__).parent / "chap2.png" canvas_to_png(str(path), c)
def test_ray_misses_to_wing2(): ray = Ray(Point(1, 0, 0), Vector(0, 1, -1)) assert [] == intersect_wing2(ray)
def step_impl(context, world, x, y, z, red, green, blue): _world = getattr(context, world) _world.light = point_light(Point(x, y, z), Color(red, green, blue))
def step_impl(context, light, x, y, z, red, green, blue): p = Point(x, y, z) c = Color(red, green, blue) setattr(context, "_p", p) setattr(context, "_c", c) context.execute_steps('given light = point_light(_p, _c)')
def step_impl(context, r, x, y, z, dx, dy, dz): _r = Ray(Point(x, y, z), Vector(dx, dy, dz)) setattr(context, r, _r)
def test_ray_intersects_wing2(): ray = Ray(Point(-1, 0, -1), Vector(0, 0, 1)) assert [1] == intersect_wing2(ray)
def step_impl(context, result, material, object_, light, x, y, z, eyev, normalv, in_shadow): _position = Point(x, y, z) setattr(context, "position", _position) context.execute_steps(f"when {result} = lighting({material}, {object_}, {light}, position, {eyev}, {normalv}, {in_shadow})")
def step_impl(context, c, pattern, shape, x, y, z): _pattern = getattr(context, pattern) _shape = getattr(context, shape) p = Point(x, y, z) _c = pattern_at_shape(_pattern, _shape, p) setattr(context, c, _c)
def step_impl(context, c, pattern, object_, x, y, z): _pattern = getattr(context, pattern) _object = getattr(context, object_) p = Point(x, y, z) _c = pattern_at_shape(_pattern, _object, p) setattr(context, c, _c)
def normal_at(self, point): object_normal = point - Point(0, 0, 0) return object_normal
from pathlib import Path from ray_tracer.canvas import Canvas, write_pixel, canvas_to_png from ray_tracer.colors import RED from ray_tracer.intersections import intersect, hit from ray_tracer.rays import Ray from ray_tracer.shapes import Sphere from ray_tracer.tuples import Point, normalize ray_origin = Point(0, 0, -5) wall_z = 10 wall_size = 7 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)
def test_ray_parallel_to_stem(): ray = Ray(Point(-1, 0, 1), Vector(0, 1, 1)) assert [] == intersect_stem(ray)