コード例 #1
0
ファイル: world.py プロジェクト: rhoban13/ray_tracer
 def reflected_color(self, comps, remaining=5):
     if remaining <= 0:
         return BLACK
     if comps.object.material.reflective == 0:
         return BLACK
     reflect_ray = Ray(comps.over_point, comps.reflectv)
     color = self.color_at(reflect_ray, remaining - 1)
     return color * comps.object.material.reflective
コード例 #2
0
def test_ray_misses_to_wing2():
    # Given
    cp = DefaultCrookedPlane()
    ray = Ray(Point(1, 0, 0), Vector(0, 1, -1))

    # When
    xs = cp.intersect_wing2(ray)
    assert xs is None
コード例 #3
0
ファイル: chap6.py プロジェクト: rhoban13/ray_tracer
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)
コード例 #4
0
def test_ray_intersects_wing2():
    # Given
    cp = DefaultCrookedPlane()
    ray = Ray(Point(-1, 0, -1), Vector(0, 0, 1))

    # When
    xs = cp.intersect_wing2(ray)

    # Then
    assert xs.t == 1
コード例 #5
0
def test_ray_parallel_to_wing2():
    # Given
    cp = DefaultCrookedPlane()
    ray = Ray(Point(-1, 1, 0), Vector(0, 1, 1))

    # When
    xs = cp.intersect_wing2(ray)

    # Then
    assert xs is None
コード例 #6
0
def test_ray_misses_stem():
    # Given
    cp = DefaultCrookedPlane()
    ray = Ray(Point(0, 1, 0), Vector(1, 0, 1))

    # When
    xs = cp.intersect_stem(ray)

    # Then
    assert xs is None
コード例 #7
0
def test_ray_intersects_stem():
    # Given
    cp = DefaultCrookedPlane()
    ray = Ray(Point(0, 0, -1), Vector(1, 0, 0))

    # When
    xs = cp.intersect_stem(ray)

    # Then
    assert xs.t == 0
コード例 #8
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)
コード例 #9
0
ファイル: world.py プロジェクト: rhoban13/ray_tracer
def is_shadowed(world, point, light=None):
    if light is None:  # This is pretty inefficient.  It recomputes intersections below for each light
        light = world.light
    v = light.position - point
    distance = v.magnitude()
    direction = v.normalize()

    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
コード例 #10
0
ファイル: world.py プロジェクト: rhoban13/ray_tracer
    def refracted_color(self, comps, remaining):
        if comps.object.material.transparency == 0:
            return BLACK

        if remaining == 0:
            return BLACK

        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 BLACK

        cos_t = math.sqrt(1 - sin2_t)
        direction = comps.normalv * (n_ratio * cos_i -
                                     cos_t) - comps.eyev * n_ratio
        refract_ray = Ray(comps.under_point, direction)
        color = self.color_at(
            refract_ray, remaining - 1) * comps.object.material.transparency
        return color
コード例 #11
0
ファイル: chap5.py プロジェクト: rhoban13/ray_tracer
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
#
コード例 #12
0
def test_ray_parallel_to_stem():
    ray = Ray(Point(-1, 0, 1), Vector(0, 1, 1))
    assert [] == intersect_stem(ray)
コード例 #13
0
def test_ray_misses_to_wing2():
    ray = Ray(Point(1, 0, 0), Vector(0, 1, -1))
    assert [] == intersect_wing2(ray)
コード例 #14
0
def test_ray_intersects_wing2():
    ray = Ray(Point(-1, 0, -1), Vector(0, 0, 1))
    assert [1] == intersect_wing2(ray)
コード例 #15
0
def test_ray_parallel_to_wing2():
    ray = Ray(Point(-1, 1, 0), Vector(0, 1, 1))
    assert [] == intersect_wing2(ray)
コード例 #16
0
def test_ray_intersects_stem():
    ray = Ray(Point(0, 0, -1), Vector(1, 0, 0))
    assert [0] == intersect_stem(ray)
コード例 #17
0
def test_ray_parallel_to_wing1():
    ray = Ray(Point(1, 1, 0), Vector(0, 1, -1))
    assert [] == intersect(ray)
コード例 #18
0
def test_ray_misses_to_wing1():
    ray = Ray(Point(-1, 0, 0), Vector(0, 1, 1))
    assert [] == intersect(ray)
コード例 #19
0
def step_impl(context, r, x, y, z, dx, dy, dz):
    _r = Ray(Point(x, y, z), Vector(dx, dy, dz))
    setattr(context, r, _r)
コード例 #20
0
def step_impl(context, r, origin, direction):
    _origin = getattr(context, origin)
    _direction = getattr(context, direction)
    _r = Ray(_origin, _direction)
    setattr(context, r, _r)
コード例 #21
0
def test_ray_misses_stem():
    ray = Ray(Point(0, 1, 0), Vector(1, 0, 1))
    assert [] == intersect_stem(ray)