def color_at(world, ray, remaining=LIMIT): xs = intersect_world(world, ray) if len(xs) == 0: return color(0, 0, 0) else: for i in xs: if i.t > 0: comps = prepare_computations(i, ray, xs) return shade_hit(world, comps, remaining) return color(0, 0, 0)
def default_world(): w = world() w.light = point_light(point(-10, 10, -10), color(1, 1, 1)) s1 = sphere() s1.material.color = color(0.8, 1.0, 0.6) s1.material.diffuse = 0.7 s1.material.specular = 0.2 w.objects.append(s1) s2 = sphere() s2.transform = scaling(0.5, 0.5, 0.5) w.objects.append(s2) return w
def reflected_color(world, comps, remaining=LIMIT): if remaining <= 0: return color(0, 0, 0) else: if comps.object.material.reflective == 0: return color(0, 0, 0) else: reflect_ray = ray(comps.over_point, comps.reflectv) remaining -= 1 c = color_at(world, reflect_ray, remaining) return c * comps.object.material.reflective
def lighting(material, obj, light, point, eyev, normalv, in_shawdow=False): if material.pattern is not None: c = pattern_at_shape(material.pattern, obj, point) else: c = material.color effective_color = c * light.intensity lightv = normalize(light.position - point) ambient = effective_color * material.ambient light_dot_normal = dot(lightv, normalv) black = color(0, 0, 0) if light_dot_normal < 0: diffuse = black specular = black else: diffuse = effective_color * material.diffuse * light_dot_normal reflectv = reflect(-lightv, normalv) reflect_dot_eye = dot(reflectv, eyev) if reflect_dot_eye <= 0: specular = black else: factor = reflect_dot_eye ** material.shininess specular = light.intensity * material.specular * factor if in_shawdow: return ambient else: return ambient + diffuse + specular
def refracted_color(world, comps, remaining): if comps.object.material.transparency == 0 or remaining <= 0: return color(0, 0, 0) else: n_ratio = comps.n1 / comps.n2 cos_i = dot(comps.eyev, comps.normalv) sin2_t = n_ratio ** 2 * (1 - cos_i ** 2) if sin2_t > 1: return color(0, 0, 0) else: cos_t = sqrt(1.0 - sin2_t) direction = comps.normalv * (n_ratio * cos_i - cos_t) - comps.eyev * n_ratio refract_ray = ray(comps.under_point, direction) c = ( color_at(world, refract_ray, remaining - 1) * comps.object.material.transparency ) return c
def __init__(self): self.color = color(1, 1, 1) self.ambient = 0.1 self.diffuse = 0.9 self.specular = 0.9 self.shininess = 200.0 self.reflective = 0 self.transparency = 0 self.refractive_index = 1 self.pattern = None
def step_impl(context): context.c2 = color(0.9, 1, 0.1)
def step_impl(context): context.c3 = color(-0.5, 0, 1)
def step_impl(context): assert context.c1 * context.c2 == color(0.9, 0.2, 0.04)
def step_impl(context): context.c1 = color(1.5, 0, 0)
def step_impl(context): context.c2 = color(0, 0.5, 0)
def step_impl(context): context.black = color(0, 0, 0)
def step_impl(context): context.red = color(1, 0, 0)
from matrix import normalize from ray import ray from shape import hit, intersect from sphere import sphere from tuple import color, point if __name__ == "__main__": ray_origin = point(0, 0, -5) wall_z = 10 wall_size = 7 canvas_pixels = 400 pixel_size = wall_size / canvas_pixels half = wall_size / 2 canvas = canvas(canvas_pixels, canvas_pixels) color = color(1, 0, 0) shape = sphere() # transformations # shape.transform = scaling(1, 0.5, 1) # shape.transform = scaling(0.5, 1, 1) # shape.transform = rotation_z(pi / 4) * scaling(0.5, 1, 1) # shape.transform = shearing(1, 0, 0, 0, 0, 0) * scaling(0.5, 1, 1) start = time.time() print("Starting render...") for y in range(canvas_pixels): world_y = half - pixel_size * y
from sphere import intersect, sphere from tuple import color, normalize, point, point_light from world import lighting if __name__ == "__main__": ray_origin = point(0, 0, -5) wall_z = 10 wall_size = 7 canvas_pixels = 400 pixel_size = wall_size / canvas_pixels half = wall_size / 2 canvas = canvas(canvas_pixels, canvas_pixels) shape = sphere() shape.material = material() shape.material.color = color(0.2, 1, 1) light_position = point(-10, 10, -10) light_color = color(1, 1, 1) light = point_light(light_position, light_color) # transformations # shape.transform = scaling(1, 0.5, 1) # shape.transform = scaling(0.5, 1, 1) # shape.transform = rotation_z(pi / 4) * scaling(0.5, 1, 1) # shape.transform = shearing(1, 0, 0, 0, 0, 0) * scaling(0.5, 1, 1) start = time.time() print("Starting render...") for y in range(canvas_pixels):
def write_file(self, filename): f = open(filename, "w") f.write(str(self)) f.close() if __name__ == "__main__": start = point(0, 1, 0) velocity = normalize(vector(1, 1.8, 0)) * 11.25 p = projectile(start, velocity) gravity = vector(0, -0.1, 0) wind = vector(-0.01, 0, 0) e = environment(gravity, wind) red = color(1, 0, 0) c = canvas(900, 550) start = time.time() print("Starting render...") x = 0 while x < 300: write_pixel(c, p.position.x, c.height - p.position.y, red) p = tick(e, p) x += 1 end = time.time() print("Finished render.") print(str(round(end - start, 2)) + "s")
def step_impl(context): assert context.c == color(1, 1.5, 2)
import time from math import pi from camera import camera, render from canvas import canvas_to_ppm from hexagon import hexagon from matrix import view_transform from tuple import color, point, point_light, vector from world import world if __name__ == "__main__": start = time.time() print("Starting render...") w = world() w.light = point_light(point(-10, 10, -10), color(1, 1, 1)) hex = hexagon() w.objects.append(hex) cam = camera(1600, 800, pi / 2) cam.transform = view_transform(point(0, 1.5, -5), point(0, 1, 0), vector(0, 1, 0)) canvas = render(cam, w) end = time.time() print("Finished render.") print(str(round(end - start, 2)) + "s") start = time.time() print("Start writing file...")
def step_impl(context): context.white = color(1, 1, 1)
def step_impl(context): assert context.pattern.pattern_at(context.pattern, point(0.75, 0, 0)) == color( 0.25, 0.25, 0.25 )
def step_impl(context): context.c1 = color(1, 0.2, 0.4)
def step_impl(context): assert context.c * 2 == color(0.4, 0.6, 0.8)
def clamp(c): return color( int(max(min(c.red * 255, 255), 0) + 0.5), int(max(min(c.green * 255, 255), 0) + 0.5), int(max(min(c.blue * 255, 255), 0) + 0.5), )
def step_impl(context): assert context.c == color(0.75, 0.5, 0.25)
def step_impl(context): context.intensity = color(1, 1, 1)
def __init__(self, width, height): self.width = width self.height = height self.canvas = [[color(0, 0, 0) for x in range(height)] for y in range(width)]
from camera import camera, render from canvas import canvas_to_ppm from material import material from matrix import rotation_x, rotation_y, scaling, translation, view_transform from pattern import checkers_pattern, gradient_pattern, ring_pattern, stripe_pattern from plane import plane from sphere import sphere from tuple import color, point, point_light, vector from world import world if __name__ == "__main__": start = time.time() print("Starting render...") w = world() w.light = point_light(point(-10, 10, -10), color(1, 1, 1)) black = color(0, 0, 0) white = color(1, 1, 1) floor = plane() floor.transform = scaling(0.5, 0.5, 0.5) floor.material.color = color(0.9, 0.9, 0.9) floor.material.specular = 0.3 floor.material.pattern = checkers_pattern(black, white) floor.material.reflective = 0.6 w.objects.append(floor) left_wall = plane() left_wall.transform = (translation(0, 0, 5) * rotation_y(-pi / 4) * rotation_x(pi / 2) * scaling(10, 0.01, 10))
def step_impl(context): for i in range(0, context.c.width): for j in range(0, context.c.height): context.c.canvas[i][j] = color(1, 0.8, 0.6)
from camera import camera, render from canvas import canvas_to_ppm from material import material from matrix import scaling, translation, view_transform from plane import plane from sphere import sphere from tuple import color, point, point_light, vector from world import world if __name__ == "__main__": start = time.time() print("Starting render...") w = world() w.light = point_light(point(-10, 10, -10), color(1, 1, 1)) floor = plane() floor.transform = scaling(10, 0.01, 10) floor.material.color = color(1, 0.9, 0.9) floor.material.specular = 0 w.objects.append(floor) # left_wall = sphere() # left_wall.transform = translation(0, 0, 5) * rotation_y(-pi/4) * rotation_x(pi/2) * scaling(10, 0.01, 10) # left_wall.material = floor.material # w.objects.append(left_wall) # right_wall = sphere() # right_wall.transform = translation(0, 0, 5) * rotation_y(pi/4) * rotation_x(pi/2) * scaling(10, 0.01, 10) # right_wall.material = floor.material
def step_impl(context): for i in range(0, context.c.width): for j in range(0, context.c.height): assert context.c.canvas[i][j] == color(0, 0, 0)