2019年6月16日日曜日

Python - Triangles - Rendering

The Ray Tracer Challenge: A Test-Driven Guide to Your First 3D Renderer (Jamis Buck(著)、Pragmatic Bookshelf)、Chapter 15(Triangles)のRendering some trianglesを取り組んでみる。

コード

Python 3

```#!/usr/bin/env python3
import math
import time
from tuples import Point, Vector, Color
from planes import Plane
from cylinders import Cylinder
from materials import Material
from camera import Camera
from lights import Light
from world import World
from transformations import translation, view_transform
from transformations import rotation_x, rotation_y, rotation_z
from triangles import Triangle
print('ファイル名, rendering time(秒)')

width = 250
height = 125

wall1 = Plane(material=Material(color=Color(0, 0, 1)),
transform=translation(0, 0, 7) *
rotation_y(-math.pi / 4) *
rotation_x(math.pi / 2))
wall2 = Plane(material=Material(color=Color(1, 0, 0)),
transform=translation(0, 0, 7) *
rotation_y(math.pi / 4) *
rotation_x(math.pi / 2))

floor = Plane(material=Material(Color(0, 1, 0)),
transform=translation(0, 0, 0))

triangle1 = Triangle(Point(0, 2, 0), Point(-1, 0, 0), Point(0, 0, -1),
transform=translation(0, 0, -2))
triangle2 = Triangle(Point(0, 2, 0), Point(0, 0, -1), Point(1, 0, 0),
transform=translation(0, 0, -2))
camera = Camera(width, height, math.pi / 2,
transform=view_transform(Point(0, 1.5, -5), Point(0, 1, 0),
Vector(0, 1, 0)))
world = World([floor, wall1, wall2, triangle1, triangle2],
Light(Point(-10, 10, -10), Color(1, 1, 1)))
start = time.time()
canvas = camera.render(world)
s = time.time() - start
with open(f'sample6.ppm', 'w') as f:
canvas.to_ppm(f)
print(f'sample6.ppm,{s}')
```

```C:\Users\...>py sample6.py
ファイル名, rendering time(秒)
sample6.ppm,247.3691611289978

C:\Users\...>
```