library_lights.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. }
  165. func (d *Decoder) decLight(start xml.StartElement, ll *LibraryLights) error {
  166. light := new(Light)
  167. ll.Light = append(ll.Light, light)
  168. light.Id = findAttrib(start, "id").Value
  169. light.Name = findAttrib(start, "name").Value
  170. for {
  171. child, _, err := d.decNextChild(start)
  172. if err != nil || child.Name.Local == "" {
  173. return err
  174. }
  175. if child.Name.Local == "technique_common" {
  176. err := d.decLightTechniqueCommon(child, light)
  177. if err != nil {
  178. return err
  179. }
  180. continue
  181. }
  182. }
  183. }
  184. func (d *Decoder) decLightTechniqueCommon(start xml.StartElement, li *Light) error {
  185. child, _, err := d.decNextChild(start)
  186. if err != nil || child.Name.Local == "" {
  187. return err
  188. }
  189. if child.Name.Local == "ambient" {
  190. err := d.decAmbient(child, li)
  191. if err != nil {
  192. return err
  193. }
  194. }
  195. if child.Name.Local == "directional" {
  196. err := d.decDirectional(child, li)
  197. if err != nil {
  198. return err
  199. }
  200. }
  201. if child.Name.Local == "point" {
  202. err := d.decPoint(child, li)
  203. if err != nil {
  204. return err
  205. }
  206. }
  207. if child.Name.Local == "spot" {
  208. err := d.decSpot(child, li)
  209. if err != nil {
  210. return err
  211. }
  212. }
  213. return nil
  214. }
  215. func (d *Decoder) decAmbient(start xml.StartElement, li *Light) error {
  216. amb := new(Ambient)
  217. li.TechniqueCommon.Type = amb
  218. child, cdata, err := d.decNextChild(start)
  219. if err != nil || child.Name.Local == "" {
  220. return err
  221. }
  222. if child.Name.Local == "color" {
  223. err := d.decLightColor(child, cdata, &amb.Color)
  224. if err != nil {
  225. return err
  226. }
  227. }
  228. return nil
  229. }
  230. func (d *Decoder) decDirectional(start xml.StartElement, li *Light) error {
  231. dir := new(Directional)
  232. li.TechniqueCommon.Type = dir
  233. child, cdata, err := d.decNextChild(start)
  234. if err != nil || child.Name.Local == "" {
  235. return err
  236. }
  237. if child.Name.Local == "color" {
  238. err := d.decLightColor(child, cdata, &dir.Color)
  239. if err != nil {
  240. return err
  241. }
  242. }
  243. return nil
  244. }
  245. func (d *Decoder) decPoint(start xml.StartElement, li *Light) error {
  246. pl := new(Point)
  247. li.TechniqueCommon.Type = pl
  248. for {
  249. child, cdata, err := d.decNextChild(start)
  250. if err != nil || child.Name.Local == "" {
  251. return err
  252. }
  253. if child.Name.Local == "color" {
  254. err := d.decLightColor(child, cdata, &pl.Color)
  255. if err != nil {
  256. return err
  257. }
  258. continue
  259. }
  260. if child.Name.Local == "constant_attenuation" {
  261. fv, err := d.decFloatValue(child, cdata)
  262. if err != nil {
  263. return err
  264. }
  265. pl.ConstantAttenuation = fv
  266. continue
  267. }
  268. if child.Name.Local == "linear_attenuation" {
  269. fv, err := d.decFloatValue(child, cdata)
  270. if err != nil {
  271. return err
  272. }
  273. pl.LinearAttenuation = fv
  274. continue
  275. }
  276. if child.Name.Local == "quadratic_attenuation" {
  277. fv, err := d.decFloatValue(child, cdata)
  278. if err != nil {
  279. return err
  280. }
  281. pl.QuadraticAttenuation = fv
  282. continue
  283. }
  284. }
  285. }
  286. func (d *Decoder) decSpot(start xml.StartElement, li *Light) error {
  287. sl := new(Spot)
  288. li.TechniqueCommon.Type = sl
  289. for {
  290. child, cdata, err := d.decNextChild(start)
  291. if err != nil || child.Name.Local == "" {
  292. return err
  293. }
  294. if child.Name.Local == "color" {
  295. err := d.decLightColor(child, cdata, &sl.Color)
  296. if err != nil {
  297. return err
  298. }
  299. continue
  300. }
  301. if child.Name.Local == "constant_attenuation" {
  302. fv, err := d.decFloatValue(child, cdata)
  303. if err != nil {
  304. return err
  305. }
  306. sl.ConstantAttenuation = fv
  307. continue
  308. }
  309. if child.Name.Local == "linear_attenuation" {
  310. fv, err := d.decFloatValue(child, cdata)
  311. if err != nil {
  312. return err
  313. }
  314. sl.LinearAttenuation = fv
  315. continue
  316. }
  317. if child.Name.Local == "quadratic_attenuation" {
  318. fv, err := d.decFloatValue(child, cdata)
  319. if err != nil {
  320. return err
  321. }
  322. sl.QuadraticAttenuation = fv
  323. continue
  324. }
  325. if child.Name.Local == "falloff_angle" {
  326. fv, err := d.decFloatValue(child, cdata)
  327. if err != nil {
  328. return err
  329. }
  330. sl.FalloffAngle = fv
  331. continue
  332. }
  333. if child.Name.Local == "falloff_exponent" {
  334. fv, err := d.decFloatValue(child, cdata)
  335. if err != nil {
  336. return err
  337. }
  338. sl.FalloffExponent = fv
  339. continue
  340. }
  341. }
  342. }
  343. func (d *Decoder) decFloatValue(start xml.StartElement, cdata []byte) (*FloatValue, error) {
  344. fv := new(FloatValue)
  345. fv.Sid = findAttrib(start, "sid").Value
  346. v, err := strconv.ParseFloat(string(cdata), 32)
  347. if err != nil {
  348. return nil, err
  349. }
  350. fv.Value = float32(v)
  351. return fv, nil
  352. }
  353. func (d *Decoder) decLightColor(start xml.StartElement, cdata []byte, lc *LightColor) error {
  354. lc.Sid = findAttrib(start, "sid").Value
  355. return decFloat32Sequence(cdata, lc.Data[:])
  356. }