Esempio n. 1
0
def test_camera_transform_ray():
    c = Camera(201, 101, math.pi / 2)
    c.transform = RotationY(math.pi / 4) * Translation(0, -2, 5)
    r = c.ray_for_pixel(100, 50)
    assert r.origin == Point(0, 2, -5)
    print(r.direction)
    assert r.direction == Vector(math.sqrt(2) / 2, 0, -math.sqrt(2) / 2)
Esempio n. 2
0
 def test_rendering_world_with_camera(self):
     w = World.default()
     c = Camera(11, 11, math.pi / 2)
     frm = Point(0, 0, -5)
     to = Point(0, 0, 0)
     up = Vector(0, 1, 0)
     c.transform = ViewTransform(frm, to, up)
     image = c.render(w)
     self.assertEqual(image.pixel_at(5, 5), Color(0.38066, 0.47583, 0.2855))
Esempio n. 3
0
def test_render():
    w = World.default()
    c = Camera(11, 11, math.pi / 2)
    f = Point(0, 0, -5)
    to = Point(0, 0, 0)
    up = Vector(0, 1, 0)
    c.transform = ViewTransform(f, to, up)
    image = c.render(w)
    assert image.read_pixel(5, 5) == Color(0.38066, 0.47583, 0.2855)
Esempio n. 4
0
from raytracer.parser import Parser
from raytracer.world import World
from raytracer.camera import Camera
from raytracer.lights import PointLight
from raytracer.base import Point, Color, ViewTransform, Vector
f = open("tests/obj_files/face.obj", "r")
p = Parser(f)
w = World()
w.objects.append(p.obj_to_group())
w.light = PointLight(Point(10, 5, 5), Color(1, 1, 1))
c = Camera(10, 10, 0.785)
c.transform = ViewTransform(Point(-6, 6, -10), Point(6, 0, 6),
                            Vector(-0.45, 1, 0))
canvas = c.render(w)
with open("images/triangl.ppm", "w") as f:
    f.write(canvas.to_ppm())
# This is wayyy too slow we're gonna need a triangle mesh or something
Esempio n. 5
0
middle.material.refractive_index = 1.5
middle.material.specular = 1
middle.material.shininess = 300

right = Sphere()
right.set_transform(Translation(1.5, 0.5, -0.5) * Scaling(0.5, 1, 0.5))
right.material.pattern = StripePattern(Color(1, 1, 1), Color(0, 1, 0))
right.material.pattern.set_pattern_transform(
    Scaling(0.1, 0.1, 0.1) * RotationY(math.pi / 4))
right.material.color = Color(0.1, 1, 0.5)
right.material.diffuse = 0.7
right.material.specular = 0.3

left = Sphere()
left.transform = Translation(-1.5, 0.33, -0.75) * Scaling(0.33, 0.33, 0.33)
left.material.color = Color(1, 0.8, 0.1)
left.material.diffuse = 0.7
left.material.specular = 0.3

world = World()
world.objects.extend([floor, middle, right, left])
world.light = PointLight(Point(-10, 10, -10), Color(1, 1, 1))

camera = Camera(1024, 512, math.pi / 3)
camera.transform = ViewTransform(Point(0, 1.5, -5), Point(0, 1, 0),
                                 Vector(0, 1, 0))

canvas = camera.render(world)
with open("pattern.ppm", "w") as f:
    f.write(canvas.to_ppm())