def test_light_eye_reflection(self):
     m = Material()
     position = Point(0, 0, 0)
     eyev = Vector(0, -math.sqrt(2) / 2, -math.sqrt(2) / 2)
     normalv = Vector(0, 0, -1)
     light = PointLight(Point(0, 10, -10), Color(1, 1, 1))
     result = PointLight.lighting(m, Sphere(), light, position, eyev, normalv, False)
     self.assertEqual(result, Color(1.6364, 1.6364, 1.6364))
 def test_light_behind_surface(self):
     m = Material()
     position = Point(0, 0, 0)
     eyev = Vector(0, 0, -1)
     normalv = Vector(0, 0, -1)
     light = PointLight(Point(0, 0, 10), Color(1, 1, 1))
     result = PointLight.lighting(m, Sphere(), light, position, eyev, normalv, False)
     self.assertEqual(result, Color(0.1, 0.1, 0.1))
 def test_lighting_surface_in_shadow(self):
     m = Material()
     position = Point(0, 0, 0)
     eyev = Vector(0, 0, -1)
     normalv = Vector(0, 0, -1)
     light = PointLight(Point(0, 0, -10), Color(1, 1, 1))
     in_shadow = True
     result = PointLight.lighting(m, Sphere(), light, position, eyev, normalv, in_shadow)
     self.assertEqual(result, Color(0.1, 0.1, 0.1))
Example #4
0
def set_light():
    # light_pos = np.array([-1, 1, 1])
    # light_theta = utils.degree2radians(30)
    # light_direction = utils.normalize(np.array([1, -1, 0]))
    # light = SpotLight(light_pos, light_theta, light_direction)
    # light_pos = np.array([0.0, 1.0, 0.01])
    light_pos = np.array([0, 1.7, 1])
    light_direction = -DEFAULT_N1
    light = PointLight(light_pos)
    light.nl = light_direction
    return light
 def test_lighting_with_pattern(self):
     m = Material()
     m.pattern = Stripe(Color(1, 1, 1), Color(0, 0, 0))
     m.ambient = 1
     m.diffuse = 0
     m.specular = 0
     eyev = Vector(0, 0, -1)
     normalv = Vector(0, 0, -1)
     light = PointLight(Point(0, 0, -10), Color(1, 1, 1))
     c1 = PointLight.lighting(m, Sphere(), light, Point(0.9, 0, 0), eyev, normalv, False)
     c2 = PointLight.lighting(m, Sphere(), light, Point(1.1, 0, 0), eyev, normalv, False)
     self.assertEqual(c1, Color(1, 1, 1))
     self.assertEqual(c2, Color(0, 0, 0))
Example #6
0
def initScene():
    global scene
    camera = Camera()
    camera.position = Vector(0, 1, 5)
    camera.point_at = Vector(0, 1, 0)
    scene = Scene(window.width, window.height, camera)
    log.info("Initialized scene with {0}x{1} image, {2}".format(window.width, window.height, camera))

    scene.ambient_light = color.scale(color.WHITE, 0.1)

    light = PointLight()
    light.position = Vector(5, 8, 8)
    log.info("Adding {0} to scene".format(light))
    scene.lights.append(light)

    light2 = PointLight()
    light2.position = Vector(-5, 8, 8)
    log.info("Adding {0} to scene".format(light2))
    scene.lights.append(light2)

    plane = Plane()
    plane.material.color = color.scale(color.WHITE, 0.5)
    plane.material.hardness = 0
    plane.material.specular = 0.5
    plane.material.reflection = 0.1
    log.info("Adding {0} to scene".format(plane))
    scene.shapes.append(plane)

    sphere = Sphere()
    sphere.position = Vector(1, 2, -3)
    sphere.radius = 1
    sphere.material.color = color.RED
    sphere.material.specular = 0.5
    sphere.material.hardness = 50
    sphere.material.reflection = 0.5
    log.info("Adding {0} to scene".format(sphere))
    scene.shapes.append(sphere)

    sphere2 = Sphere()
    sphere2.position = Vector(-1, 1, -1.5)
    sphere2.radius = 1
    sphere2.material.color = color.YELLOW
    sphere2.material.specular = 0.5
    sphere2.material.hardness = 50
    sphere2.material.reflection = 0.5
    log.info("Adding {0} to scene".format(sphere2))
    scene.shapes.append(sphere2)
Example #7
0
 def test_light_directly_behind_surface_and_eye(self):
     m = Material()
     position = Point(0, 0, 0)
     eyev = Vector(0, 0, -1)
     normalv = Vector(0, 0, -1)
     light = PointLight(Point(0, 0, 10), Color(1, 1, 1))
     result = lighting(m, light, position, eyev, normalv)
     self.assertEqual(result, Color(0.1, 0.1, 0.1))
