Example #1
0
def main():
    aspect_ratio = 1
    height = 256
    width = int(height * aspect_ratio)  # round
    name = "test.png"
    wrapper = ImageWrapper(name, width, height)
    red = Lambertian(Vector3(1, 0, 0))
    green = Lambertian(Vector3(0.2, 1, 0.1))
    camera = Camera(Vector3(0, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1), 1,
                    90)
    spheres = [
        Sphere(Vector3(0, 10, 0), 5, red),
        Sphere(Vector3(0, 10, -100), 95, green)
    ]
    samples = 50
    for y in range(height):
        for x in range(width):
            final_color = Vector3(0, 0, 0)
            for sample in range(samples):
                ray = camera.generate_ray((x + random.random()) / width,
                                          (y + random.random()) / height)
                color = get_intersection(ray, spheres, MAX_DEPTH)
                final_color += color
            final_color /= samples
            wrapper.write_pixel(x, y, final_color)
        wrapper.save()