示例#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)
示例#2
0
    def test_computing_normal_on_scaled_sphere(self):
        s = Sphere()
        s.set_transform(scaling(1, 0.5, 1))

        n = s.normal_at(point(0, math.sqrt(2) / 2, -math.sqrt(2) / 2))

        self.assert_tuple_equals(vector(0, 0.97014, -0.24254), n, 0.001)
    def test_view_transformation_matrix_looking_in_positive_z_direction(self):
        from_where = point(0, 0, 0)
        to = point(0, 0, 1)
        up = vector(0, 1, 0)

        t = view_transform(from_where, to, up)

        self.assertEqual(scaling(-1, 1, -1), t)
    def test_chain_transformation_must_be_applied_in_reverse_order(self):
        p = point(1, 0, 1)
        a = rotation_x(pi / 2)
        b = scaling(5, 5, 5)
        c = translation(10, 5, 7)

        t = c * b * a

        self.assertEqual(point(15, 0, 7), t * p)
示例#5
0
    def test_intersecting_scaled_sphere_with_ray(self):
        r = Ray(point(0, 0, -5), vector(0, 0, 1))
        s = Sphere()

        s.set_transform(scaling(2, 2, 2))
        xs = s.intersect(r)

        self.assertEqual(2, len(xs))
        self.assertEqual(3, xs[0].t)
        self.assertEqual(7, xs[1].t)
    def test_individual_transformation_are_applied_in_sequence(self):
        p = point(1, 0, 1)
        a = rotation_x(pi / 2)
        b = scaling(5, 5, 5)
        c = translation(10, 5, 7)

        p2 = a * p
        p3 = b * p2
        p4 = c * p3

        self.assertEqual(point(15, 0, 7), p4)
示例#7
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])
示例#8
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) /
 def test_reflection_scaling_by_a_negative_value(self):
     self.assertEqual(vector(-2, 3, 4), scaling(-1, 1, 1) * vector(2, 3, 4))
示例#10
0
 def test_multiplying_by_inverse_of_scaling_matrix(self):
     self.assertEqual(vector(-2, 2, 2),
                      scaling(2, 3, 4).inverse() * vector(-4, 6, 8))
示例#11
0
 def test_scaling_matrix_applied_to_vector(self):
     self.assertEqual(vector(-8, 18, 32),
                      scaling(2, 3, 4) * vector(-4, 6, 8))
示例#12
0
 def test_scaling_matrix_applied_to_point(self):
     self.assertEqual(point(-8, 18, 32), scaling(2, 3, 4) * point(-4, 6, 8))
示例#13
0
 def test_scaling_ray(self):
     r = Ray(point(1, 2, 3), vector(0, 1, 0))
     m = scaling(2, 3, 4)
     r2 = r.transform(m)
     self.assertEqual(point(2, 6, 12), r2.origin)
     self.assertEqual(vector(0, 3, 0), r2.direction)
示例#14
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