Example #8
0
 def test_light_and_eye_perpendicular(self):
     m = Material()
     position = Point(0, 0, 0)
     eyev = Vector(0, 0, -1)
     normalv = Vector(0, 0, -1)
     light = PointLight(Point(0, 0, -10), Color(1, 1, 1))
     result = lighting(m, light, position, eyev, normalv)
     self.assertEqual(result, Color(1.9, 1.9, 1.9))
Example #9
0
 def test_shade_intersection_inside(self):
     w = World.default_world()
     w.light = PointLight(Point(0, 0.25, 0), Color(1, 1, 1))
     r = Ray(Point(0, 0, 0), Vector(0, 0, 1))
     shape = w.objects[1]
     i = Intersection(0.5, shape)
     comps = Computations.prepare_computations(i, r)
     c = World.shade_hit(w, comps)
     self.assertEqual(c, Color(0.90498, 0.90498, 0.90498))
Example #10
0
 def test_light_perpendicular_and_eye_at_45(self):
     r = math.sqrt(2) / 2
     m = Material()
     position = Point(0, 0, 0)
     eyev = Vector(0, r, -r)
     normalv = Vector(0, 0, -1)
     light = PointLight(Point(0, 0, -10), Color(1, 1, 1))
     result = lighting(m, light, position, eyev, normalv)
     self.assertEqual(result, Color(1.0, 1.0, 1.0))
Example #11
0
 def test_light_at_45_and_eye_at_neg_45(self):
     r = math.sqrt(2) / 2
     m = Material()
     position = Point(0, 0, 0)
     eyev = Vector(0, -r, -r)
     normalv = Vector(0, 0, -1)
     light = PointLight(Point(0, 10, -10), Color(1, 1, 1))
     result = lighting(m, light, position, eyev, normalv)
     x = 1.6363961
     self.assertColorEqual(result, Color(x, x, x))
Example #12
0
 def test_color_at_with_mutually_reflective_surfaces(self):
     w = World()
     w.light = PointLight(Point(0, 0, 0), Color(1, 1, 1))
     lower = Plane()
     lower.material.reflective = 1
     lower.transform = Transformations.translation(0, -1, 0)
     w.objects.append(lower)
     upper = Plane()
     upper.material.reflective = 1
     upper.transform = Transformations.translation(0, 1, 0)
     w.objects.append(upper)
     r = Ray(Point(0, 0, 0), Vector(0, 1, 0))
     self.assertIsNotNone(World.color_at(w, r))
Example #13
0
 def test_shade_hit_intersection_in_shadow(self):
     w = World()
     w.light = PointLight(Point(0, 0, -10), Color(1, 1, 1))
     s1 = Sphere()
     w.objects.append(s1)
     s2 = Sphere()
     s2.transform = Transformations.translation(0, 0, 10)
     w.objects.append(s2)
     r = Ray(Point(0, 0, 5), Vector(0, 0, 1))
     i = Intersection(4, s2)
     comps = Computations.prepare_computations(i, r)
     c = World.shade_hit(w, comps)
     self.assertEqual(c, Color(0.1, 0.1, 0.1))
Example #14
0
    def test_default_world(self):
        light = PointLight(Point(-10, 10, -10), Color(1, 1, 1))
        s1 = Sphere()
        s1.material.color = Color(0.8, 1.0, 0.6)
        s1.material.diffuse = 0.7
        s1.material.specular = 0.2

        s2 = Sphere()
        s2.transform = Transformations.scaling(0.5, 0.5, 0.5)

        w = World.default_world()
        self.assertEqual(w.light, light)
        self.assertTrue(s1 in w.objects)
        self.assertTrue(s2 in w.objects)
Example #15
0
 def test_point_light(self):
     p = Point(0, 0, 0)
     c = Color(1, 1, 1)
     l = PointLight(p, c)
     self.assertEqual(l.position, p)
     self.assertEqual(l.intensity, c)
Example #16
0
    right = Sphere()
    right.transform = Transformations.translation(1.5, 0.5, -0.5).dot(
        Transformations.scaling(0.5, 0.5, 0.5))
    right.material = Material()
    right.material.color = Color(0.5, 1, 0.1)
    right.material.diffuse = 0.7
    right.material.specular = 0.3

    # The smallest sphere is scaled by a third, before being translated
    left = Sphere()
    left.transform = Transformations.translation(-1.5, 0.33, -0.75).dot(
        Transformations.scaling(0.33, 0.33, 0.33))
    left.material = Material()
    left.material.color = Color(1, 0.8, 0.1)
    left.material.diffuse = 0.7
    left.material.specular = 0.3

    # The light source is white, shining from above and to the left
    world = World()
    world.objects = [floor, left_wall, right_wall, middle, right, left]
    world.light = PointLight(Point(-10, 10, -10), Color(1, 1, 1))

    # Camera
    camera = Camera(100, 50, math.pi / 3)
    camera.transform = World.view_transform(Point(0, 1.5, -5), Point(0, 1, 0),
                                            Vector(0, 1, 0))

    # render the result to a canvas
    canvas = Camera.render(camera, world)
    canvas.canvas_to_ppm()
