-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.go
More file actions
61 lines (51 loc) · 1.36 KB
/
camera.go
File metadata and controls
61 lines (51 loc) · 1.36 KB
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
52
53
54
55
56
57
58
59
60
61
package main
import "math"
func NewCamera(
position Vec3,
lookAt Vec3,
up Vec3,
vfov,
aspectRatio float64,
aperture float64,
focusDist float64,
) Camera {
theta := degreesToRadians(vfov)
h := math.Tan(theta / 2)
viewportHeight := 2.0 * h
viewportWidth := aspectRatio * viewportHeight
w := position.Sub(lookAt).Unit()
u := up.Cross(w).Unit()
v := w.Cross(u)
origin := position
horizontal := u.ScalarMul(focusDist * viewportWidth)
vertical := v.ScalarMul(focusDist * viewportHeight)
lowerLeftCorner := origin.Sub(horizontal.ScalarDiv(2)).Sub(vertical.ScalarDiv(2)).Sub(w.ScalarMul(focusDist))
lensRadius := aperture / 2
return Camera{
origin: origin,
horizontal: horizontal,
vertical: vertical,
lensRadius: lensRadius,
lowerLeftCorner: lowerLeftCorner,
u: u,
v: v,
w: w,
}
}
type Camera struct {
origin Vec3
horizontal Vec3
vertical Vec3
lowerLeftCorner Vec3
u, v, w Vec3
lensRadius float64
}
func (c *Camera) GetRay(s, t float64) *Ray {
rd := Vec3RandomInUnitDisk().ScalarMul(c.lensRadius)
offset := c.u.ScalarMul(rd.x).Add(c.v.ScalarMul(rd.y))
direction := c.lowerLeftCorner.Add(c.horizontal.ScalarMul(s)).Add(c.vertical.ScalarMul(t)).Sub(c.origin).Sub(offset)
return &Ray{
origin: c.origin.Add(offset),
direction: direction,
}
}