示例#1
0
 def test_chained_transformations_are_in_reverse_order(self):
     p = Point(1, 0, 1)
     a = rotation_x(math.pi / 2)
     b = scaling(5, 5, 5)
     c = translation(10, 5, 7)
     t = c * b * a * p
     assert t == Point(15, 0, 7)
示例#2
0
 def test_individual_transformations_in_sequence(self):
     p = Point(1, 0, 1)
     a = rotation_x(math.pi / 2)
     b = scaling(5, 5, 5)
     c = translation(10, 5, 7)
     p2 = a * p
     assert p2 == Point(1, -1, 0)
     p3 = b * p2
     assert p3 == Point(5, -5, 0)
     p4 = c * p3
     assert p4 == Point(15, 0, 7)
from raytracer.lights import PointLight
from raytracer.patterns import *
from raytracer.matrices import scaling, rotation_x, rotation_y, rotation_z, translation, view_transform
from raytracer.scene import World
from raytracer.shapes import Cylinder, Plane
from raytracer.tuples import Color, Point, Vector

if __name__ == '__main__':
    floor = Plane()
    floor.material.color = Color(.8, .8, .8)
    floor.material.reflective = .2

    cylinder = Cylinder(closed=True)
    cylinder.maximum = 0.25
    cylinder.material.color = Color(.8, 0, 0)
    cylinder.material.reflective = .6
    cylinder.transformation = translation(0, 1, 0)

    world = World()
    world.add(floor, cylinder)
    world.light_source = PointLight(Point(-10, 10, -10), Color.white())

    camera = Camera(160, 120, pi / 3)
    camera.transformation = view_transform(Point(0, 1.5, -5), Point(
        0, 1, 0), Vector(0, 1, 0)) * rotation_y(-pi / 4) * rotation_x(-pi / 6)

    canvas = camera.render(world)

    write_ppm_to_file(canvas.to_ppm(),
                      f'..{sep}..{sep}resources{sep}cylinders.ppm')
示例#4
0
 def test_inverse_of_x_rotation_rotates_opposite_direction(self):
     p = Point(0, 1, 0)
     full_quarter = rotation_x(math.pi / 2)
     inv = full_quarter.inverse()
     assert inv * p == Point(0, 0, -1)
示例#5
0
 def test_rotate_point_around_x_axis(self):
     p = Point(0, 1, 0)
     half_quarter = rotation_x(math.pi / 4)
     full_quarter = rotation_x(math.pi / 2)
     assert half_quarter * p == Point(0, math.sqrt(2) / 2, math.sqrt(2) / 2)
     assert full_quarter * p == Point(0, 0, 1)
from raytracer.materials import Material
from raytracer.matrices import scaling, rotation_y, rotation_x, translation, view_transform
from raytracer.scene import World
from raytracer.shapes import Sphere
from raytracer.tuples import Color, Point, Vector

if __name__ == '__main__':
    floor = Sphere()
    floor.transformation = scaling(10, 0.01, 10)
    floor.material = Material()
    floor.material.color = Color(1, 0.9, 0.9)
    floor.material.specular = 0

    left_wall = Sphere()
    left_wall.transformation = translation(0, 0, 5) * \
        rotation_y(-pi / 4) * rotation_x(pi / 2) * \
        scaling(10, 0.01, 10)
    left_wall.material = floor.material

    right_wall = Sphere()
    right_wall.transformation = translation(0, 0, 5) * \
        rotation_y(pi / 4) * rotation_x(pi / 2) * \
        scaling(10, 0.01, 10)
    right_wall.material = floor.material

    middle = Sphere()
    middle.transformation = translation(-0.5, 1, 0.5)
    middle.material = Material()
    middle.material.color = Color(0.1, 1, 0.5)
    middle.material.diffuse = 0.7
    middle.material.specular = 0.3