Example #17
0
    wall_size = 7

    canvas_pixels = 100
    pixel_size = wall_size / canvas_pixels

    half = wall_size / 2

    canvas = Canvas(canvas_pixels, canvas_pixels)
    color = Color(1, 0, 0)
    shape = Sphere()
    shape.material = Material()
    shape.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)

    for y in range(canvas_pixels):
        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)

            r = Ray(ray_origin, Vector.normalize(position - ray_origin))
            # shrink along y-axis
            # r = r.transform(Transformations.scaling(1, 0.5, 1))

            # shrink along x-axis
            # r = r.transform(Transformations.scaling(0.5, 1, 1))
        return map[int(theta * mapWidth), int(phi * mapHeight)]


# Scene definition

scene = []

scene.append(Sphere(np.array([0.0,0.0,0.0]),\
    2.0, np.array([1.0, 1.0, 1.0]), 0.0, 0.5, 0.5, 32))

#scene.append(Sphere(np.array([0.0,-2.0,0.0]),\
#    1.0, np.array([1.0, 1.0, 1.0]), 0.0, 0.5, 0.5, 32))

# Lights
lights = []
lights.append(PointLight(np.array([0.0, 0.0, 3.0]),\
    np.array([1.0, 1.0, 1.0])))

# Sphere map
map = cv2.imread("resources/autumnCubeMap.hdr", -1)
tonemapReinhard = cv2.createTonemapReinhard(1.5, 1.0, 0.0, 1.0)
map = tonemapReinhard.process(map)
mapHeight = map.shape[1]
mapWidth = map.shape[0]

# Camera
t = 0.0  # Camera control parameter
cam = Camera(np.array([4.0 * np.cos(t), 4.0 * np.sin(t), 0.0]),\
    np.array([0.0, 0.0, 0.0]), np.array([0.0, 0.0, 1.0]),\
    20, 1.25, 25, 400)

# Screen
Example #19
0
from camera import Camera
from color import Color
from light import PointLight
from obj_file import ObjFile
from tuple import *
from world import World

if __name__ == '__main__':
    parser = ObjFile.parse_obj_file("teapot-low.obj")
    teapot = ObjFile.obj_to_group(parser)
    teapot.material.ambient = .3
    teapot.material.color = Color(.75, .1, .1)

    teapot.divide(1)

    # The light source is white, shining from above and to the left
    world = World()
    world.objects = [teapot]
    world.light = PointLight(Point(-5, 30, 25), Color(1, 1, 1))

    # Camera
    camera = Camera(200, 150, math.pi / 3)
    # camera = Camera(50, 50, math.pi / 3)
    camera.transform = World.view_transform(Point(0, 30, 25), Point(0, 0, 0),
                                            Vector(0, 0, 1))

    # render the result to a canvas
    canvas = Camera.render(camera, world)
    canvas.canvas_to_ppm()
Example #20
0
 def test_point_light(self):
     intensity = Color(1, 1, 1)
     position = Point(0, 0, 0)
     light = PointLight(position, intensity)
     self.assertEqual(light.position, position)
     self.assertEqual(light.intensity, intensity)
Example #21
0
from color import Color
from ray import Ray
import intersection
from light import PointLight, lighting

W = 200
H = 200
D = 200
im = Image.new('RGB', (W, H))
pix = im.load()

sphere = Sphere()
sphere.material.color = Color(1, 0.2, 1)
light_color = Color(1, 1, 1)
light_position = Point(-10, 10, -10)
light = PointLight(light_position, light_color)

## this is done in object coordinates
eye = Point(0, 0, -5)
tt = Matrix.translate(0, 0, -2)
sphere.transform = tt
wall = (-3, 3, -3, 3)  # LRBT

# the -1 flips the y-coordinate so it's up
my = Matrix.scale(1.0, -1.0, 1.0)
ms = Matrix.scale(
    float(wall[1] - wall[0]) / W,
    float(wall[3] - wall[2]) / H, 1.0)
mt = Matrix.translate(wall[0], wall[2], 0)
m = my * mt * ms