-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.go
46 lines (38 loc) · 984 Bytes
/
scene.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
package wengine
import "github.com/go-gl/mathgl/mgl32"
type ObjectMap map[string]*Object
type Scene struct {
objects ObjectMap
}
func NewScene() *Scene {
return &Scene{objects: make(ObjectMap)}
}
func (s *Scene) Objects() ObjectMap {
return s.objects
}
func (s *Scene) RegisterObject(name string, object *Object) {
s.objects[name] = object
}
func (s *Scene) updateTransforms() {
for _, obj := range s.Objects() {
if !obj.enabled {
continue
}
model := s.buildTransform(obj)
obj.model = model
obj.position = model.Mul4x1(mgl32.Vec4{0, 0, 0, 1}).Vec3()
obj.up = model.Mul4x1(mgl32.Vec4{0, 1, 0, 0}).Vec3()
obj.right = model.Mul4x1(mgl32.Vec4{1, 0, 0, 0}).Vec3()
obj.forward = obj.up.Cross(obj.right)
}
}
func (s *Scene) buildTransform(object *Object) mgl32.Mat4 {
model := mgl32.Ident4()
for t := object; t != nil; t = t.parent {
model = t.TranslationMatrix().
Mul4(t.RotationMatrix()).
Mul4(t.ScaleMatrix()).
Mul4(model)
}
return model
}