-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.go
59 lines (49 loc) · 1.29 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
package main
import (
"math"
"math/rand"
)
type cameraConfig struct {
location vec3
lookingAt vec3
upDirection vec3
fovDegrees float64
focalLength float64
focalRatio float64
}
type camera struct {
screenLoc vec3
screenX vec3
screenY vec3
eyeLoc vec3
eyeX vec3
eyeY vec3
}
func newCamera(cfg cameraConfig) *camera {
cam := new(camera)
upDirection := cfg.upDirection.unit()
viewDirection := cfg.lookingAt.sub(cfg.location).unit()
cam.screenX = viewDirection.cross(upDirection)
cam.screenY = cam.screenX.cross(viewDirection)
cam.eyeX = cam.screenX.scale(cfg.focalLength / cfg.focalRatio)
cam.eyeY = cam.screenY.scale(cfg.focalLength / cfg.focalRatio)
cam.eyeLoc = cfg.location
fovRads := cfg.fovDegrees / 180 * math.Pi
halfScreenWidth := math.Tan(fovRads/2) * cfg.focalLength
cam.screenX = cam.screenX.scale(halfScreenWidth)
cam.screenY = cam.screenY.scale(halfScreenWidth)
cam.screenLoc = cam.eyeLoc.add(viewDirection.scale(cfg.focalLength))
return cam
}
func (c *camera) makeRay(x, y float64, rng *rand.Rand) ray {
start := c.eyeLoc.
add(c.eyeX.scale(2*rng.Float64() - 1.0)).
add(c.eyeY.scale(2*rng.Float64() - 1.0))
end := c.screenLoc.
add(c.screenX.scale(x)).
add(c.screenY.scale(y))
return ray{
origin: start,
uDir: end.sub(start).unit(),
}
}