uniform.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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 gls
  5. import (
  6. "fmt"
  7. "github.com/g3n/engine/math32"
  8. )
  9. //
  10. // Type Uniform is the type for all uniforms
  11. //
  12. type Uniform struct {
  13. name string // original name
  14. nameidx string // cached indexed name
  15. idx int // index value of indexed name
  16. }
  17. // Location returns the current location of the uniform
  18. // for the current active program
  19. func (uni *Uniform) Location(gs *GLS) int32 {
  20. loc := gs.prog.GetUniformLocation(uni.name)
  21. return loc
  22. }
  23. // Location returns the current location of the uniform
  24. // for the current active program and index
  25. func (uni *Uniform) LocationIdx(gs *GLS, idx int) int32 {
  26. // Rebuilds uniform indexed name if necessary
  27. if uni.nameidx == "" || uni.idx != idx {
  28. uni.nameidx = fmt.Sprintf("%s[%d]", uni.name, idx)
  29. uni.idx = idx
  30. }
  31. loc := gs.prog.GetUniformLocation(uni.nameidx)
  32. return loc
  33. }
  34. //
  35. // Type Uniform1i is a Uniform containing one int value
  36. //
  37. type Uniform1i struct {
  38. Uniform
  39. v0 int32
  40. }
  41. func NewUniform1i(name string) *Uniform1i {
  42. uni := new(Uniform1i)
  43. uni.name = name
  44. return uni
  45. }
  46. func (uni *Uniform1i) Init(name string) {
  47. uni.name = name
  48. }
  49. func (uni *Uniform1i) Set(v int32) {
  50. uni.v0 = v
  51. }
  52. func (uni *Uniform1i) Get() int32 {
  53. return uni.v0
  54. }
  55. func (uni *Uniform1i) Transfer(gs *GLS) {
  56. gs.Uniform1i(uni.Location(gs), uni.v0)
  57. }
  58. func (uni *Uniform1i) TransferIdx(gs *GLS, idx int) {
  59. gs.Uniform1i(uni.LocationIdx(gs, idx), uni.v0)
  60. }
  61. //
  62. // Type Uniform1f is a Uniform containing one float32 value
  63. //
  64. type Uniform1f struct {
  65. Uniform
  66. v0 float32
  67. }
  68. func NewUniform1f(name string) *Uniform1f {
  69. uni := new(Uniform1f)
  70. uni.Init(name)
  71. return uni
  72. }
  73. func (uni *Uniform1f) Init(name string) {
  74. uni.name = name
  75. }
  76. func (uni *Uniform1f) Set(v float32) {
  77. uni.v0 = v
  78. }
  79. func (uni *Uniform1f) Get() float32 {
  80. return uni.v0
  81. }
  82. func (uni *Uniform1f) Transfer(gs *GLS) {
  83. gs.Uniform1f(uni.Location(gs), uni.v0)
  84. }
  85. func (uni *Uniform1f) TransferIdx(gs *GLS, idx int) {
  86. gs.Uniform1f(uni.LocationIdx(gs, idx), uni.v0)
  87. }
  88. //
  89. // Type Uniform2f is a Uniform containing two float32 values
  90. //
  91. type Uniform2f struct {
  92. Uniform
  93. v0 float32
  94. v1 float32
  95. }
  96. func NewUniform2f(name string) *Uniform2f {
  97. uni := new(Uniform2f)
  98. uni.Init(name)
  99. return uni
  100. }
  101. func (uni *Uniform2f) Init(name string) {
  102. uni.name = name
  103. }
  104. func (uni *Uniform2f) Set(v0, v1 float32) {
  105. uni.v0 = v0
  106. uni.v1 = v1
  107. }
  108. func (uni *Uniform2f) Get() (float32, float32) {
  109. return uni.v0, uni.v1
  110. }
  111. func (uni *Uniform2f) SetVector2(v *math32.Vector2) {
  112. uni.v0 = v.X
  113. uni.v1 = v.Y
  114. }
  115. func (uni *Uniform2f) GetVector2() math32.Vector2 {
  116. return math32.Vector2{uni.v0, uni.v1}
  117. }
  118. func (uni *Uniform2f) Transfer(gs *GLS) {
  119. gs.Uniform2f(uni.Location(gs), uni.v0, uni.v1)
  120. }
  121. func (uni *Uniform2f) TransferIdx(gs *GLS, idx int) {
  122. gs.Uniform2f(uni.LocationIdx(gs, idx), uni.v0, uni.v1)
  123. }
  124. //
  125. // Type Uniform3f is a Uniform containing three float32 values
  126. //
  127. type Uniform3f struct {
  128. Uniform
  129. v0 float32
  130. v1 float32
  131. v2 float32
  132. }
  133. func NewUniform3f(name string) *Uniform3f {
  134. uni := new(Uniform3f)
  135. uni.Init(name)
  136. return uni
  137. }
  138. func (uni *Uniform3f) Init(name string) {
  139. uni.name = name
  140. }
  141. func (uni *Uniform3f) Set(v0, v1, v2 float32) {
  142. uni.v0 = v0
  143. uni.v1 = v1
  144. uni.v2 = v2
  145. }
  146. func (uni *Uniform3f) Get() (float32, float32, float32) {
  147. return uni.v0, uni.v1, uni.v2
  148. }
  149. func (uni *Uniform3f) SetVector3(v *math32.Vector3) {
  150. uni.v0 = v.X
  151. uni.v1 = v.Y
  152. uni.v2 = v.Z
  153. }
  154. func (uni *Uniform3f) GetVector3() math32.Vector3 {
  155. return math32.Vector3{uni.v0, uni.v1, uni.v2}
  156. }
  157. func (uni *Uniform3f) SetColor(color *math32.Color) {
  158. uni.v0 = color.R
  159. uni.v1 = color.G
  160. uni.v2 = color.B
  161. }
  162. func (uni *Uniform3f) GetColor() math32.Color {
  163. return math32.Color{uni.v0, uni.v1, uni.v2}
  164. }
  165. func (uni *Uniform3f) Transfer(gl *GLS) {
  166. loc := uni.Location(gl)
  167. gl.Uniform3f(loc, uni.v0, uni.v1, uni.v2)
  168. //log.Debug("Uniform3f: %s (%v) -> %v,%v,%v", uni.name, loc, uni.v0, uni.v1, uni.v2)
  169. }
  170. func (uni *Uniform3f) TransferIdx(gl *GLS, idx int) {
  171. loc := uni.LocationIdx(gl, idx)
  172. gl.Uniform3f(loc, uni.v0, uni.v1, uni.v2)
  173. //log.Debug("Uniform3f: %s -> %v,%v,%v", uni.nameidx, uni.v0, uni.v1, uni.v2)
  174. }
  175. //
  176. // Type Uniform4f is a Uniform containing four float32 values
  177. //
  178. type Uniform4f struct {
  179. Uniform
  180. v0 float32
  181. v1 float32
  182. v2 float32
  183. v3 float32
  184. }
  185. func NewUniform4f(name string) *Uniform4f {
  186. uni := new(Uniform4f)
  187. uni.Init(name)
  188. return uni
  189. }
  190. func (uni *Uniform4f) Init(name string) {
  191. uni.name = name
  192. }
  193. func (uni *Uniform4f) Set(v0, v1, v2, v3 float32) {
  194. uni.v0 = v0
  195. uni.v1 = v1
  196. uni.v2 = v2
  197. uni.v3 = v3
  198. }
  199. func (uni *Uniform4f) Get() (float32, float32, float32, float32) {
  200. return uni.v0, uni.v1, uni.v2, uni.v3
  201. }
  202. func (uni *Uniform4f) SetVector4(v *math32.Vector4) {
  203. uni.v0 = v.X
  204. uni.v1 = v.Y
  205. uni.v2 = v.Z
  206. uni.v3 = v.W
  207. }
  208. func (uni *Uniform4f) GetVector4() math32.Vector4 {
  209. return math32.Vector4{uni.v0, uni.v1, uni.v2, uni.v3}
  210. }
  211. func (uni *Uniform4f) SetColor4(c *math32.Color4) {
  212. uni.v0 = c.R
  213. uni.v1 = c.G
  214. uni.v2 = c.B
  215. uni.v3 = c.A
  216. }
  217. func (uni *Uniform4f) GetColor4() math32.Color4 {
  218. return math32.Color4{uni.v0, uni.v1, uni.v2, uni.v3}
  219. }
  220. func (uni *Uniform4f) Transfer(gl *GLS) {
  221. //log.Debug("Uniform4f.Transfer: %s %d", uni.name, uni.Location(gl))
  222. gl.Uniform4f(uni.Location(gl), uni.v0, uni.v1, uni.v2, uni.v3)
  223. }
  224. func (uni *Uniform4f) TransferIdx(gl *GLS, idx int) {
  225. gl.Uniform4f(uni.LocationIdx(gl, idx), uni.v0, uni.v1, uni.v2, uni.v3)
  226. }
  227. //
  228. // Type UniformMatrix3f is a Uniform containing nine float32 values
  229. // organized as 3x3 matrix
  230. //
  231. type UniformMatrix3f struct {
  232. Uniform
  233. v [9]float32
  234. }
  235. // NewUniformMatrix3 creates and returns a pointer to a new UniformMatrix3f
  236. // with the specified name
  237. func NewUniformMatrix3f(name string) *UniformMatrix3f {
  238. uni := new(UniformMatrix3f)
  239. uni.Init(name)
  240. return uni
  241. }
  242. // Init initializes this uniform the specified name
  243. // It is normally used when the uniform is embedded in another object.
  244. func (uni *UniformMatrix3f) Init(name string) {
  245. uni.name = name
  246. }
  247. // SetMatrix3 sets the matrix stored by the uniform
  248. func (uni *UniformMatrix3f) SetMatrix3(m *math32.Matrix3) {
  249. uni.v = *m
  250. }
  251. // GetMatrix3 gets the matrix stored by the uniform
  252. func (uni *UniformMatrix3f) GetMatrix3() math32.Matrix3 {
  253. return uni.v
  254. }
  255. // SetElement sets the value of the matrix element at the specified column and row
  256. func (uni *UniformMatrix3f) SetElement(col, row int, v float32) {
  257. uni.v[col*3+row] = v
  258. }
  259. // GetElement gets the value of the matrix element at the specified column and row
  260. func (uni *UniformMatrix3f) GetElement(col, row int, v float32) float32 {
  261. return uni.v[col*3+row]
  262. }
  263. // Set sets the value of the matrix element by its position starting
  264. // from 0 for col0, row0 to 8 from col2, row2.
  265. // This way the matrix can be considered as a vector of 9 elements
  266. func (uni *UniformMatrix3f) Set(pos int, v float32) {
  267. uni.v[pos] = v
  268. }
  269. // Get gets the value of the matrix element by its position starting
  270. // from 0 for col0, row0 to 8 from col2, row2.
  271. // This way the matrix can be considered as a vector of 9 elements
  272. func (uni *UniformMatrix3f) Get(pos int) float32 {
  273. return uni.v[pos]
  274. }
  275. // Transfer transfer the uniform matrix data to the graphics library
  276. func (uni *UniformMatrix3f) Transfer(gl *GLS) {
  277. gl.UniformMatrix3fv(uni.Location(gl), 1, false, &uni.v[0])
  278. }
  279. // TransferIdx transfer the uniform matrix data to a specified destination index
  280. // of an uniform array to the graphics library
  281. func (uni *UniformMatrix3f) TransferIdx(gl *GLS, idx int) {
  282. gl.UniformMatrix3fv(uni.LocationIdx(gl, idx), 1, false, &uni.v[0])
  283. }
  284. //
  285. // Type UniformMatrix4f is a Uniform containing sixteen float32 values
  286. // organized as 4x4 matrix
  287. //
  288. type UniformMatrix4f struct {
  289. Uniform
  290. v [16]float32
  291. }
  292. func NewUniformMatrix4f(name string) *UniformMatrix4f {
  293. uni := new(UniformMatrix4f)
  294. uni.Init(name)
  295. return uni
  296. }
  297. func (uni *UniformMatrix4f) Init(name string) {
  298. uni.name = name
  299. }
  300. func (uni *UniformMatrix4f) SetMatrix4(m *math32.Matrix4) {
  301. uni.v = *m
  302. }
  303. func (uni *UniformMatrix4f) GetMatrix4() math32.Matrix4 {
  304. return uni.v
  305. }
  306. func (uni *UniformMatrix4f) Transfer(gl *GLS) {
  307. gl.UniformMatrix4fv(uni.Location(gl), 1, false, &uni.v[0])
  308. }
  309. func (uni *UniformMatrix4f) TransferIdx(gl *GLS, idx int) {
  310. gl.UniformMatrix4fv(uni.LocationIdx(gl, idx), 1, false, &uni.v[0])
  311. }
  312. //
  313. // Type Uniform1fv is a uniform containing an array of float32 values
  314. //
  315. type Uniform1fv struct {
  316. Uniform // embedded uniform
  317. v []float32 // array of values
  318. }
  319. // NewUniform1fv creates and returns an uniform with array of float32 values
  320. // with the specified size
  321. func NewUniform1fv(name string, count int) *Uniform1fv {
  322. uni := new(Uniform1fv)
  323. uni.Init(name, count)
  324. return uni
  325. }
  326. // Init initializes an Uniform1fv object with the specified name and count of float32 values.
  327. // It is normally used when the uniform is embedded in another object.
  328. func (uni *Uniform1fv) Init(name string, count int) {
  329. uni.name = name
  330. uni.v = make([]float32, count)
  331. }
  332. // SetVector3 sets the value of the elements of uniform starting at pos
  333. // from the specified Vector3 object.
  334. func (uni *Uniform1fv) SetVector3(pos int, v *math32.Vector3) {
  335. uni.v[pos] = v.X
  336. uni.v[pos+1] = v.Y
  337. uni.v[pos+2] = v.Z
  338. }
  339. // GetVector3 gets the value of the elements of the uniform starting at
  340. // pos as a Vector3 object.
  341. func (uni *Uniform1fv) GetVector3(pos int) math32.Vector3 {
  342. return math32.Vector3{uni.v[pos], uni.v[pos+1], uni.v[pos+2]}
  343. }
  344. // SetColor sets the value of the elements of the uniform starting at pos
  345. // from the specified Color object.
  346. func (uni *Uniform1fv) SetColor(pos int, color *math32.Color) {
  347. uni.v[pos] = color.R
  348. uni.v[pos+1] = color.G
  349. uni.v[pos+2] = color.B
  350. }
  351. // GetColor gets the value of the elements of the uniform starting at pos
  352. // as a Color object.
  353. func (uni *Uniform1fv) GetColor(pos int) math32.Color {
  354. return math32.Color{uni.v[pos], uni.v[pos+1], uni.v[pos+2]}
  355. }
  356. // Set sets the value of the element at the specified position
  357. func (uni *Uniform1fv) Set(pos int, v float32) {
  358. uni.v[pos] = v
  359. }
  360. // Get gets the value of the element at the specified position
  361. func (uni *Uniform1fv) Get(pos int, v float32) float32 {
  362. return uni.v[pos]
  363. }
  364. // TransferIdx transfer a block of 'count' values of this uniform starting at 'pos'.
  365. func (uni *Uniform1fv) TransferIdx(gl *GLS, pos, count int) {
  366. gl.Uniform1fv(uni.LocationIdx(gl, pos), 1, uni.v[pos:])
  367. }
  368. //
  369. // Type Uniform3fv is a uniform containing an array of three float32 values
  370. //
  371. type Uniform3fv struct {
  372. Uniform // embedded uniform
  373. count int // number of groups of 3 float32 values
  374. v []float32 // array of values
  375. }
  376. // NewUniform3fv creates and returns an uniform array with the specified size
  377. // of 3 float values
  378. func NewUniform3fv(name string, count int) *Uniform3fv {
  379. uni := new(Uniform3fv)
  380. uni.Init(name, count)
  381. return uni
  382. }
  383. // Init initializes an Uniform3fv object with the specified name and count of 3 float32 groups.
  384. // It is normally used when the uniform is embedded in another object.
  385. func (uni *Uniform3fv) Init(name string, count int) {
  386. uni.name = name
  387. uni.count = count
  388. uni.v = make([]float32, count*3)
  389. }
  390. // Set sets the value of all elements of the specified group of 3 floats for this uniform array
  391. func (uni *Uniform3fv) Set(idx int, v0, v1, v2 float32) {
  392. pos := idx * 3
  393. uni.v[pos] = v0
  394. uni.v[pos+1] = v1
  395. uni.v[pos+2] = v2
  396. }
  397. // Get gets the value of all elements of the specified group of 3 floats for this uniform array
  398. func (uni *Uniform3fv) Get(idx int) (v0, v1, v2 float32) {
  399. pos := idx * 3
  400. return uni.v[pos], uni.v[pos+1], uni.v[pos+2]
  401. }
  402. // SetVector3 sets the value of all elements for the specified group of 3 float for this uniform array
  403. // from the specified Vector3 object.
  404. func (uni *Uniform3fv) SetVector3(idx int, v *math32.Vector3) {
  405. pos := idx * 3
  406. uni.v[pos] = v.X
  407. uni.v[pos+1] = v.Y
  408. uni.v[pos+2] = v.Z
  409. }
  410. // GetVector3 gets the value of all elements of the specified group of 3 float for this uniform array
  411. // as a Vector3 object.
  412. func (uni *Uniform3fv) GetVector3(idx int) math32.Vector3 {
  413. pos := idx * 3
  414. return math32.Vector3{uni.v[pos], uni.v[pos+1], uni.v[pos+2]}
  415. }
  416. // SetColor sets the value of all elements of the specified group of 3 floats for this uniform array
  417. // form the specified Color object.
  418. func (uni *Uniform3fv) SetColor(idx int, color *math32.Color) {
  419. pos := idx * 3
  420. uni.v[pos] = color.R
  421. uni.v[pos+1] = color.G
  422. uni.v[pos+2] = color.B
  423. }
  424. // GetColor gets the value of all elements of the specified group of 3 float for this uniform array
  425. // as a Color object.
  426. func (uni *Uniform3fv) GetColor(idx int) math32.Color {
  427. pos := idx * 3
  428. return math32.Color{uni.v[pos], uni.v[pos+1], uni.v[pos+2]}
  429. }
  430. // SetPos sets the value at the specified position in the uniform array.
  431. func (uni *Uniform3fv) SetPos(pos int, v float32) {
  432. uni.v[pos] = v
  433. }
  434. // GetPos gets the value at the specified position in the uniform array.
  435. func (uni *Uniform3fv) GetPos(pos int) float32 {
  436. return uni.v[pos]
  437. }
  438. // Transfer transfers the current values of this uniform to the current shader program
  439. func (uni *Uniform3fv) Transfer(gl *GLS) {
  440. gl.Uniform3fv(uni.Location(gl), int32(uni.count), uni.v)
  441. }
  442. // Transfer transfers the current values of this uniform to a specified
  443. // start position in the target uniform array.
  444. func (uni *Uniform3fv) TransferIdx(gl *GLS, idx int) {
  445. gl.Uniform3fv(uni.LocationIdx(gl, idx), int32(uni.count), uni.v)
  446. }
  447. //
  448. // Type Uniform4fv is a Uniform containing an array of four float32 values
  449. //
  450. type Uniform4fv struct {
  451. Uniform // embedded uniform
  452. count int // number of group of 4 float32 values
  453. v []float32 // array of values
  454. }
  455. // NewUniform4fv creates and returns an uniform array with the specified size
  456. // of 4 float values
  457. func NewUniform4fv(name string, count int) *Uniform4fv {
  458. uni := new(Uniform4fv)
  459. uni.Init(name, count)
  460. return uni
  461. }
  462. // Init initializes an Uniform4fv object with the specified name and count of 4 float32 groups.
  463. // It is normally used when the uniform is embedded in another object.
  464. func (uni *Uniform4fv) Init(name string, count int) {
  465. uni.name = name
  466. uni.count = count
  467. uni.v = make([]float32, count*4)
  468. }
  469. // Set sets the value of all elements of the specified group of 4 floats for this uniform array
  470. func (uni *Uniform4fv) Set(idx int, v0, v1, v2, v3 float32) {
  471. pos := idx * 4
  472. uni.v[pos] = v0
  473. uni.v[pos+1] = v1
  474. uni.v[pos+2] = v2
  475. uni.v[pos+3] = v3
  476. }
  477. // Get gets the value of all elements of the specified group of 4 floats for this uniform array
  478. func (uni *Uniform4fv) Get(idx int) (v0, v1, v2, v3 float32) {
  479. pos := idx * 4
  480. return uni.v[pos], uni.v[pos+1], uni.v[pos+2], uni.v[pos+3]
  481. }
  482. // SetVector4 sets the value of all elements for the specified group of 4 float for this uniform array
  483. // from the specified Vector4 object.
  484. func (uni *Uniform4fv) SetVector4(idx int, v *math32.Vector4) {
  485. pos := idx * 4
  486. uni.v[pos] = v.X
  487. uni.v[pos+1] = v.Y
  488. uni.v[pos+2] = v.Z
  489. uni.v[pos+3] = v.W
  490. }
  491. // GetVector4 gets the value of all elements of the specified group of 4 float for this uniform array
  492. // as a Vector4 object.
  493. func (uni *Uniform4fv) GetVector4(idx int) math32.Vector4 {
  494. pos := idx * 4
  495. return math32.Vector4{uni.v[pos], uni.v[pos+1], uni.v[pos+2], uni.v[pos+3]}
  496. }
  497. // SetColor4 sets the value of all elements of the specified group of 4 floats for this uniform array
  498. // form the specified Color4 object.
  499. func (uni *Uniform4fv) SetColor4(idx int, color *math32.Color4) {
  500. pos := idx * 4
  501. uni.v[pos] = color.R
  502. uni.v[pos+1] = color.G
  503. uni.v[pos+2] = color.B
  504. uni.v[pos+3] = color.A
  505. }
  506. // GetColor4 gets the value of all elements of the specified group of 4 float for this uniform array
  507. // as a Color4 object.
  508. func (uni *Uniform4fv) GetColor4(idx int) math32.Color4 {
  509. pos := idx * 4
  510. return math32.Color4{uni.v[pos], uni.v[pos+1], uni.v[pos+2], uni.v[pos+3]}
  511. }
  512. // SetPos sets the value at the specified position in the uniform array.
  513. func (uni *Uniform4fv) SetPos(pos int, v float32) {
  514. uni.v[pos] = v
  515. }
  516. // GetPos gets the value at the specified position in the uniform array.
  517. func (uni *Uniform4fv) GetPos(pos int) float32 {
  518. return uni.v[pos]
  519. }
  520. // Transfer transfers the current values of this uniform to the current shader program
  521. func (uni *Uniform4fv) Transfer(gl *GLS) {
  522. gl.Uniform4fv(uni.Location(gl), int32(uni.count), uni.v)
  523. }