def render(self, camera, world): """render world into canvas""" image = Canvas(camera.horizontal_size_px, camera.vertical_size_px) for y in range(camera.vertical_size_px): for x in range(camera.horizontal_size_px): ray = camera.ray_for_pixel(x, y) color = world.color_at(ray) image.set_pixel(x, y, color) return image
def render_part(self, pid, camera, world, hmin, hmax, part_file): """ """ tqdm_text = "#" + "{}".format(pid).zfill(3) image = Canvas(camera.horizontal_size_px, hmax - hmin) pbar = tqdm(total=hmax - hmin, desc=tqdm_text, position=pid) for y in range(hmin, hmax): for x in range(camera.horizontal_size_px): ray = camera.ray_for_pixel(x, y) color = world.color_at(ray) image.set_pixel(x, y - hmin, color) pbar.update(1) with open(part_file, 'w') as outf: outf.write(image.pixels_to_string())
def main(): p = dict(position=point(0, 1, 0), velocity=multiply(normalize(vector(1, 1.8, 0)), 11.25)) e = dict(gravity=vector(0, -0.1, 0), wind=vector(-0.01, 0, 0)) c = Canvas(900, 550) while p['position'][1] > 0.0: print( f"position {p['position'][0]}, {p['position'][1]}, {p['position'][2]}" ) c.set_pixel(round(p['position'][0]), c.height - round(p['position'][1]), color(0.0, 1.0, 0.0)) p = tick(e, p) with open('cannon.ppm', 'w') as out_file: out_file.write(c.to_ppm())
def main(): c = Canvas(500, 500) p = point(0, 0, 1) translate = translation(250, 0, 250) scale = scaling(100, 0, 100) for h in range(12): r = rotation_y(h * pi / 6) transform = multiply_matrix(translate, multiply_matrix(scale, r)) p2 = multiply_tuple(transform, p) print(f"position ({p2[0]}, {p2[1]}, {p2[2]})") c.set_pixel(round(p2[0]), c.height - round(p2[2]), color(0.0, 1.0, 0.0)) with open('clock.ppm', 'w') as out_file: out_file.write(c.to_ppm())
def main(): canvas_pixels = 500 canvas = Canvas(canvas_pixels, canvas_pixels) shape = Sphere() # assign material shape.material = Material() shape.material.color = color(1, 0.2, 1) light_position = point(-10, 10, -10) light_color = color(1, 1, 1) light = PointLight(light_position, light_color) ray_origin = point(0, 0, -5) wall_z = 10 wall_size = 7.0 pixel_size = wall_size / canvas_pixels half = wall_size / 2 for y in range(canvas_pixels): world_y = half - pixel_size * y for x in range(canvas_pixels): world_x = -half + pixel_size * x pos = point(world_x, world_y, wall_z) r = Ray(ray_origin, normalize(subtract(pos, ray_origin))) xs = shape.intersect(r) shape_hit = hit(xs) if shape_hit is not None: hit_point = r.position_at(shape_hit.t) normal = shape_hit.object.normal_at(hit_point) eye = negate(r.direction) px_color = lighting(shape_hit.object.material, shape_hit.object, light, hit_point, eye, normal) canvas.set_pixel(x, y, px_color) with open('render_phong_sphere.ppm', 'w') as out_file: out_file.write(canvas.to_ppm())
def main(): canvas_pixels = 400 canvas = Canvas(canvas_pixels, canvas_pixels) red = color(1, 0, 0) shape = Sphere() # shrink it along the y axis #shape.set_transform(scaling(1, 0.5, 1)) # shrink it along the x axis #shape.set_transform(scaling(0.5, 1, 1)) # shrink it, and rotate it! # shape.set_transform(multiply_matrix(rotation_z(pi / 4), scaling(0.5, 1, # 1))) # shrink it, and skew it! # shape.set_transform( # multiply_matrix(shearing(1, 0, 0, 0, 0, 0), scaling(0.5, 1, 1))) ray_origin = point(0, 0, -5) wall_z = 10 wall_size = 7.0 pixel_size = wall_size / canvas_pixels half = wall_size / 2 for y in range(canvas_pixels): world_y = half - pixel_size * y for x in range(canvas_pixels): world_x = -half + pixel_size * x pos = point(world_x, world_y, wall_z) r = Ray(ray_origin, normalize(subtract(pos, ray_origin))) xs = shape.intersect(r) if hit(xs) is not None: canvas.set_pixel(x, y, red) with open('render_sphere.ppm', 'w') as out_file: out_file.write(canvas.to_ppm())
from math import pi as PI from core import Vector, Canvas, Camera, Scene, LightSource from core.dynamics import rotate, oscillate, combine from shapes import Sphere, Cuboid # Camera canvas = Canvas(Vector(60, 30)) camera = Camera(Vector(3, 3, 0), Vector(0, 0, 1), canvas, PI / 3, 13) camera.advance = combine(rotate(Vector(3, 3, 5), -2 * PI / 60), ) main_light = LightSource(Vector(2, 4.5, 1), 9) secondary_light = LightSource(Vector(6, 3, 7), 4) lights = [main_light, secondary_light] # Objects body = Cuboid(Vector(3, 3, 5), Vector(1.2, 3, 1)) head = Sphere(Vector(3, 4.5, 5), 0.8) balls = [Sphere(Vector(x, 1, 5), 1) for x in [2, 4]] wall = Cuboid(Vector(3, 3, 11), Vector(10, 10, 1)) corner = Cuboid(Vector(-3, 3, 5), Vector(1, 10, 10)) floor = Cuboid(Vector(3, 0, 5), Vector(10, 1, 10)) objects = [body, head, wall, corner, floor] + balls # Scene scene = Scene("bigben", camera, lights, objects, frame_count=60)
def step_impl(context, w, h): context.c = Canvas(w, h)