/
camera.go
116 lines (93 loc) · 2.15 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package oglabstr
import (
"github.com/go-gl/mathgl/mgl32"
"github.com/chewxy/math32"
)
type Direction byte
const (
Forward Direction = iota
Backward
Right
Left
Up
Down
)
type Camera struct {
Position mgl32.Vec3
Front, Up, Right mgl32.Vec3
WorldUp mgl32.Vec3
Yaw, Pitch float32
MovementSpeed float32
Sensitivity float32
Zoom float32
//For perspective projection
Near, Far float32
}
func NewCamera(position mgl32.Vec3) *Camera {
c := Camera{}
c.Position = position
c.Front = mgl32.Vec3{0.0, 0.0, -1.0}
c.WorldUp = mgl32.Vec3{0.0, 1.0, 0.0}
c.Yaw = -90.0
c.Pitch = 0.0
c.MovementSpeed = 2.5
c.Sensitivity = 0.1
c.Zoom = 45.0
c.Near = 0.1
c.Far = 100.0
c.updateVectors()
return &c
}
func (c *Camera) GetViewMat() mgl32.Mat4 {
return mgl32.LookAtV(c.Position, c.Position.Add(c.Front), c.Up)
}
func (c *Camera) KeyboardUpdate(direction Direction, deltaTime float32) {
velocity := c.MovementSpeed * deltaTime
switch direction {
case Forward:
c.Position = c.Position.Add(c.Front.Mul(velocity))
case Backward:
c.Position = c.Position.Sub(c.Front.Mul(velocity))
case Right:
c.Position = c.Position.Add(c.Right.Mul(velocity))
case Left:
c.Position = c.Position.Sub(c.Right.Mul(velocity))
case Up:
c.Position = c.Position.Add(c.Up.Mul(velocity))
case Down:
c.Position = c.Position.Sub(c.Up.Mul(velocity))
}
}
func (c *Camera) MouseUpdate(xoffset, yoffset float32) {
xoffset *= c.Sensitivity
yoffset *= c.Sensitivity
c.Yaw += xoffset
c.Pitch += yoffset
if c.Pitch > 89.0 {
c.Pitch = 89.0
}
if c.Pitch < -89.0 {
c.Pitch = -89.0
}
c.updateVectors()
}
func (c *Camera) ScrollUpdate(yoffset float32) {
if c.Zoom >= 1.0 && c.Zoom <= 45.0 {
c.Zoom -= yoffset
}
if c.Zoom <= 1.0 {
c.Zoom = 1.0
}
if c.Zoom >= 45.0 {
c.Zoom = 45.0
}
}
func (c *Camera) updateVectors() {
c.Front = mgl32.Vec3{
math32.Cos(mgl32.DegToRad(c.Yaw)) * math32.Cos(mgl32.DegToRad(c.Pitch)),
math32.Sin(mgl32.DegToRad(c.Pitch)),
math32.Sin(mgl32.DegToRad(c.Yaw)) * math32.Cos(mgl32.DegToRad(c.Pitch)),
}.Normalize()
c.Right = c.Front.Cross(c.WorldUp).Normalize()
c.Up = c.Right.Cross(c.Front).Normalize()
}