library_visual_scenes.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package collada
  5. import (
  6. "encoding/xml"
  7. "fmt"
  8. "io"
  9. "strconv"
  10. )
  11. //
  12. // Library Visual Scenes
  13. //
  14. type LibraryVisualScenes struct {
  15. Asset *Asset
  16. VisualScene []*VisualScene
  17. }
  18. func (lv *LibraryVisualScenes) Dump(out io.Writer, indent int) {
  19. if lv == nil {
  20. return
  21. }
  22. fmt.Fprintf(out, "%sLibraryVisualScenes:\n", sIndent(indent))
  23. ind := indent + step
  24. if lv.Asset != nil {
  25. lv.Asset.Dump(out, ind)
  26. }
  27. for _, vs := range lv.VisualScene {
  28. vs.Dump(out, ind)
  29. }
  30. }
  31. //
  32. // A visual scene contain all the nodes of a visual scene
  33. //
  34. type VisualScene struct {
  35. Id string
  36. Name string
  37. Node []*Node // Array of nodes
  38. }
  39. func (vs *VisualScene) Dump(out io.Writer, indent int) {
  40. fmt.Fprintf(out, "%sVisualScene id:%s name:%s\n", sIndent(indent), vs.Id, vs.Name)
  41. for _, n := range vs.Node {
  42. n.Dump(out, indent+step)
  43. }
  44. }
  45. //
  46. // Base Node is embedded in each node instance
  47. //
  48. type Node struct {
  49. Id string
  50. Name string
  51. Sid string
  52. Type string
  53. Layer []string
  54. TransformationElements []interface{} // Node instance type (may be nil)
  55. Instance interface{}
  56. Node []*Node // Array of children nodes
  57. }
  58. func (n *Node) Dump(out io.Writer, indent int) {
  59. fmt.Fprintf(out, "%sNode id:%s name:%s sid:%s type:%s layer:%v\n",
  60. sIndent(indent), n.Id, n.Name, n.Sid, n.Type, n.Layer)
  61. // Dump transformation elements
  62. for _, te := range n.TransformationElements {
  63. switch tt := te.(type) {
  64. case *Matrix:
  65. tt.Dump(out, indent+step)
  66. case *Rotate:
  67. tt.Dump(out, indent+step)
  68. case *Scale:
  69. tt.Dump(out, indent+step)
  70. case *Translate:
  71. tt.Dump(out, indent+step)
  72. }
  73. }
  74. // Dump instance type
  75. switch it := n.Instance.(type) {
  76. case *InstanceGeometry:
  77. it.Dump(out, indent+step)
  78. }
  79. // Dump node children
  80. for _, n := range n.Node {
  81. n.Dump(out, indent+step)
  82. }
  83. }
  84. //
  85. // Matrix
  86. //
  87. type Matrix struct {
  88. Sid string
  89. Data [16]float32
  90. }
  91. func (m *Matrix) Dump(out io.Writer, indent int) {
  92. fmt.Fprintf(out, "%sMatrix sid:%s data:%v\n", sIndent(indent), m.Sid, m.Data)
  93. }
  94. //
  95. // Rotate
  96. //
  97. type Rotate struct {
  98. Sid string
  99. Data [4]float32
  100. }
  101. func (r *Rotate) Dump(out io.Writer, indent int) {
  102. fmt.Fprintf(out, "%sRotate sid:%s data:%v\n", sIndent(indent), r.Sid, r.Data)
  103. }
  104. //
  105. // Translate
  106. //
  107. type Translate struct {
  108. Sid string
  109. Data [3]float32
  110. }
  111. func (t *Translate) Dump(out io.Writer, indent int) {
  112. fmt.Fprintf(out, "%sTranslate sid:%s data:%v\n", sIndent(indent), t.Sid, t.Data)
  113. }
  114. //
  115. // Scale
  116. //
  117. type Scale struct {
  118. Sid string
  119. Data [3]float32
  120. }
  121. func (s *Scale) Dump(out io.Writer, indent int) {
  122. fmt.Fprintf(out, "%sScale sid:%s data:%v\n", sIndent(indent), s.Sid, s.Data)
  123. }
  124. //
  125. // InstanceGeometry
  126. //
  127. type InstanceGeometry struct {
  128. Url string // Geometry URL (required) references the ID of a Geometry
  129. Name string // name of this element (optional)
  130. BindMaterial *BindMaterial
  131. }
  132. func (ig *InstanceGeometry) Dump(out io.Writer, indent int) {
  133. fmt.Fprintf(out, "%sInstanceGeometry url:%s name:%s\n", sIndent(indent), ig.Url, ig.Name)
  134. if ig.BindMaterial != nil {
  135. ig.BindMaterial.Dump(out, indent+step)
  136. }
  137. }
  138. //
  139. // BindMaterial
  140. //
  141. type BindMaterial struct {
  142. Params []Param
  143. TechniqueCommon struct {
  144. InstanceMaterial []*InstanceMaterial
  145. }
  146. }
  147. func (bm *BindMaterial) Dump(out io.Writer, indent int) {
  148. fmt.Fprintf(out, "%sBindMaterial\n", sIndent(indent))
  149. ind := indent + step
  150. fmt.Fprintf(out, "%sTechniqueCommon\n", sIndent(ind))
  151. ind += step
  152. for _, im := range bm.TechniqueCommon.InstanceMaterial {
  153. im.Dump(out, ind)
  154. }
  155. }
  156. //
  157. // InstanceMaterial
  158. //
  159. type InstanceMaterial struct {
  160. Sid string
  161. Name string
  162. Target string
  163. Symbol string
  164. Bind []Bind
  165. BindVertexInput []BindVertexInput
  166. }
  167. func (im *InstanceMaterial) Dump(out io.Writer, indent int) {
  168. fmt.Fprintf(out, "%sInstanceMaterial sid:%s name:%s target:%s symbol:%s\n",
  169. sIndent(indent), im.Sid, im.Name, im.Target, im.Symbol)
  170. ind := indent + step
  171. for _, bvi := range im.BindVertexInput {
  172. bvi.Dump(out, ind)
  173. }
  174. }
  175. //
  176. // Bind
  177. //
  178. type Bind struct {
  179. Semantic string
  180. Target string
  181. }
  182. //
  183. // BindVertexInput
  184. //
  185. type BindVertexInput struct {
  186. Semantic string
  187. InputSemantic string
  188. InputSet uint
  189. }
  190. func (bvi *BindVertexInput) Dump(out io.Writer, indent int) {
  191. fmt.Fprintf(out, "%sBindVertexInput semantic:%s InputSemantic:%s InputSet:%d\n",
  192. sIndent(indent), bvi.Semantic, bvi.InputSemantic, bvi.InputSet)
  193. }
  194. func (d *Decoder) decLibraryVisualScenes(start xml.StartElement, dom *Collada) error {
  195. lv := new(LibraryVisualScenes)
  196. dom.LibraryVisualScenes = lv
  197. for {
  198. // Get next child element
  199. child, _, err := d.decNextChild(start)
  200. if err != nil || child.Name.Local == "" {
  201. return err
  202. }
  203. // Decodes VisualScene
  204. if child.Name.Local == "visual_scene" {
  205. err = d.decVisualScene(child, lv)
  206. if err != nil {
  207. return err
  208. }
  209. continue
  210. }
  211. }
  212. return nil
  213. }
  214. func (d *Decoder) decVisualScene(vsStart xml.StartElement, lv *LibraryVisualScenes) error {
  215. // Get attributes and appends new visual scene
  216. vs := &VisualScene{}
  217. vs.Id = findAttrib(vsStart, "id").Value
  218. vs.Name = findAttrib(vsStart, "name").Value
  219. vs.Node = make([]*Node, 0)
  220. lv.VisualScene = append(lv.VisualScene, vs)
  221. // Decodes visual scene children
  222. for {
  223. // Get next child element
  224. child, _, err := d.decNextChild(vsStart)
  225. if err != nil || child.Name.Local == "" {
  226. return err
  227. }
  228. // Decodes Node
  229. if child.Name.Local == "node" {
  230. err = d.decNode(child, &vs.Node)
  231. if err != nil {
  232. return err
  233. }
  234. }
  235. }
  236. return nil
  237. }
  238. func (d *Decoder) decNode(nodeStart xml.StartElement, parent *[]*Node) error {
  239. // Get node attributes and appends the new node to its parent
  240. n := &Node{}
  241. n.Id = findAttrib(nodeStart, "id").Value
  242. n.Name = findAttrib(nodeStart, "name").Value
  243. n.Sid = findAttrib(nodeStart, "name").Value
  244. n.Type = findAttrib(nodeStart, "type").Value
  245. n.Node = make([]*Node, 0)
  246. *parent = append(*parent, n)
  247. // Decodes node children
  248. for {
  249. // Get next child element
  250. child, data, err := d.decNextChild(nodeStart)
  251. if err != nil || child.Name.Local == "" {
  252. return err
  253. }
  254. if child.Name.Local == "matrix" {
  255. err = d.decMatrix(data, n)
  256. if err != nil {
  257. return err
  258. }
  259. continue
  260. }
  261. if child.Name.Local == "rotate" {
  262. err = d.decRotate(data, n)
  263. if err != nil {
  264. return err
  265. }
  266. continue
  267. }
  268. if child.Name.Local == "scale" {
  269. err = d.decScale(data, n)
  270. if err != nil {
  271. return err
  272. }
  273. continue
  274. }
  275. if child.Name.Local == "translate" {
  276. err = d.decTranslate(data, n)
  277. if err != nil {
  278. return err
  279. }
  280. continue
  281. }
  282. if child.Name.Local == "instance_geometry" {
  283. err = d.decInstanceGeometry(child, n)
  284. if err != nil {
  285. return err
  286. }
  287. continue
  288. }
  289. // Decodes child node recursively
  290. if child.Name.Local == "node" {
  291. err = d.decNode(child, &n.Node)
  292. if err != nil {
  293. return err
  294. }
  295. continue
  296. }
  297. }
  298. return nil
  299. }
  300. func (d *Decoder) decMatrix(cdata []byte, n *Node) error {
  301. mat := new(Matrix)
  302. n.TransformationElements = append(n.TransformationElements, mat)
  303. err := decFloat32Sequence(cdata, mat.Data[0:16])
  304. if err != nil {
  305. return err
  306. }
  307. return nil
  308. }
  309. func (d *Decoder) decRotate(cdata []byte, n *Node) error {
  310. rot := new(Rotate)
  311. n.TransformationElements = append(n.TransformationElements, rot)
  312. err := decFloat32Sequence(cdata, rot.Data[0:4])
  313. if err != nil {
  314. return err
  315. }
  316. return nil
  317. }
  318. func (d *Decoder) decTranslate(cdata []byte, n *Node) error {
  319. tr := new(Translate)
  320. n.TransformationElements = append(n.TransformationElements, tr)
  321. err := decFloat32Sequence(cdata, tr.Data[0:3])
  322. if err != nil {
  323. return err
  324. }
  325. return nil
  326. }
  327. func (d *Decoder) decScale(cdata []byte, n *Node) error {
  328. s := new(Scale)
  329. n.TransformationElements = append(n.TransformationElements, s)
  330. err := decFloat32Sequence(cdata, s.Data[0:3])
  331. if err != nil {
  332. return err
  333. }
  334. return nil
  335. }
  336. func (d *Decoder) decInstanceGeometry(start xml.StartElement, n *Node) error {
  337. // Creates new InstanceGeometry,sets its attributes and associates with node
  338. ig := new(InstanceGeometry)
  339. ig.Url = findAttrib(start, "url").Value
  340. ig.Name = findAttrib(start, "name").Value
  341. n.Instance = ig
  342. // Decodes instance geometry children
  343. for {
  344. // Get next child element
  345. child, _, err := d.decNextChild(start)
  346. if err != nil || child.Name.Local == "" {
  347. return err
  348. }
  349. // Decodes bind_material
  350. if child.Name.Local == "bind_material" {
  351. err := d.decBindMaterial(child, &ig.BindMaterial)
  352. if err != nil {
  353. return err
  354. }
  355. continue
  356. }
  357. }
  358. return nil
  359. }
  360. func (d *Decoder) decBindMaterial(start xml.StartElement, dest **BindMaterial) error {
  361. *dest = new(BindMaterial)
  362. for {
  363. // Get next child element
  364. child, _, err := d.decNextChild(start)
  365. if err != nil || child.Name.Local == "" {
  366. return err
  367. }
  368. if child.Name.Local == "technique_common" {
  369. err := d.decBindMaterialTechniqueCommon(child, *dest)
  370. if err != nil {
  371. return err
  372. }
  373. continue
  374. }
  375. }
  376. return nil
  377. }
  378. func (d *Decoder) decBindMaterialTechniqueCommon(start xml.StartElement, bm *BindMaterial) error {
  379. for {
  380. // Get next child element
  381. child, _, err := d.decNextChild(start)
  382. if err != nil || child.Name.Local == "" {
  383. return err
  384. }
  385. if child.Name.Local == "instance_material" {
  386. err := d.decInstanceMaterial(child, bm)
  387. if err != nil {
  388. return err
  389. }
  390. continue
  391. }
  392. }
  393. return nil
  394. }
  395. func (d *Decoder) decInstanceMaterial(start xml.StartElement, bm *BindMaterial) error {
  396. im := new(InstanceMaterial)
  397. im.Sid = findAttrib(start, "sid").Value
  398. im.Name = findAttrib(start, "name").Value
  399. im.Target = findAttrib(start, "target").Value
  400. im.Symbol = findAttrib(start, "symbol").Value
  401. bm.TechniqueCommon.InstanceMaterial = append(bm.TechniqueCommon.InstanceMaterial, im)
  402. for {
  403. child, _, err := d.decNextChild(start)
  404. if err != nil || child.Name.Local == "" {
  405. return err
  406. }
  407. if child.Name.Local == "bind_vertex_input" {
  408. var bvi BindVertexInput
  409. bvi.Semantic = findAttrib(child, "semantic").Value
  410. bvi.InputSemantic = findAttrib(child, "input_semantic").Value
  411. v, err := strconv.Atoi(findAttrib(child, "input_set").Value)
  412. if err != nil {
  413. return err
  414. }
  415. bvi.InputSet = uint(v)
  416. im.BindVertexInput = append(im.BindVertexInput, bvi)
  417. continue
  418. }
  419. }
  420. return nil
  421. }