Exemple #1
0
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
Exemple #2
0
 def __init__(self, p1, p2, p3):
     super().__init__()
     self.p1 = p1
     self.p2 = p2
     self.p3 = p3
     self.e1 = p2 - p1
     self.e2 = p3 - p1
     self.normal = normalize(cross(self.e2, self.e1))
Exemple #3
0
def normal_to_world(shape, normal):
    normal = transpose(inverse(shape.transform)) * normal
    normal.w = 0
    normal = normalize(normal)

    if shape.parent is not None:
        normal = normal_to_world(shape.parent, normal)

    return normal
Exemple #4
0
def ray_for_pixel(camera, px, py):
    xoffset = (px + 0.5) * camera.pixel_size
    yoffset = (py + 0.5) * camera.pixel_size

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

    pixel = inverse(camera.transform) * point(world_x, world_y, -1)
    origin = inverse(camera.transform) * point(0, 0, 0)
    direction = normalize(pixel - origin)

    return ray(origin, direction)
Exemple #5
0
def is_shadowed(world, point):
    v = world.light.position - point
    distance = magnitude(v)
    direction = normalize(v)

    r = ray(point, direction)
    intersections = intersect_world(world, r)

    h = hit(intersections)
    if h is not None and h.t < distance:
        return True
    else:
        return False
Exemple #6
0
def view_transform(frm, to, up):
    forward = normalize(to - frm)
    upn = normalize(up)
    left = cross(forward, upn)
    true_up = cross(left, forward)
    orientation = matrix(
        left.x,
        left.y,
        left.z,
        0,
        true_up.x,
        true_up.y,
        true_up.z,
        0,
        -forward.x,
        -forward.y,
        -forward.z,
        0,
        0,
        0,
        0,
        1,
    )
    return orientation * translation(-frm.x, -frm.y, -frm.z)
Exemple #7
0
    # 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

        for x in range(canvas_pixels):

            world_x = -half + pixel_size * x
            pos = point(world_x, world_y, wall_z)

            r = ray(ray_origin, normalize(pos - ray_origin))
            xs = intersect(shape, r)

            if hit(xs) is not None:
                pnt = position(r, xs[0].t)
                normal = normal_at(xs[0].object, pnt)
                eye = -r.direction
                color = lighting(xs[0].object.material, xs[0].object, light,
                                 pnt, eye, normal)
                write_pixel(canvas, x, y, color)

    end = time.time()
    print("Finished render.")
    print(str(round(end - start, 2)) + "s")

    start = time.time()
def step_impl(context):
    context.direction = normalize(vector(0.1, 1, 1))
def step_impl(context):
    context.direction = normalize(vector(0, -1, 2))
Exemple #10
0
        self.header = "P3\n" + str(c.width) + " " + str(c.height) + "\n255"
        self.canvas = c
        self.body = str(c)

    def __str__(self):
        return self.header + "\n" + self.body + "\n"

    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:
Exemple #11
0
def step_impl(context):
    context.norm = normalize(context.v)
Exemple #12
0
def step_impl(context):
    assert normalize(context.v) == vector(0.26726, 0.53452, 0.80178)
Exemple #13
0
def step_impl(context):
    assert normalize(context.v) == vector(1, 0, 0)