-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxform.go
73 lines (65 loc) · 1.57 KB
/
xform.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
package sge
import "github.com/klkblake/s3dm"
type XformNode struct {
Parent *XformNode
Children []*XformNode
Xform s3dm.XformScale
WorldXform s3dm.XformScale
Leaf Leaf
}
func NewXformNode() *XformNode {
node := new(XformNode)
node.Xform = s3dm.XformScaleIdentity
node.WorldXform = s3dm.XformScaleIdentity
return node
}
func (node *XformNode) Add(child *XformNode) {
if child.Parent != nil {
child.Parent.Remove(child)
}
node.Children = append(node.Children, child)
child.Parent = node
}
func (node *XformNode) Remove(child *XformNode) {
for i, n := range node.Children {
if n == child {
node.Children = append(node.Children[:i], node.Children[i+1:]...)
break
}
}
if len(node.Children) == 0 {
node.Children = nil
}
if child.Parent == node {
child.Parent = nil
}
}
func (node *XformNode) Attach(leaf Leaf) {
node.Add(leaf.XformNode())
}
func (node *XformNode) Update() {
if node.Parent != nil {
// XXX this should be a function in s3dm.
pxf := node.Parent.WorldXform
xf := node.Xform
var wxf s3dm.XformScale
// XXX Dodgy hax, will be going away soon.
wxf.Position = pxf.Position.Add(xf.Position.Sub(s3dm.Position{}).Rotate(pxf.Rotation).Mul(pxf.Scale))
wxf.Rotation = pxf.Rotation.Mul(xf.Rotation)
wxf.Scale = pxf.Scale.Mul(xf.Scale.Rotate(pxf.Rotation))
node.WorldXform = wxf
} else {
node.WorldXform = node.Xform
}
for _, child := range node.Children {
child.Update()
}
}
func (node *XformNode) Walk(f func(Leaf)) {
if node.Leaf != nil {
f(node.Leaf)
}
for _, child := range node.Children {
child.Walk(f)
}
}