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)
Exemple #2
0
def main():
    shape = ClippedShape(crooked_plane())
    #shape.transform = ReflectionXZ()
    shape.material = Material()
    shape.material.color = Color(1, .2, 1)
    objects = (shape, )
    draw_objects(objects)
Exemple #3
0
def make_middle():
    middle = set_transform(Sphere(), Translation(-0.5, 1, 0.5))
    middle.material = Material()
    middle.material.color = Color(0.1, 1, 0.5)
    middle.material.diffuse = 0.7
    middle.material.specular = 0.3
    return middle
Exemple #4
0
def floor_material():
    material = Material()
    material.color = Color(1, .9, .9)
    material.specular = 0
    material.pattern = stripe_pattern(RED, BLUE)
    material.pattern.transform = Scaling(.2, .2, .2) * rotation_z(math.pi / 4)
    return material
Exemple #5
0
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)
Exemple #6
0
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)
Exemple #7
0
def make_right():
    right = set_transform(Sphere(),
                          Translation(1.5, 0.5, -0.5) * Scaling(0.5, 0.5, 0.5))
    right.material = Material()
    right.material.color = Color(0.5, 1, 0.1)
    right.material.diffuse = 0.7
    right.material.specular = 0.3
    return right
Exemple #8
0
def make_middle():
    middle = set_transform(Sphere(), Translation(-0.5, 1, 0.5))
    middle.material = Material()
    middle.material.color = Color(0.1, 1, 0.5)
    middle.material.diffuse = 0.7
    middle.material.specular = 0.3
    middle.material.pattern = gradient_pattern(BLUE, GREEN)
    middle.material.pattern.transform = Scaling(.2, .2, .2)
    return middle
Exemple #9
0
def make_left():
    left = set_transform(
        Sphere(),
        Translation(-1.5, 0.33, -0.75) * Scaling(0.33, 0.33, 0.33))
    left.material = Material()
    left.material.color = Color(1, 0.8, 0.1)
    left.material.diffuse = 0.7
    left.material.specular = 0.3
    return left
Exemple #10
0
class Material:
    color: Color = Color(1, 1, 1)
    ambient: float = .1
    diffuse: float = .9
    pattern: Pattern = None
    specular: float = .9
    shininess: float = 200.0
    reflective: float = 0.0
    transparency: float = 0.0
    refractive_index: float = 1
Exemple #11
0
def make_left():
    left = set_transform(
        Sphere(),
        Translation(-1.5, 0.33, -0.75) * Scaling(0.33, 0.33, 0.33))
    left.material = Material()
    left.material.color = Color(1, 0.8, 0.1)
    left.material.diffuse = 0.7
    left.material.specular = 0.3
    left.material.pattern = checkers_pattern(WHITE, BLACK)
    return left
Exemple #12
0
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 set_props_from_table(context, shape):
    for row in itertools.chain([context.table.headings], context.table):
        k, v = row
        if k.startswith('material'):
            _, materialprop = k.split('.')
            if materialprop == "color":
                rgb = [float(p) for p in re.findall('\d+\.?\d*', v)]
                value = Color(*rgb)
            elif materialprop == "pattern":
                if v != "test_pattern()":
                    raise NotImplementedError("Only test_pattern is implemented")
                value = test_pattern()
            else:
                value = float(v)
            setattr(shape.material, materialprop, value)
        elif k == "transform":
            t = eval(v)
            shape.transform = t
        else:
            raise NotImplementedError(f"{k} is not implemented")
Exemple #14
0
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)
Exemple #15
0
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 step_impl(context, name, red, green, blue):
    setattr(context, name, Color(red, green, blue))
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)')
Exemple #19
0
from ray_tracer.canvas import Canvas, write_pixel, canvas_to_png
from ray_tracer.colors import Color, WHITE
from ray_tracer.intersections import intersect, hit
from ray_tracer.lights import point_light
from ray_tracer.material import Material, lighting
from ray_tracer.rays import Ray, position
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
light = point_light(Point(-10, 10, -10), WHITE)
shape = Sphere()
shape.material = Material()
shape.material.color = Color(1, .2, 1)


def get_color(a_hit, ray):
    point = position(ray, a_hit.t)
    normal = a_hit.object.normal_at(point)
    eye = -ray.direction
    color = lighting(a_hit.object.material, a_hit.object, light, point, eye,
                     normal)
    return color


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)
def step_impl(context, pattern, r1, g1, b1, r2, g2, b2):
    _pattern = stripe_pattern(Color(r1, g1, b1), Color(r2, g2, b2))
    setattr(context, pattern, _pattern)
def step_impl(context, material, r1, g1, b1, r2, g2, b2):
    _material = getattr(context, material)
    _material.pattern = stripe_pattern(Color(r1, g1, b1), Color(r2, g2, b2))
    setattr(context, material, _material)
Exemple #22
0
 def pattern_at(self, point):
     return Color(point.x, point.y, point.z)
Exemple #23
0
 def pixel_at(self, x, y):
     return Color(*(self._image[y][x])/255)
Exemple #24
0
def floor_material():
    material = Material()
    material.color = Color(1, .9, .9)
    material.specular = 0
    return material
Exemple #25
0
def step_impl(context, canvas):
    c = getattr(context, canvas)
    for i in range(0, c.width):
        for j in range(0, c.height):
            assert pixel_at(c, i, j) == Color(0, 0, 0)