uniform.go 14 KB

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