Пример #1
0
 def test_construct_ppm_header(self):
     canvas = Canvas(5, 3)
     ppm = canvas.to_ppm()
     lines = ppm.split('\n')
     self.assertTrue(lines[0] == 'P3')
     self.assertTrue(lines[1] == '5 3')
     self.assertTrue(lines[2] == '255')
Пример #2
0
 def test_wrap_ppm_pixel_data_at_70_chars(self):
     canvas = Canvas(10, 2, Color(1, 0.8, 0.6))
     ppm = canvas.to_ppm()
     lines = ppm.split('\n')
     self.assertTrue(lines[3] == '255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204')
     self.assertTrue(lines[4] == '153 255 204 153 255 204 153 255 204 153 255 204 153')
     self.assertTrue(lines[5] == '255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204')
     self.assertTrue(lines[6] == '153 255 204 153 255 204 153 255 204 153 255 204 153')
Пример #3
0
 def test_splitting_long_lines_in_ppm(self):
     c = Canvas(10, 2)
     for x in range(c.width):
         for y in range(c.height):
             c.write_pixel(x, y, Color(1, 0.8, 0.6))
     ppm = c.to_ppm()
     assert ''.join(ppm[3:]) == "255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204\n"\
                                "153 255 204 153 255 204 153 255 204 153 255 204 153\n" \
                                "255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204\n"\
                                "153 255 204 153 255 204 153 255 204 153 255 204 153\n"
Пример #4
0
 def test_constructing_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()
     assert ''.join(ppm[3:]) == "255 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" \
                                "0 0 0 0 0 0 0 128 0 0 0 0 0 0 0\n" \
                                "0 0 0 0 0 0 0 0 0 0 0 0 0 0 255\n"
Пример #5
0
 def test_construct_ppm_pixel_data(self):
     canvas = Canvas(5, 3)
     c1 = Color(1.5, 0, 0)
     c2 = Color(0, 0.5, 0)
     c3 = Color(-0.5, 0, 1)
     canvas.write_pixel(0, 0, c1)
     canvas.write_pixel(2, 1, c2)
     canvas.write_pixel(4, 2, c3)
     ppm = canvas.to_ppm()
     lines = ppm.split('\n')
     self.assertTrue(lines[3] == '255 0 0 0 0 0 0 0 0 0 0 0 0 0 0')
     self.assertTrue(lines[4] == '0 0 0 0 0 0 0 128 0 0 0 0 0 0 0')
     self.assertTrue(lines[5] == '0 0 0 0 0 0 0 0 0 0 0 0 0 0 255')
Пример #6
0
    half = wall_size / 2

    canvas = Canvas(canvas_pixels, canvas_pixels)
    red = Color(1, 0, 0)
    sphere = Sphere()
    m = Material()
    m.color = Color(1, 0.2, 1)
    sphere.material = m

    light = PointLight(Point(-10, 10, -10), Color(1, 1, 1))

    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)
            ray = Ray(ray_origin, (position - ray_origin).normalize())
            hit = sphere.intersect(ray).hit()
            if hit:
                point = ray.position(hit.t)
                normal = hit.object.normal_at(point)
                eye = -ray.direction

                color = hit.object.material.lighting(light, point, eye, normal)

                canvas.write_pixel(x, y, color)

    write_ppm_to_file(canvas.to_ppm(),
                      f'..{sep}..{sep}resources{sep}circle.ppm')
Пример #7
0
 def test_ppm_files_terminated_with_newline(self):
     c = Canvas(5, 3)
     ppm = c.to_ppm()
     assert ppm[-1][-1] == '\n'
Пример #8
0
 def test_constructing_ppm_header(self):
     c = Canvas(5, 3)
     ppm = c.to_ppm()
     assert ''.join(ppm[:3]) == "P3\n" \
                                "5 3\n" \
                                "255\n"
Пример #9
0
 def test_ppm_ends_with_newline_char(self):
     canvas = Canvas(5, 3)
     ppm = canvas.to_ppm()
     self.assertTrue(ppm[len(ppm) - 1] == '\n')