-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.py
51 lines (45 loc) · 2.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# MiniLight Python : minimal global illumination renderer
#
# Copyright (c) 2007-2008, Harrison Ainsworth / HXA7241 and Juraj Sukop.
# http://www.hxa7241.org/
from math import pi, tan
from random import random
from raytracer import RayTracer
from vector3f import Vector3f
import re
SEARCH = re.compile('(\(.+\))\s*(\(.+\))\s*(\S+)').search
class Camera(object):
def __init__(self, in_stream):
for line in in_stream:
if not line.isspace():
p, d, a = SEARCH(line).groups()
self.view_position = Vector3f(p)
self.view_direction = Vector3f(d).unitize()
if self.view_direction.is_zero():
self.view_direction = Vector3f(0.0, 0.0, 1.0)
self.view_angle = min(max(10.0, float(a)), 160.0) * (pi / 180.0)
self.right = Vector3f(0.0, 1.0, 0.0).cross(self.view_direction).unitize()
if self.right.is_zero():
self.up = Vector3f(0.0, 0.0, 1.0 if self.view_direction.y else -1.0)
self.right = self.up.cross(self.view_direction).unitize()
else:
self.up = self.view_direction.cross(self.right).unitize()
break
def get_frame(self, scene, image, frame_type='depth'):
raytracer = RayTracer(scene)
aspect = float(image.height) / float(image.width)
for y in range(image.height):
for x in range(image.width):
x_coefficient = ((x + random()) * 2.0 / image.width) - 1.0
y_coefficient = ((y + random()) * 2.0 / image.height) - 1.0
offset = self.right * x_coefficient + self.up * (y_coefficient * aspect)
sample_direction = (self.view_direction + (offset * tan(self.view_angle * 0.5))).unitize()
if frame_type == 'depth':
distance = raytracer.get_distance_to_first_hit(self.view_position, sample_direction)
image.add_to_pixel(x, y, distance)
elif frame_type == 'reverse_depth':
distance = raytracer.get_distance_to_last_hit(self.view_position, sample_direction)
image.add_to_pixel(x, y, distance)
else:
radiance = raytracer.get_radiance(self.view_position, sample_direction)
image.add_to_pixel(x, y, radiance)