-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.go
63 lines (55 loc) · 1.65 KB
/
camera.go
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
62
63
package gotrace
import (
"math"
"math/rand"
)
// A Camera is the eye through which the the scene is observed
type Camera struct {
origin Vec3
horizontal Vec3
vertical Vec3
corner Vec3
u, v, w Vec3
lensRadius float64
tStart, tStop float64
AspectRatio float64
}
// NewCamera creates a camera
func NewCamera(lookFrom, lookAt, up Vec3, verticalFOV, aspectRatio, aperture, focusDist, tStart, tStop float64) Camera {
theta := (math.Pi * verticalFOV) / 180.0
height := math.Tan(theta / 2.0)
width := aspectRatio * height
w := lookFrom.Sub(lookAt).Unit()
u := up.Cross(w).Unit()
v := w.Cross(u)
horizontal := u.Scale(2 * width * focusDist)
vertical := v.Scale(2 * height * focusDist)
corner := lookFrom.Sub(u.Scale(width * focusDist)).Sub(v.Scale(height * focusDist)).Sub(w.Scale(focusDist))
return Camera{
origin: lookFrom,
horizontal: horizontal,
vertical: vertical,
corner: corner,
u: u,
v: v,
w: w,
lensRadius: aperture / 2.0,
tStart: tStart,
tStop: tStop,
AspectRatio: aspectRatio,
}
}
// RayTo casts a Ray from the camera to the given (u, v) coordinates
// the Ray is cast at a random time during the camera lens' opening
func (c Camera) RayTo(s float64, t float64, rnd *rand.Rand) Ray {
rd := RandDisk(rnd).Scale(c.lensRadius)
offset := c.u.Scale(rd.X).Add(c.v.Scale(rd.Y))
hOffset := c.horizontal.Scale(s)
vOffset := c.vertical.Scale(t)
return Ray{
Origin: c.origin.Add(offset),
Direction: c.corner.Add(hOffset).Add(vOffset).Sub(c.origin).Sub(offset),
Time: rnd.Float64()*(c.tStop-c.tStart) + c.tStart,
RandSource: rnd,
}
}