-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.py
34 lines (29 loc) · 1.4 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
32
33
34
import random
from ray import *
from vector import *
def randomInUnitDisk():
p = vec3(random.uniform(0, 1), random.uniform(0, 1), 0) * 2.0 - vec3(1, 1, 0)
while (vec3.dot(p, p) >= 1.0):
p = vec3(random.uniform(0, 1), random.uniform(0, 1), 0) * 2.0 - vec3(1, 1, 0)
return p
class Camera:
def __init__(self, aspect, fov, up, position, look_at, aperture = 0.1, focus_dist = 1.0, time0 = 0.0, time1 = 0.0):
theta = fov * math.pi / 180
half_height = math.tan(theta / 2)
half_width = aspect * half_height
w = (position - look_at).getNorm()
u = vec3.cross(up, w).getNorm()
v = vec3.cross(w, u)
self.__position = position
self.__lower_left = position - (u * half_width + v * half_height + w) * focus_dist
self.__horizontal = u * 2 * half_width * focus_dist
self.__vertical = v * 2 * half_height * focus_dist
self.__lens_radius = aperture / 2.0
self.__time0 = time0
self.__time1 = time1
def getRay(self, u, v):
rd = randomInUnitDisk() * self.__lens_radius
offset = u * rd[0] + v * rd[1]
position = vec3(self.__position[0] + offset, self.__position[1] + offset, self.__position[2] + offset)
time = self.__time0 + random.uniform(0, 1) * (self.__time0 - self.__time1)
return ray(position, self.__lower_left + self.__horizontal*u + self.__vertical*v - position, time)