-
Notifications
You must be signed in to change notification settings - Fork 0
/
library_visual_scenes.go
491 lines (425 loc) · 10.8 KB
/
library_visual_scenes.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package collada
import (
"encoding/xml"
"fmt"
"io"
"strconv"
)
//
// LibraryVisualScenes
//
type LibraryVisualScenes struct {
Asset *Asset
VisualScene []*VisualScene
}
// Dump prints out information about the LibraryVisualScenes
func (lv *LibraryVisualScenes) Dump(out io.Writer, indent int) {
if lv == nil {
return
}
fmt.Fprintf(out, "%sLibraryVisualScenes:\n", sIndent(indent))
ind := indent + step
if lv.Asset != nil {
lv.Asset.Dump(out, ind)
}
for _, vs := range lv.VisualScene {
vs.Dump(out, ind)
}
}
//
// VisualScene contains all the nodes of a visual scene
//
type VisualScene struct {
Id string
Name string
Node []*Node // Array of nodes
}
// Dump prints out information about the VisualScene
func (vs *VisualScene) Dump(out io.Writer, indent int) {
fmt.Fprintf(out, "%sVisualScene id:%s name:%s\n", sIndent(indent), vs.Id, vs.Name)
for _, n := range vs.Node {
n.Dump(out, indent+step)
}
}
//
// Node is embedded in each node instance
//
type Node struct {
Id string
Name string
Sid string
Type string
Layer []string
TransformationElements []interface{} // Node instance type (may be nil)
Instance interface{}
Node []*Node // Array of children nodes
}
// Dump prints out information about the Node
func (n *Node) Dump(out io.Writer, indent int) {
fmt.Fprintf(out, "%sNode id:%s name:%s sid:%s type:%s layer:%v\n",
sIndent(indent), n.Id, n.Name, n.Sid, n.Type, n.Layer)
// Dump transformation elements
for _, te := range n.TransformationElements {
switch tt := te.(type) {
case *Matrix:
tt.Dump(out, indent+step)
case *Rotate:
tt.Dump(out, indent+step)
case *Scale:
tt.Dump(out, indent+step)
case *Translate:
tt.Dump(out, indent+step)
}
}
// Dump instance type
switch it := n.Instance.(type) {
case *InstanceGeometry:
it.Dump(out, indent+step)
}
// Dump node children
for _, n := range n.Node {
n.Dump(out, indent+step)
}
}
//
// Matrix
//
type Matrix struct {
Sid string
Data [16]float32
}
// Dump prints out information about the Matrix
func (m *Matrix) Dump(out io.Writer, indent int) {
fmt.Fprintf(out, "%sMatrix sid:%s data:%v\n", sIndent(indent), m.Sid, m.Data)
}
//
// Rotate
//
type Rotate struct {
Sid string
Data [4]float32
}
// Dump prints out information about the Rotate
func (r *Rotate) Dump(out io.Writer, indent int) {
fmt.Fprintf(out, "%sRotate sid:%s data:%v\n", sIndent(indent), r.Sid, r.Data)
}
//
// Translate
//
type Translate struct {
Sid string
Data [3]float32
}
// Dump prints out information about the Translate
func (t *Translate) Dump(out io.Writer, indent int) {
fmt.Fprintf(out, "%sTranslate sid:%s data:%v\n", sIndent(indent), t.Sid, t.Data)
}
//
// Scale
//
type Scale struct {
Sid string
Data [3]float32
}
// Dump prints out information about the Scale
func (s *Scale) Dump(out io.Writer, indent int) {
fmt.Fprintf(out, "%sScale sid:%s data:%v\n", sIndent(indent), s.Sid, s.Data)
}
//
// InstanceGeometry
//
type InstanceGeometry struct {
Url string // Geometry URL (required) references the ID of a Geometry
Name string // name of this element (optional)
BindMaterial *BindMaterial
}
// Dump prints out information about the InstanceGeometry
func (ig *InstanceGeometry) Dump(out io.Writer, indent int) {
fmt.Fprintf(out, "%sInstanceGeometry url:%s name:%s\n", sIndent(indent), ig.Url, ig.Name)
if ig.BindMaterial != nil {
ig.BindMaterial.Dump(out, indent+step)
}
}
//
// BindMaterial
//
type BindMaterial struct {
Params []Param
TechniqueCommon struct {
InstanceMaterial []*InstanceMaterial
}
}
// Dump prints out information about the BindMaterial
func (bm *BindMaterial) Dump(out io.Writer, indent int) {
fmt.Fprintf(out, "%sBindMaterial\n", sIndent(indent))
ind := indent + step
fmt.Fprintf(out, "%sTechniqueCommon\n", sIndent(ind))
ind += step
for _, im := range bm.TechniqueCommon.InstanceMaterial {
im.Dump(out, ind)
}
}
//
// InstanceMaterial
//
type InstanceMaterial struct {
Sid string
Name string
Target string
Symbol string
Bind []Bind
BindVertexInput []BindVertexInput
}
// Dump prints out information about the InstanceMaterial
func (im *InstanceMaterial) Dump(out io.Writer, indent int) {
fmt.Fprintf(out, "%sInstanceMaterial sid:%s name:%s target:%s symbol:%s\n",
sIndent(indent), im.Sid, im.Name, im.Target, im.Symbol)
ind := indent + step
for _, bvi := range im.BindVertexInput {
bvi.Dump(out, ind)
}
}
//
// Bind
//
type Bind struct {
Semantic string
Target string
}
//
// BindVertexInput
//
type BindVertexInput struct {
Semantic string
InputSemantic string
InputSet uint
}
// Dump prints out information about the BindVertexInput
func (bvi *BindVertexInput) Dump(out io.Writer, indent int) {
fmt.Fprintf(out, "%sBindVertexInput semantic:%s InputSemantic:%s InputSet:%d\n",
sIndent(indent), bvi.Semantic, bvi.InputSemantic, bvi.InputSet)
}
func (d *Decoder) decLibraryVisualScenes(start xml.StartElement, dom *Collada) error {
lv := new(LibraryVisualScenes)
dom.LibraryVisualScenes = lv
for {
// Get next child element
child, _, err := d.decNextChild(start)
if err != nil || child.Name.Local == "" {
return err
}
// Decodes VisualScene
if child.Name.Local == "visual_scene" {
err = d.decVisualScene(child, lv)
if err != nil {
return err
}
continue
}
}
}
func (d *Decoder) decVisualScene(vsStart xml.StartElement, lv *LibraryVisualScenes) error {
// Get attributes and appends new visual scene
vs := &VisualScene{}
vs.Id = findAttrib(vsStart, "id").Value
vs.Name = findAttrib(vsStart, "name").Value
vs.Node = make([]*Node, 0)
lv.VisualScene = append(lv.VisualScene, vs)
// Decodes visual scene children
for {
// Get next child element
child, _, err := d.decNextChild(vsStart)
if err != nil || child.Name.Local == "" {
return err
}
// Decodes Node
if child.Name.Local == "node" {
err = d.decNode(child, &vs.Node)
if err != nil {
return err
}
}
}
}
func (d *Decoder) decNode(nodeStart xml.StartElement, parent *[]*Node) error {
// Get node attributes and appends the new node to its parent
n := &Node{}
n.Id = findAttrib(nodeStart, "id").Value
n.Name = findAttrib(nodeStart, "name").Value
n.Sid = findAttrib(nodeStart, "name").Value
n.Type = findAttrib(nodeStart, "type").Value
n.Node = make([]*Node, 0)
*parent = append(*parent, n)
// Decodes node children
for {
// Get next child element
child, data, err := d.decNextChild(nodeStart)
if err != nil || child.Name.Local == "" {
return err
}
if child.Name.Local == "matrix" {
err = d.decMatrix(data, n)
if err != nil {
return err
}
continue
}
if child.Name.Local == "rotate" {
err = d.decRotate(data, n)
if err != nil {
return err
}
continue
}
if child.Name.Local == "scale" {
err = d.decScale(data, n)
if err != nil {
return err
}
continue
}
if child.Name.Local == "translate" {
err = d.decTranslate(data, n)
if err != nil {
return err
}
continue
}
if child.Name.Local == "instance_geometry" {
err = d.decInstanceGeometry(child, n)
if err != nil {
return err
}
continue
}
// Decodes child node recursively
if child.Name.Local == "node" {
err = d.decNode(child, &n.Node)
if err != nil {
return err
}
continue
}
}
}
func (d *Decoder) decMatrix(cdata []byte, n *Node) error {
mat := new(Matrix)
n.TransformationElements = append(n.TransformationElements, mat)
err := decFloat32Sequence(cdata, mat.Data[0:16])
if err != nil {
return err
}
return nil
}
func (d *Decoder) decRotate(cdata []byte, n *Node) error {
rot := new(Rotate)
n.TransformationElements = append(n.TransformationElements, rot)
err := decFloat32Sequence(cdata, rot.Data[0:4])
if err != nil {
return err
}
return nil
}
func (d *Decoder) decTranslate(cdata []byte, n *Node) error {
tr := new(Translate)
n.TransformationElements = append(n.TransformationElements, tr)
err := decFloat32Sequence(cdata, tr.Data[0:3])
if err != nil {
return err
}
return nil
}
func (d *Decoder) decScale(cdata []byte, n *Node) error {
s := new(Scale)
n.TransformationElements = append(n.TransformationElements, s)
err := decFloat32Sequence(cdata, s.Data[0:3])
if err != nil {
return err
}
return nil
}
func (d *Decoder) decInstanceGeometry(start xml.StartElement, n *Node) error {
// Creates new InstanceGeometry,sets its attributes and associates with node
ig := new(InstanceGeometry)
ig.Url = findAttrib(start, "url").Value
ig.Name = findAttrib(start, "name").Value
n.Instance = ig
// Decodes instance geometry children
for {
// Get next child element
child, _, err := d.decNextChild(start)
if err != nil || child.Name.Local == "" {
return err
}
// Decodes bind_material
if child.Name.Local == "bind_material" {
err := d.decBindMaterial(child, &ig.BindMaterial)
if err != nil {
return err
}
continue
}
}
}
func (d *Decoder) decBindMaterial(start xml.StartElement, dest **BindMaterial) error {
*dest = new(BindMaterial)
for {
// Get next child element
child, _, err := d.decNextChild(start)
if err != nil || child.Name.Local == "" {
return err
}
if child.Name.Local == "technique_common" {
err := d.decBindMaterialTechniqueCommon(child, *dest)
if err != nil {
return err
}
continue
}
}
}
func (d *Decoder) decBindMaterialTechniqueCommon(start xml.StartElement, bm *BindMaterial) error {
for {
// Get next child element
child, _, err := d.decNextChild(start)
if err != nil || child.Name.Local == "" {
return err
}
if child.Name.Local == "instance_material" {
err := d.decInstanceMaterial(child, bm)
if err != nil {
return err
}
continue
}
}
}
func (d *Decoder) decInstanceMaterial(start xml.StartElement, bm *BindMaterial) error {
im := new(InstanceMaterial)
im.Sid = findAttrib(start, "sid").Value
im.Name = findAttrib(start, "name").Value
im.Target = findAttrib(start, "target").Value
im.Symbol = findAttrib(start, "symbol").Value
bm.TechniqueCommon.InstanceMaterial = append(bm.TechniqueCommon.InstanceMaterial, im)
for {
child, _, err := d.decNextChild(start)
if err != nil || child.Name.Local == "" {
return err
}
if child.Name.Local == "bind_vertex_input" {
var bvi BindVertexInput
bvi.Semantic = findAttrib(child, "semantic").Value
bvi.InputSemantic = findAttrib(child, "input_semantic").Value
v, err := strconv.Atoi(findAttrib(child, "input_set").Value)
if err != nil {
return err
}
bvi.InputSet = uint(v)
im.BindVertexInput = append(im.BindVertexInput, bvi)
continue
}
}
}