Ejemplo n.º 1
0
    def test_default_world(self):
        light = PointLight(point(-10, 10, -10), color(1, 1, 1))

        s1 = Sphere()
        s2 = Sphere()

        s1.material.color = color(0.8, 1.0, 0.6)
        s1.material.diffuse = 0.7
        s1.material.specular = 0.2

        s2.set_transform(scaling(0.5, 0.5, 0.5))

        world = default_world()

        self.assertEqual(light.position, world.light.position)
        self.assertEqual(light.intensity, world.light.intensity)

        self.assertEqual(s1.material.color, world.objects[0].material.color)
        self.assertEqual(s1.material.diffuse,
                         world.objects[0].material.diffuse)
        self.assertEqual(s1.material.specular,
                         world.objects[0].material.specular)

        self.assertEqual(s2.material.color, world.objects[1].material.color)
        self.assertEqual(s2.material.diffuse,
                         world.objects[1].material.diffuse)
        self.assertEqual(s2.material.specular,
                         world.objects[1].material.specular)
Ejemplo n.º 2
0
    def test_shading_an_intersection_form_the_inside(self):
        world = default_world()
        world.light = PointLight(point(0, 0.25, 0), color(1, 1, 1))
        ray = Ray(point(0, 0, 0), vector(0, 0, 1))
        shape = world.objects[1]
        xs = Intersection(0.5, shape)

        xs.prepare_hit(ray)
        c = shade_hit(world, xs)

        self.assert_tuple_equals(color(0.90498, 0.90498, 0.90498), c, 0.001)
Ejemplo n.º 3
0
def default_world():
    light = PointLight(point(-10, 10, -10), color(1, 1, 1))

    s1 = Sphere()
    s2 = Sphere()

    s1.material.color = color(0.8, 1.0, 0.6)
    s1.material.diffuse = 0.7
    s1.material.specular = 0.2

    s2.set_transform(scaling(0.5, 0.5, 0.5))

    return World(light=light, objects=[s1, s2])
Ejemplo n.º 4
0
    def test_the_color_when_ray_misses(self):
        world = default_world()
        ray = Ray(point(0, 0, -5), vector(0, 1, 0))

        c = color_at(world, ray)

        self.assertEqual(color(0, 0, 0), c)
Ejemplo n.º 5
0
 def test_is_canvas(self):
     c = Canvas(10, 20)
     self.assertEqual(10, c.width)
     self.assertEqual(20, c.height)
     for y in range(c.height):
         for x in range(c.width):
             self.assertEqual(color(0, 0, 0), c.pixel_at(x, y))
Ejemplo n.º 6
0
    def test_ppm_pixel_data(self):
        c = Canvas(5, 3)
        c1 = color(1.5, 0, 0)
        c2 = color(0, 0.5, 0)
        c3 = color(-0.5, 0, 1)
        c.write_pixel(0, 0, c1)
        c.write_pixel(2, 1, c2)
        c.write_pixel(4, 2, c3)
        ppm = c.to_ppm()
        file = [
            '255 0 0 0 0 0 0 0 0 0 0 0 0 0 0',
            '0 0 0 0 0 0 0 128 0 0 0 0 0 0 0',
            '0 0 0 0 0 0 0 0 0 0 0 0 0 0 255'
        ]

        self.assertEqual(file, ppm.split('\n')[3:6])
Ejemplo n.º 7
0
    def test_the_color_when_ray_hits(self):
        world = default_world()
        ray = Ray(point(0, 0, -5), vector(0, 0, 1))

        c = color_at(world, ray)

        self.assert_tuple_equals(color(0.38066, 0.47583, 0.2855), c, 0.001)
Ejemplo n.º 8
0
    def test_point_light_has_position_and_intensity(self):
        intensity = color(1, 1, 1)
        position = point(0, 0, 0)

        light = PointLight(position, intensity)

        self.assertEqual(position, light.position)
        self.assertEqual(intensity, light.intensity)
Ejemplo n.º 9
0
    def test_shading_and_intersection(self):
        world = default_world()
        ray = Ray(point(0, 0, -5), vector(0, 0, 1))
        shape = world.objects[0]
        xs = Intersection(4, shape)

        xs.prepare_hit(ray)
        c = shade_hit(world, xs)

        self.assert_tuple_equals(color(0.38066, 0.47583, 0.2855), c, 0.001)
Ejemplo n.º 10
0
 def __init__(self,
              color=tuples.color(1, 1, 1),
              ambient=0.1,
              diffuse=0.9,
              specular=0.9,
              shininess=200):
     self.color = color
     self.ambient = ambient
     self.diffuse = diffuse
     self.specular = specular
     self.shininess = shininess
