-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.go
61 lines (48 loc) · 1.36 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
package raytrace
import (
"fmt"
)
type Camera struct {
position Vector
lookAt Vector
equator Vector
up Vector // defines the tilt of the camera
screen Vector // defines the center position of the viewport/screen in 3D space
}
func CreateCameraDefaultUp(position Vector, lookAt Vector) *Camera {
return CreateCamera(position, lookAt, Vector{0.0, 1.0, 0.0})
}
func CreateCamera(position Vector, lookAt Vector, up Vector) *Camera {
normalizedUp := up.Normalize()
return &Camera{
up: normalizedUp,
position: position,
lookAt: lookAt,
equator: lookAt.Normalize().Cross(normalizedUp),
screen: position.Add(lookAt)}
}
func (c *Camera) Position() Vector {
return c.position
}
func (c *Camera) LookAt() Vector {
return c.lookAt
}
func (c *Camera) Equator() Vector {
return c.equator
}
func (c *Camera) Up() Vector {
return c.up
}
func (c *Camera) Screen() Vector {
return c.screen
}
func (c *Camera) GetRay(vx float64, vy float64) Ray {
// pos = Screen - Up * vy - Equator * vx;
pos := c.screen.Subtract(c.up.MultiplyFloat(vy)).Subtract(c.equator.MultiplyFloat(vx))
dir := pos.Subtract(c.position)
ray := Ray{position: pos, direction: dir.Normalize()}
return ray
}
func (c *Camera) String() string {
return fmt.Sprintf("(Camera Pos %v LookAt %v Equator %v Up %v Screen %v )", c.Position(), c.LookAt(), c.Equator(), c.Up(), c.Screen())
}