/
transform.go
80 lines (65 loc) · 1.5 KB
/
transform.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
package gogol
import (
"github.com/go-gl/gl/v2.1/gl"
)
type Transform struct {
position Vector
scale Vector
rotation float32
parentTransform *Transform
childTransforms []*Transform
Matrix Matrix
NeedsUpdate bool
}
var rootTransform *Transform
func init() {
rootTransform = &Transform{
parentTransform: nil,
Matrix: NewMatrix(),
}
}
func NewTransform(parent *Transform) *Transform {
t := &Transform{
parentTransform: parent,
Matrix: NewMatrix(),
}
if parent == nil {
t.parentTransform = rootTransform
}
return t
}
func (t *Transform) AddChild(c *Transform) {
c.parentTransform = t
t.childTransforms = append(t.childTransforms, c)
}
func (t *Transform) Translate(x float32, y float32) {
t.position.X = x
t.position.Y = y
t.SetNeedsUpdate()
}
func (t *Transform) Rotate(deg float32) {
t.rotation += deg
t.SetNeedsUpdate()
}
func (t *Transform) SetNeedsUpdate() {
t.NeedsUpdate = true
for _, c := range t.childTransforms {
c.SetNeedsUpdate()
}
}
func (t *Transform) Update() {
var m *float32
if t.parentTransform != nil {
m = &(t.parentTransform.Matrix[0])
} else {
m = &(t.Matrix[0])
}
gl.LoadMatrixf(m)
gl.Translatef(t.position.X, t.position.Y, 0)
gl.Rotatef(t.rotation, 0, 0, -1)
gl.GetFloatv(gl.MODELVIEW_MATRIX, &(t.Matrix[0]))
t.NeedsUpdate = false
for _, c := range t.childTransforms {
c.Update()
}
}