uniform.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. func NewUniformMatrix3f(name string) *UniformMatrix3f {
  237. uni := new(UniformMatrix3f)
  238. uni.Init(name)
  239. return uni
  240. }
  241. func (uni *UniformMatrix3f) Init(name string) {
  242. uni.name = name
  243. }
  244. func (uni *UniformMatrix3f) SetMatrix3(m *math32.Matrix3) {
  245. uni.v = *m
  246. }
  247. func (uni *UniformMatrix3f) GetMatrix3() math32.Matrix3 {
  248. return uni.v
  249. }
  250. func (uni *UniformMatrix3f) Transfer(gl *GLS) {
  251. gl.UniformMatrix3fv(uni.Location(gl), 1, false, uni.v[0:9])
  252. }
  253. func (uni *UniformMatrix3f) TransferIdx(gl *GLS, idx int) {
  254. gl.UniformMatrix3fv(uni.LocationIdx(gl, idx), 1, false, uni.v[0:9])
  255. }
  256. //
  257. // Type UniformMatrix4f is a Uniform containing sixteen float32 values
  258. // organized as 4x4 matrix
  259. //
  260. type UniformMatrix4f struct {
  261. Uniform
  262. v [16]float32
  263. }
  264. func NewUniformMatrix4f(name string) *UniformMatrix4f {
  265. uni := new(UniformMatrix4f)
  266. uni.Init(name)
  267. return uni
  268. }
  269. func (uni *UniformMatrix4f) Init(name string) {
  270. uni.name = name
  271. }
  272. func (uni *UniformMatrix4f) SetMatrix4(m *math32.Matrix4) {
  273. uni.v = *m
  274. }
  275. func (uni *UniformMatrix4f) GetMatrix4() math32.Matrix4 {
  276. return uni.v
  277. }
  278. func (uni *UniformMatrix4f) Transfer(gl *GLS) {
  279. gl.UniformMatrix4fv(uni.Location(gl), 1, false, uni.v[0:16])
  280. }
  281. func (uni *UniformMatrix4f) TransferIdx(gl *GLS, idx int) {
  282. gl.UniformMatrix4fv(uni.LocationIdx(gl, idx), 1, false, uni.v[0:16])
  283. }