library_lights.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. // LibraryLights
  13. //
  14. type LibraryLights struct {
  15. Id string
  16. Name string
  17. Asset *Asset
  18. Light []*Light
  19. }
  20. // Dump prints out information about the LibraryLights
  21. func (ll *LibraryLights) Dump(out io.Writer, indent int) {
  22. if ll == nil {
  23. return
  24. }
  25. fmt.Fprintf(out, "%sLibraryLights id:%s name:%s\n", sIndent(indent), ll.Id, ll.Name)
  26. for _, light := range ll.Light {
  27. light.Dump(out, indent+step)
  28. }
  29. }
  30. //
  31. // Light
  32. //
  33. type Light struct {
  34. Id string
  35. Name string
  36. TechniqueCommon struct {
  37. Type interface{}
  38. }
  39. }
  40. // Dump prints out information about the Light
  41. func (li *Light) Dump(out io.Writer, indent int) {
  42. fmt.Fprintf(out, "%sLights id:%s name:%s\n", sIndent(indent), li.Id, li.Name)
  43. ind := indent + step
  44. fmt.Fprintf(out, "%sTechniqueCommon\n", sIndent(ind))
  45. ind += step
  46. switch lt := li.TechniqueCommon.Type.(type) {
  47. case *Ambient:
  48. lt.Dump(out, ind)
  49. case *Directional:
  50. lt.Dump(out, ind)
  51. case *Point:
  52. lt.Dump(out, ind)
  53. case *Spot:
  54. lt.Dump(out, ind)
  55. }
  56. }
  57. //
  58. // Ambient
  59. //
  60. type Ambient struct {
  61. Color LightColor
  62. }
  63. // Dump prints out information about the Ambient
  64. func (amb *Ambient) Dump(out io.Writer, indent int) {
  65. fmt.Fprintf(out, "%sAmbient\n", sIndent(indent))
  66. ind := indent + step
  67. amb.Color.Dump(out, ind)
  68. }
  69. //
  70. // Directional
  71. //
  72. type Directional struct {
  73. Color LightColor
  74. }
  75. // Dump prints out information about the Directional
  76. func (dir *Directional) Dump(out io.Writer, indent int) {
  77. fmt.Fprintf(out, "%sDirectional\n", sIndent(indent))
  78. ind := indent + step
  79. dir.Color.Dump(out, ind)
  80. }
  81. //
  82. // Point
  83. //
  84. type Point struct {
  85. Color LightColor
  86. ConstantAttenuation *FloatValue
  87. LinearAttenuation *FloatValue
  88. QuadraticAttenuation *FloatValue
  89. }
  90. // Dump prints out information about the Point
  91. func (pl *Point) Dump(out io.Writer, indent int) {
  92. fmt.Fprintf(out, "%sPoint\n", sIndent(indent))
  93. indent += step
  94. pl.Color.Dump(out, indent)
  95. pl.ConstantAttenuation.Dump("ConstantAttenuation", out, indent)
  96. pl.LinearAttenuation.Dump("LinearAttenuation", out, indent)
  97. pl.QuadraticAttenuation.Dump("QuadraticAttenuation", out, indent)
  98. }
  99. //
  100. // Spot
  101. //
  102. type Spot struct {
  103. Color LightColor
  104. ConstantAttenuation *FloatValue
  105. LinearAttenuation *FloatValue
  106. QuadraticAttenuation *FloatValue
  107. FalloffAngle *FloatValue
  108. FalloffExponent *FloatValue
  109. }
  110. // Dump prints out information about the Spot
  111. func (sl *Spot) Dump(out io.Writer, indent int) {
  112. fmt.Fprintf(out, "%sSpot\n", sIndent(indent))
  113. indent += step
  114. sl.Color.Dump(out, indent)
  115. sl.ConstantAttenuation.Dump("ConstantAttenuation", out, indent)
  116. sl.LinearAttenuation.Dump("LinearAttenuation", out, indent)
  117. sl.QuadraticAttenuation.Dump("QuadraticAttenuation", out, indent)
  118. sl.FalloffAngle.Dump("FalloffAngle", out, indent)
  119. sl.FalloffExponent.Dump("FalloffExponent", out, indent)
  120. }
  121. //
  122. // FloatValue
  123. //
  124. type FloatValue struct {
  125. Sid string
  126. Value float32
  127. }
  128. // Dump prints out information about the FloatValue
  129. func (fv *FloatValue) Dump(name string, out io.Writer, indent int) {
  130. if fv == nil {
  131. return
  132. }
  133. fmt.Fprintf(out, "%s%s sid:%s value:%v\n", sIndent(indent), name, fv.Sid, fv.Value)
  134. }
  135. //
  136. // LightColor
  137. //
  138. type LightColor struct {
  139. Sid string
  140. Data [3]float32
  141. }
  142. // Dump prints out information about the LightColor
  143. func (lc *LightColor) Dump(out io.Writer, indent int) {
  144. fmt.Fprintf(out, "%sColor sid:%s data:%v\n", sIndent(indent), lc.Sid, lc.Data)
  145. }
  146. func (d *Decoder) decLibraryLights(start xml.StartElement, dom *Collada) error {
  147. ll := new(LibraryLights)
  148. dom.LibraryLights = ll
  149. ll.Id = findAttrib(start, "id").Value
  150. ll.Name = findAttrib(start, "name").Value
  151. for {
  152. child, _, err := d.decNextChild(start)
  153. if err != nil || child.Name.Local == "" {
  154. return err
  155. }
  156. if child.Name.Local == "light" {
  157. err := d.decLight(child, ll)
  158. if err != nil {
  159. return err
  160. }
  161. continue
  162. }
  163. }
  164. return nil
  165. }
  166. func (d *Decoder) decLight(start xml.StartElement, ll *LibraryLights) error {
  167. light := new(Light)
  168. ll.Light = append(ll.Light, light)
  169. light.Id = findAttrib(start, "id").Value
  170. light.Name = findAttrib(start, "name").Value
  171. for {
  172. child, _, err := d.decNextChild(start)
  173. if err != nil || child.Name.Local == "" {
  174. return err
  175. }
  176. if child.Name.Local == "technique_common" {
  177. err := d.decLightTechniqueCommon(child, light)
  178. if err != nil {
  179. return err
  180. }
  181. continue
  182. }
  183. }
  184. return nil
  185. }
  186. func (d *Decoder) decLightTechniqueCommon(start xml.StartElement, li *Light) error {
  187. child, _, err := d.decNextChild(start)
  188. if err != nil || child.Name.Local == "" {
  189. return err
  190. }
  191. if child.Name.Local == "ambient" {
  192. err := d.decAmbient(child, li)
  193. if err != nil {
  194. return err
  195. }
  196. }
  197. if child.Name.Local == "directional" {
  198. err := d.decDirectional(child, li)
  199. if err != nil {
  200. return err
  201. }
  202. }
  203. if child.Name.Local == "point" {
  204. err := d.decPoint(child, li)
  205. if err != nil {
  206. return err
  207. }
  208. }
  209. if child.Name.Local == "spot" {
  210. err := d.decSpot(child, li)
  211. if err != nil {
  212. return err
  213. }
  214. }
  215. return nil
  216. }
  217. func (d *Decoder) decAmbient(start xml.StartElement, li *Light) error {
  218. amb := new(Ambient)
  219. li.TechniqueCommon.Type = amb
  220. child, cdata, err := d.decNextChild(start)
  221. if err != nil || child.Name.Local == "" {
  222. return err
  223. }
  224. if child.Name.Local == "color" {
  225. err := d.decLightColor(child, cdata, &amb.Color)
  226. if err != nil {
  227. return err
  228. }
  229. }
  230. return nil
  231. }
  232. func (d *Decoder) decDirectional(start xml.StartElement, li *Light) error {
  233. dir := new(Directional)
  234. li.TechniqueCommon.Type = dir
  235. child, cdata, err := d.decNextChild(start)
  236. if err != nil || child.Name.Local == "" {
  237. return err
  238. }
  239. if child.Name.Local == "color" {
  240. err := d.decLightColor(child, cdata, &dir.Color)
  241. if err != nil {
  242. return err
  243. }
  244. }
  245. return nil
  246. }
  247. func (d *Decoder) decPoint(start xml.StartElement, li *Light) error {
  248. pl := new(Point)
  249. li.TechniqueCommon.Type = pl
  250. for {
  251. child, cdata, err := d.decNextChild(start)
  252. if err != nil || child.Name.Local == "" {
  253. return err
  254. }
  255. if child.Name.Local == "color" {
  256. err := d.decLightColor(child, cdata, &pl.Color)
  257. if err != nil {
  258. return err
  259. }
  260. continue
  261. }
  262. if child.Name.Local == "constant_attenuation" {
  263. fv, err := d.decFloatValue(child, cdata)
  264. if err != nil {
  265. return err
  266. }
  267. pl.ConstantAttenuation = fv
  268. continue
  269. }
  270. if child.Name.Local == "linear_attenuation" {
  271. fv, err := d.decFloatValue(child, cdata)
  272. if err != nil {
  273. return err
  274. }
  275. pl.LinearAttenuation = fv
  276. continue
  277. }
  278. if child.Name.Local == "quadratic_attenuation" {
  279. fv, err := d.decFloatValue(child, cdata)
  280. if err != nil {
  281. return err
  282. }
  283. pl.QuadraticAttenuation = fv
  284. continue
  285. }
  286. }
  287. return nil
  288. }
  289. func (d *Decoder) decSpot(start xml.StartElement, li *Light) error {
  290. sl := new(Spot)
  291. li.TechniqueCommon.Type = sl
  292. for {
  293. child, cdata, err := d.decNextChild(start)
  294. if err != nil || child.Name.Local == "" {
  295. return err
  296. }
  297. if child.Name.Local == "color" {
  298. err := d.decLightColor(child, cdata, &sl.Color)
  299. if err != nil {
  300. return err
  301. }
  302. continue
  303. }
  304. if child.Name.Local == "constant_attenuation" {
  305. fv, err := d.decFloatValue(child, cdata)
  306. if err != nil {
  307. return err
  308. }
  309. sl.ConstantAttenuation = fv
  310. continue
  311. }
  312. if child.Name.Local == "linear_attenuation" {
  313. fv, err := d.decFloatValue(child, cdata)
  314. if err != nil {
  315. return err
  316. }
  317. sl.LinearAttenuation = fv
  318. continue
  319. }
  320. if child.Name.Local == "quadratic_attenuation" {
  321. fv, err := d.decFloatValue(child, cdata)
  322. if err != nil {
  323. return err
  324. }
  325. sl.QuadraticAttenuation = fv
  326. continue
  327. }
  328. if child.Name.Local == "falloff_angle" {
  329. fv, err := d.decFloatValue(child, cdata)
  330. if err != nil {
  331. return err
  332. }
  333. sl.FalloffAngle = fv
  334. continue
  335. }
  336. if child.Name.Local == "falloff_exponent" {
  337. fv, err := d.decFloatValue(child, cdata)
  338. if err != nil {
  339. return err
  340. }
  341. sl.FalloffExponent = fv
  342. continue
  343. }
  344. }
  345. return nil
  346. }
  347. func (d *Decoder) decFloatValue(start xml.StartElement, cdata []byte) (*FloatValue, error) {
  348. fv := new(FloatValue)
  349. fv.Sid = findAttrib(start, "sid").Value
  350. v, err := strconv.ParseFloat(string(cdata), 32)
  351. if err != nil {
  352. return nil, err
  353. }
  354. fv.Value = float32(v)
  355. return fv, nil
  356. }
  357. func (d *Decoder) decLightColor(start xml.StartElement, cdata []byte, lc *LightColor) error {
  358. lc.Sid = findAttrib(start, "sid").Value
  359. return decFloat32Sequence(cdata, lc.Data[:])
  360. }