Ejemplo n.º 11
0
    def test_rendering_world_with_camera(self):
        w = default_world()
        c = Camera(11, 11, pi / 2.0)
        from_where = point(0, 0, -5)
        to = point(0, 0, 0)
        up = vector(0, 1, 0)
        c.transform = view_transform(from_where, to, up)

        image = render(c, w)

        self.assert_tuple_equals(color(0.38066, 0.47583, 0.2855),
                                 image.pixel_at(5, 5), 0.001)
Ejemplo n.º 12
0
    def __init__(self, width: int, height: int):
        self.width = width
        self.height = height

        self.canvas = [[color(0, 0, 0) for x in range(width)]
                       for y in range(height)]
Ejemplo n.º 13
0
import time
from math import pi
from renderer.camera import Camera, render
from renderer.lights import PointLight
from renderer.materials import Material
from renderer.sphere import Sphere
from renderer.transformations import scaling, translation, rotation_y, rotation_x, view_transform
from renderer.tuples import point, color, vector
from renderer.world import World

floor = Sphere()
floor.transform = scaling(10, 0.01, 10)
floor.material.color = color(1, 0.9, 0.9)
floor.material.specular = 0

left_wall = Sphere()
left_wall.transform = translation(0, 0, 5) * rotation_y(-pi / 4) * rotation_x(
    pi / 2) * scaling(10, 0.01, 10)
left_wall.material.color = color(1, 0.9, 0.9)
left_wall.material.specular = 0

right_wall = Sphere()
right_wall.transform = translation(0, 0, 5) * rotation_y(pi / 4) * rotation_x(
    pi / 2) * scaling(10, 0.01, 10)
right_wall.material.color = color(1, 0.9, 0.9)
right_wall.material.specular = 0

middle = Sphere()
middle.transform = translation(-0.5, 1, 0.5)
middle.material.color = color(0.1, 1, 0.5)
middle.material.ambient = 0.1
Ejemplo n.º 14
0
 def test_colors_as_tuples(self):
     c = color(-0.5, 0.4, 1.7)
     self.assertEqual(-0.5, c.red())
     self.assertEqual(0.4, c.green())
     self.assertEqual(1.7, c.blue())
Ejemplo n.º 15
0
 def test_write_pixels(self):
     c = Canvas(10, 20)
     red = color(1, 0, 0)
     c.write_pixel(2, 3, red)
     self.assertEqual(red, c.pixel_at(2, 3))
     self.assertEqual(color(0, 0, 0), c.pixel_at(0, 0))
Ejemplo n.º 16
0
import time

from renderer.canvas import Canvas
from renderer.intersections import hit
from renderer.lights import PointLight
from renderer.rays import Ray
from renderer.sphere import Sphere
from renderer.transformations import scaling
from renderer.tuples import point, color

s = Sphere()
s.set_transform(scaling(0.5, 1, 1))
s.material.color = color(1, 0.2, 1)
light_position = point(-10, 10, -10)
light_color = color(1, 1, 1)
light = PointLight(light_position, light_color)
ray_origin = point(0, 0, -5)
wall_z = 10
wall_size = 7.0
canvas_pixels = 1000
pixel_size = wall_size / canvas_pixels
half = wall_size / 2

c = Canvas(canvas_pixels, canvas_pixels)
col = color(1, 0, 0)

start_time = time.time()
for y in range(canvas_pixels):
    elapsed_time = time.time() - start_time
    if elapsed_time > 0 and y > 0:
        print("time_to_finish: " + str(1.0 * (canvas_pixels - y) /
Ejemplo n.º 17
0
from renderer.canvas import Canvas
from renderer.intersections import hit
from renderer.rays import Ray
from renderer.sphere import Sphere
from renderer.tuples import point, color

s = Sphere()
ray_origin = point(0, 0, -5)
wall_z = 10
wall_size = 7.0
canvas_pixels = 1000
pixel_size = wall_size / canvas_pixels
half = wall_size / 2

c = Canvas(canvas_pixels, canvas_pixels)
col = color(1, 1, 0)

start_time = time.time()
for y in range(canvas_pixels):
    elapsed_time = time.time() - start_time
    if elapsed_time > 0 and y > 0:
        print("time_to_finish: " + str(1.0 * (canvas_pixels - y) /
                                       (1.0 * y / elapsed_time)))
    world_y = half - pixel_size * y
    for x in range(canvas_pixels):
        world_x = -half + pixel_size * x
        position = point(world_x, world_y, wall_z)
        ray = Ray(ray_origin, (position - ray_origin))
        xs = s.intersect(ray)
        if hit(xs):
            c.write_pixel(x, y, col)