-
Notifications
You must be signed in to change notification settings - Fork 3
/
camera.go
28 lines (24 loc) · 1.06 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
package main
import (
"math"
)
// A Camera is defined by a Ray (for the location and orientation of the image
// plane), the width of the image plane, a horizontal angle of view, and an
// aspect ratio. For now, there's no rotation specified so the top and bottom
// edges of the image plane are always parallel to the xz plane.
type Camera struct {
Loc Ray // Location; Loc.V1 is the center of the image plane
Width float64 // Width of the image plane
Haov Rad // Horizontal angle of view
Aspect float64 // Aspect ratio: height / width
}
// Vantage returns the vantage point for the camera. This is the origin of rays.
// It is situated behind the center of the image plane.
func (c *Camera) Vantage() Vec3 {
// First find how far behind the image plane the vp is.
// tan(half of aspect ratio) = (half of image plane width) / distance
d := (0.5 * c.Width) / math.Tan(float64(0.5*c.Haov))
// Construct the vector of magnitude d and the right direction to go
// from the image plane center to the vantage point.
return c.Loc.D.Normalize().Mul(-d).Add(c.Loc.V)
}