-
Notifications
You must be signed in to change notification settings - Fork 0
/
collada.go
129 lines (109 loc) · 3.07 KB
/
collada.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
117
118
119
120
121
122
123
124
125
126
127
128
129
package loader
import (
"io"
"path/filepath"
"time"
"github.com/kasworld/h4o/_examples/app"
"github.com/kasworld/h4o/_examples/usrevent"
"github.com/kasworld/h4o/_examples/util"
"github.com/kasworld/h4o/eventtype"
"github.com/kasworld/h4o/light"
"github.com/kasworld/h4o/loader/collada"
"github.com/kasworld/h4o/math32"
"github.com/kasworld/h4o/node"
"github.com/kasworld/h4o/util/helper"
)
func init() {
app.DemoMap["loader.collada"] = &LoaderCollada{}
}
type LoaderCollada struct {
prevLoaded node.NodeI
selFile *util.FileSelectButton
animTargets map[string]*collada.AnimationTarget
}
// Start is called once at the start of the demo.
func (t *LoaderCollada) Start(a *app.App) {
// Creates file selection button
t.selFile = util.NewFileSelectButton(a.DirData()+"/collada", "Select File", 400, 300)
t.selFile.SetPosition(10, 10)
t.selFile.FS.SetFileFilters("*.dae")
a.DemoPanel().Add(t.selFile)
t.selFile.Subscribe(usrevent.OnSelect, func(evname eventtype.EventType, ev interface{}) {
fpath := ev.(string)
err := t.load(a, fpath)
if err == nil {
t.selFile.Label.SetText("File: " + filepath.Base(fpath))
t.selFile.SetError("")
} else {
t.selFile.Label.SetText("Select File")
t.selFile.SetError(err.Error())
}
})
// Add directional top white light
l1 := light.NewDirectional(&math32.Color{1, 1, 1}, 1.0)
l1.SetPosition(0, 1, 0)
a.Scene().Add(l1)
// Add directional right white light
l2 := light.NewDirectional(&math32.Color{1, 1, 1}, 1.0)
l2.SetPosition(1, 0, 0)
a.Scene().Add(l2)
// Add directional front white light
l3 := light.NewDirectional(&math32.Color{1, 1, 1}, 1.0)
l3.SetPosition(0, 1, 1)
a.Scene().Add(l3)
// Sets camera position
a.Camera().SetPosition(0, 4, 10)
a.Camera().LookAt(&math32.Vector3{0, 0, 0}, &math32.Vector3{0, 1, 0})
// Create axes helper
axes := helper.NewAxes(1.5)
a.Scene().Add(axes)
// Loads default model
fpath := filepath.Join(a.DirData(), "collada/scene.dae")
t.load(a, fpath)
t.selFile.Label.SetText("File: " + filepath.Base(fpath))
}
func (t *LoaderCollada) load(a *app.App, path string) error {
// Remove previous model from the scene
if t.prevLoaded != nil {
a.Scene().Remove(t.prevLoaded)
t.prevLoaded.Dispose()
t.prevLoaded = nil
}
// Decodes collada file
dec, err := collada.Decode(path)
if err != nil && err != io.EOF {
t.selFile.SetError(err.Error())
return err
}
dec.SetDirImages(a.DirData() + "/images")
// Loads collada scene
s, err := dec.NewScene()
if err != nil {
t.selFile.SetError(err.Error())
return err
}
a.Scene().Add(s)
t.prevLoaded = s
// Checks for animations
ats, err := dec.NewAnimationTargets(s)
if err == nil {
t.animTargets = ats
for _, at := range ats {
at.SetStart(-1.0)
at.Reset()
at.SetLoop(true)
}
}
return nil
}
// Update is called every frame.
func (t *LoaderCollada) Update(a *app.App, deltaTime time.Duration) {
if t.animTargets != nil {
dt := float32(deltaTime.Seconds())
for _, at := range t.animTargets {
at.Update(dt)
}
}
}
// Cleanup is called once at the end of the demo.
func (t *LoaderCollada) Cleanup(a *app.App) {}