-
Notifications
You must be signed in to change notification settings - Fork 21
/
camera.py
31 lines (26 loc) · 1.09 KB
/
camera.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import taichi as ti
from vector import *
import math
@ti.data_oriented
class Camera:
def __init__(self, vfrom, at, up, fov, aspect_ratio, aperture, focus_dist):
theta = math.radians(fov)
h = math.tan(theta / 2.0)
viewport_height = 2.0 * h
viewport_width = viewport_height * aspect_ratio
focal_length = 1.0
w = (vfrom - at).normalized()
self.u = up.cross(w).normalized()
self.v = w.cross(self.u)
self.origin = vfrom
self.horizontal = focus_dist * viewport_width * self.u
self.vertical = focus_dist * viewport_height * self.v
self.lower_left_corner = self.origin - (self.horizontal / 2.0) \
- (self.vertical / 2.0) \
- focus_dist * w
self.lens_radius = aperture / 2.0
@ti.func
def get_ray(self, u, v):
rd = self.lens_radius * random_in_unit_disk()
offset = self.u * rd.x + self.v * rd.y
return self.origin + offset, self.lower_left_corner + u * self.horizontal + v * self.vertical - self.origin - offset