def main(): nx = 200 ny = 100 ns = 30 f = open("generated_images/first_world.ppm","w") f.write("P3\n%d %d\n255\n"%(nx,ny)) cam = Camera() world = Object3DList( [Sphere(Vec3(0.0,0.0,-1.0), 0.5), Sphere(Vec3(0.0,-100.5,-1.0),100)]) # Note break with guide convention, vertical pixels start with index 0 at top for y in range(0,ny): for x in range(0,nx): col = Vec3(0.0,0.0,0.0) for _ in range(0, ns): u = (float(x)+random.random())/float(nx) v = (float(y)+random.random())/float(ny) r = cam.get_ray(u, v) col += color(r, world) col /= ns f.write(col.color_string(scale=255.99)) f.close()
def main(): # nx = 100 # ny = 100 # ns = 10 nx = 1000 ny = 1000 ns = 100 f = open("generated_images/balls_world_fuzz_glass.ppm", "w") f.write("P3\n%d %d\n255\n" % (nx, ny)) cam = Camera(look_from=Vec3(0.5,0.5,0.0), look_at=Vec3(0.0,0.5,-1.0), vec_up=Vec3(0.0, 1.0, 0.0), v_fov=90.0, aspect=nx/ny, aperture=0.0, focus_dist=1.0) world = Object3DList( [Sphere(Vec3(0.0, -1000.0, -1.0), 1000.0, Lambertian(albedo=Vec3(0.8, 0.8, 0.8))), Sphere(Vec3(0.0, 0.5, -1.0), 0.5, Lambertian(albedo=Vec3(0.8, 0.3, 0.3))), Sphere(Vec3(0.0, 0.1, -0.5), 0.1, Lambertian(albedo=Vec3(0.5, 0.2, 0.5))), Sphere(Vec3(-0.2, 0.5, -0.3), 0.1, Dielectric(ref_idx=1.5)), Sphere(Vec3(0.75, 0.25, -0.5), 0.25, Metal(albedo=Vec3(0.6, 0.8, 0.8), fuzz=0.02)), Sphere(Vec3(-0.75, 0.25, -0.75), 0.25, Metal(albedo=Vec3(0.8, 0.6, 0.2), fuzz=0.0)) ]) # Note break with guide convention, vertical pixels start with index 0 at top for y in range(0, ny): for x in range(0, nx): col = Vec3(0.0, 0.0, 0.0) for _ in range(0, ns): u = (float(x) + random.random()) / float(nx) v = (float(y) + random.random()) / float(ny) r = cam.get_ray(u, v) col += color(r, world, 0) print(y*100000 + x*ns + _) col /= ns col = col ** 0.5 f.write(col.color_string(scale=255.99)) f.close()
def color(ray: Ray, world: Object3DList): h_rec = HitRecord() if world.hit(ray, 0.0, float("inf"), h_rec): return Vec3(h_rec.normal.x+1.0, h_rec.normal.y+1.0, h_rec.normal.z+1.0)*0.5 unit_direction = unit_vector(ray.direction) t = 0.5*(unit_direction.y+1.0) return (1-t)*Vec3(1.0,1.0,1.0) + t*Vec3(0.5,0.7,1.0)
def color(ray: Ray, world: Object3DList, depth: int): h_rec = HitRecord() if world.hit(ray, 0.001, float("inf"), h_rec): retval, s_ray, attenuation = h_rec.material.scatter(ray, h_rec) if depth < 50 and retval: return attenuation * color(s_ray, world, depth + 1) else: return Vec3(0.0, 0.0, 0.0) else: unit_direction = unit_vector(ray.direction) t = 0.5 * (unit_direction.y + 1.0) return (1 - t) * Vec3(1.0, 1.0, 1.0) + t * Vec3(0.5, 0.7, 1.0)
def main(): # nx = 1000 # ny = 500 # ns = 30 nx = 200 ny = 100 ns = 10 f = open("generated_images/balls_world_fuzz.ppm", "w") f.write("P3\n%d %d\n255\n" % (nx, ny)) cam = Camera(upper_left_corner=Vec3(-2.0, 1.5, -1.0), horizontal=Vec3(4.0, 0.0, 0.0), vertical=Vec3(0.0, -2.0, 0.0), origin=Vec3(0.0, 0.5, 0.0)) world = Object3DList([ Sphere(Vec3(0.0, -1000.0, -1.0), 1000.0, Lambertian(albedo=Vec3(0.8, 0.8, 0.8))), Sphere(Vec3(0.0, 0.5, -1.0), 0.5, Lambertian(albedo=Vec3(0.8, 0.3, 0.3))), Sphere(Vec3(0.0, 0.1, -0.5), 0.1, Lambertian(albedo=Vec3(0.5, 0.2, 0.5))), Sphere(Vec3(0.75, 0.25, -0.5), 0.25, Metal(albedo=Vec3(0.6, 0.8, 0.8), fuzz=0.5)), Sphere(Vec3(-0.75, 0.25, -0.75), 0.25, Metal(albedo=Vec3(0.8, 0.6, 0.2), fuzz=0.0)) ]) # Note break with guide convention, vertical pixels start with index 0 at top for y in range(0, ny): for x in range(0, nx): col = Vec3(0.0, 0.0, 0.0) for _ in range(0, ns): u = (float(x) + random.random()) / float(nx) v = (float(y) + random.random()) / float(ny) r = cam.get_ray(u, v) col += color(r, world, 0) col /= ns col = col**0.5 f.write(col.color_string(scale=255.99)) f.close()
def populate_world() -> Object3DList: obj_list = [Sphere(Vec3(0.0, -1000.0, -0.0), 1000.0, Lambertian(albedo=Vec3(0.8, 0.8, 0.8))) ] sphere_radius = 0.5 for idx in range(0, 3): for idz in range(0, 3): for idy in range(0, 3): pos = Vec3( (idx - 1) * 4 * sphere_radius, (2 * idy + 1) * sphere_radius, (idz - 1) * 4 * sphere_radius) n = random.random() if n < 0.33: alb = 0.8 * Vec3(random.random(), random.random(), random.random()) obj_list.append( Sphere(pos, sphere_radius, Lambertian(albedo=alb))) elif n >= 0.33 and n < 0.66: alb = 0.8 * Vec3(random.random(), random.random(), random.random()) fuzz = 0.2 * random.random() obj_list.append( Sphere(pos, sphere_radius, Metal(albedo=alb, fuzz=fuzz))) else: ref_idx = random.random() * 0.5 + 1 obj_list.append( Sphere(pos, sphere_radius, Dielectric(ref_idx=ref_idx))) return Object3DList(obj_list)