/
camera.go
66 lines (58 loc) · 1.44 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
64
65
66
package sundog
import (
"math"
"math/rand"
"github.com/sporsh/gosundog/geometry"
"github.com/sporsh/gosundog/v3"
)
type Camera struct {
Origin v3.V
Basis geometry.Basis
Aperture float64
FocalLength float64
FieldOfView float64
AspectRatio float64
}
// RayThrough computes a ray in the world vector space
// that corresponds to the u, v screen coordinates between [-1, 1]
// as seen in the visible field from of cacmera
func (c *Camera) RayThrough(u, v float64) geometry.Ray {
origin := c.RandomOriginWithinAperture()
target := v3.Add(
c.Origin,
c.Basis.ToLocal(v3.V{
X: u * c.FieldOfView * c.FocalLength,
Y: v * c.FieldOfView * c.FocalLength,
Z: c.FocalLength,
}),
)
direction := v3.Normalize(v3.Sub(target, origin))
return geometry.Ray{
Direction: direction,
Origin: origin,
TMin: 0,
TMax: math.Inf(1),
}
}
// LookAt reorients the camera's basis so that it is looking straight at a target
func (c *Camera) LookAt(target v3.V) {
}
// RandomOriginWithinAperture returns, as a three dimensional vector, a point
// in world space that lies within the camera's aperture
func (c *Camera) RandomOriginWithinAperture() v3.V {
if c.Aperture == 0 {
return c.Origin
}
u1, u2 := rand.Float64(), rand.Float64()
r := u2 * c.Aperture
theta := u1 * 2 * math.Pi
localPoint := v3.V{
X: math.Cos(theta) * r,
Y: math.Sin(theta) * r,
Z: 0,
}
return v3.Add(
c.Basis.ToLocal(localPoint),
c.Origin,
)
}