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 prepare_computations(intersection, ray, xs=None):
    c = comps()
    c.t = intersection.t
    c.object = intersection.object
    c.point = position(ray, c.t)
    c.eyev = -ray.direction
    c.normalv = normal_at(c.object, c.point)
    if dot(c.normalv, c.eyev) < 0:
        c.inside = True
        c.normalv = -c.normalv
    else:
        c.inside = False
    c.over_point = c.point + c.normalv * EPSILON
    c.under_point = c.point - c.normalv * EPSILON
    c.reflectv = reflect(ray.direction, c.normalv)
    containers = []
    if xs:
        for i in xs:
            if i == intersection:
                if len(containers) == 0:
                    c.n1 = 1.0
                else:
                    c.n1 = containers[-1].material.refractive_index
            if i.object in containers:
                containers.remove(i.object)
            else:
                containers.append(i.object)
            if i == intersection:
                if len(containers) == 0:
                    c.n2 = 1.0
                else:
                    c.n2 = containers[-1].material.refractive_index
                break
    return c
Exemple #3
0
 def local_intersect(self, triangle, ray):
     dir_cross_e2 = cross(ray.direction, triangle.e2)
     det = dot(triangle.e1, dir_cross_e2)
     if abs(det) < EPSILON:
         return []
     else:
         f = 1.0 / det
         p1_to_origin = ray.origin - triangle.p1
         u = f * dot(p1_to_origin, dir_cross_e2)
         if u < 0 or u > 1:
             return []
         origin_cross_e1 = cross(p1_to_origin, triangle.e1)
         v = f * dot(ray.direction, origin_cross_e1)
         if v < 0 or (u + v) > 1:
             return []
         t = f * dot(triangle.e2, origin_cross_e1)
         return [intersection(t, triangle)]
Exemple #4
0
def schlick(comps):
    cos = dot(comps.eyev, comps.normalv)
    if comps.n1 > comps.n2:
        n = comps.n1 / comps.n2
        sin2_t = (n ** 2) * (1.0 - cos ** 2)
        if sin2_t > 1.0:
            return 1.0
        cos_t = sqrt(1.0 - sin2_t)
        cos = cos_t

    r0 = ((comps.n1 - comps.n2) / (comps.n1 + comps.n2)) ** 2
    return r0 + (1 - r0) * (1 - cos) ** 5
Exemple #5
0
def refracted_color(world, comps, remaining):
    if comps.object.material.transparency == 0 or remaining <= 0:
        return color(0, 0, 0)
    else:
        n_ratio = comps.n1 / comps.n2
        cos_i = dot(comps.eyev, comps.normalv)
        sin2_t = n_ratio ** 2 * (1 - cos_i ** 2)
        if sin2_t > 1:
            return color(0, 0, 0)
        else:
            cos_t = sqrt(1.0 - sin2_t)
            direction = comps.normalv * (n_ratio * cos_i - cos_t) - comps.eyev * n_ratio
            refract_ray = ray(comps.under_point, direction)
            c = (
                color_at(world, refract_ray, remaining - 1)
                * comps.object.material.transparency
            )
            return c
Exemple #6
0
def step_impl(context):
    assert dot(context.a, context.b) == 20