Esempio n. 1
0
def view_transform(from_, to, up):
    forward = normalize(to - from_)
    left = cross(forward, normalize(up))
    true_up = cross(left, forward)
    orientation = Transformation(
        np.stack(
            (left.ndarray, true_up.ndarray, -forward.ndarray, [0, 0, 0, 1])))
    translation = Translation(-from_.x, -from_.y, -from_.z)
    return orientation * translation
Esempio n. 2
0
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)
    a_hit = hit(xs)
    if a_hit is not None:
        color = get_color(a_hit, ray)
        write_pixel(canvas, canvas_x, canvas_y, color)
Esempio n. 3
0
    def normal_at(self, world_point):
        inv_ = self.transform.inverse()
        object_point = inv_ * world_point

        object_normal = self.inner.normal_at(object_point)
        linear_component = self.transform.linear_component()
        world_normal = linear_component.inverse().transpose() * object_normal
        return normalize(world_normal)
Esempio n. 4
0
    def ray_for_pixel(self, x, y):
        xoffset = (x + .5) * self.pixel_size
        yoffset = (y + .5) * self.pixel_size

        world_x = self.half_width - xoffset
        world_y = self.half_height - yoffset

        pixel = self.transform.inverse() * Point(world_x, world_y, -1)
        origin = self.transform.inverse() * Point(0, 0, 0)

        direction = normalize(pixel - origin)
        return Ray(origin, direction)
Esempio n. 5
0
def lighting(material, object_, light, point, eyev, normalv, in_shadow=False):

    if material.pattern is not None:
        color = pattern_at_shape(material.pattern, object_, point)
    else:
        color = material.color

    # combine the surface color with the light's color/intensity
    effective_color = color * light.intensity

    # find the direction to the light source
    lightv = normalize(light.position - point)

    # compute the ambient contribution
    ambient = effective_color * material.ambient

    # light_dot_normal representst the cosine of the angle between the
    # light vector and the normal vector.  A negative number means teh
    # light is on the other side of the surface
    light_dot_normal = dot(lightv, normalv)
    if light_dot_normal < 0:
        diffuse = BLACK
        specular = BLACK
    else:
        # compute the diffuse contribution
        diffuse = effective_color * material.diffuse * light_dot_normal

        # reflect_dot_eye represents the cosine of the angle between the
        # refletion vector and the eye vector.  A negative number means the
        # light relfects away from the eye
        reflectv = reflect(-lightv, normalv)
        reflect_dot_eye = dot(reflectv, eyev)

        if reflect_dot_eye <= 0:
            specular = BLACK
        else:
            # compute the specular contribution
            factor = pow(reflect_dot_eye, material.shininess)
            specular = light.intensity * material.specular * factor

    if in_shadow:
        return ambient

    # Add the three contributions together to get the final shading
    return ambient + diffuse + specular
Esempio n. 6
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)
Esempio n. 7
0
wall_z = 10
wall_size = 7
canvas_pixels = 100
pixel_size = wall_size / canvas_pixels
half = wall_size / 2

canvas = Canvas(canvas_pixels, canvas_pixels)
shape = Sphere()

for y in range(canvas_pixels - 1):
    world_y = half - pixel_size * y
    for x in range(canvas_pixels - 1):
        world_x = -half + pixel_size * x

        position = Point(world_x, world_y, wall_z)
        r = Ray(ray_origin, normalize(position - ray_origin))
        xs = intersect(shape, r)
        if hit(xs) is not None:
            write_pixel(canvas, x, y, RED)

path = Path(__file__).parent / "chap5.png"
canvas_to_png(str(path), canvas)

# Copyright 2020 Bloomberg Finance L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
Esempio n. 8
0
def step_impl(context, name, v):
    _v = getattr(context, v)
    setattr(context, name, normalize(_v))