texture2D.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 texture
  5. import (
  6. "fmt"
  7. "image"
  8. "image/draw"
  9. _ "image/gif"
  10. _ "image/jpeg"
  11. _ "image/png"
  12. "os"
  13. "unsafe"
  14. "github.com/g3n/engine/gls"
  15. )
  16. type Texture2D struct {
  17. gs *gls.GLS // Pointer to OpenGL state
  18. refcount int // Current number of references
  19. texname uint32 // Texture handle
  20. magFilter uint32 // magnification filter
  21. minFilter uint32 // minification filter
  22. wrapS uint32 // wrap mode for s coordinate
  23. wrapT uint32 // wrap mode for t coordinate
  24. iformat int32 // internal format
  25. width int32 // texture width in pixels
  26. height int32 // texture height in pixels
  27. format uint32 // format of the pixel data
  28. formatType uint32 // type of the pixel data
  29. updateData bool // texture data needs to be sent
  30. updateParams bool // texture parameters needs to be sent
  31. genMipmap bool // generate mipmaps flag
  32. data interface{} // array with texture data
  33. uniUnit gls.Uniform // Texture unit uniform location cache
  34. uniInfo gls.Uniform // Texture info uniform location cache
  35. udata struct { // Combined uniform data in 3 vec2:
  36. offsetX float32
  37. offsetY float32
  38. repeatX float32
  39. repeatY float32
  40. flipY float32
  41. visible float32
  42. }
  43. }
  44. func newTexture2D() *Texture2D {
  45. t := new(Texture2D)
  46. t.gs = nil
  47. t.refcount = 1
  48. t.texname = 0
  49. t.magFilter = gls.LINEAR
  50. t.minFilter = gls.LINEAR
  51. t.wrapS = gls.CLAMP_TO_EDGE
  52. t.wrapT = gls.CLAMP_TO_EDGE
  53. t.updateData = false
  54. t.updateParams = true
  55. t.genMipmap = true
  56. // Initialize Uniform elements
  57. t.uniUnit.Init("MatTexture")
  58. t.uniInfo.Init("MatTexinfo")
  59. t.SetOffset(0, 0)
  60. t.SetRepeat(1, 1)
  61. t.SetFlipY(true)
  62. t.SetVisible(true)
  63. return t
  64. }
  65. // NewTexture2DFromImage creates and returns a pointer to a new Texture2D
  66. // using the specified image file as data.
  67. // Supported image formats are: PNG, JPEG and GIF.
  68. func NewTexture2DFromImage(imgfile string) (*Texture2D, error) {
  69. // Decodes image file into RGBA8
  70. rgba, err := DecodeImage(imgfile)
  71. if err != nil {
  72. return nil, err
  73. }
  74. t := newTexture2D()
  75. t.SetFromRGBA(rgba)
  76. return t, nil
  77. }
  78. // NewTexture2DFromRGBA creates a new texture from a pointer to an RGBA image object.
  79. func NewTexture2DFromRGBA(rgba *image.RGBA) *Texture2D {
  80. t := newTexture2D()
  81. t.SetFromRGBA(rgba)
  82. return t
  83. }
  84. // NewFromData creates a new texture from data
  85. func NewTexture2DFromData(width, height int, format int, formatType, iformat int, data interface{}) *Texture2D {
  86. t := newTexture2D()
  87. t.SetData(width, height, format, formatType, iformat, data)
  88. return t
  89. }
  90. // Incref increments the reference count for this texture
  91. // and returns a pointer to the geometry.
  92. // It should be used when this texture is shared by another
  93. // material.
  94. func (t *Texture2D) Incref() *Texture2D {
  95. t.refcount++
  96. return t
  97. }
  98. // Dispose decrements this texture reference count and
  99. // if necessary releases OpenGL resources and C memory
  100. // associated with this texture.
  101. func (t *Texture2D) Dispose() {
  102. if t.refcount > 1 {
  103. t.refcount--
  104. return
  105. }
  106. if t.gs != nil {
  107. t.gs.DeleteTextures(t.texname)
  108. t.gs = nil
  109. }
  110. }
  111. // SetUniformNames sets the names of the uniforms in the shader for sampler and texture info.
  112. func (t *Texture2D) SetUniformNames(sampler, info string) {
  113. t.uniUnit.Init(sampler)
  114. t.uniInfo.Init(info)
  115. }
  116. // SetImage sets a new image for this texture
  117. func (t *Texture2D) SetImage(imgfile string) error {
  118. // Decodes image file into RGBA8
  119. rgba, err := DecodeImage(imgfile)
  120. if err != nil {
  121. return err
  122. }
  123. t.SetFromRGBA(rgba)
  124. return nil
  125. }
  126. // SetFromRGBA sets the texture data from the speficied image.RGBA object
  127. func (t *Texture2D) SetFromRGBA(rgba *image.RGBA) {
  128. t.SetData(
  129. rgba.Rect.Size().X,
  130. rgba.Rect.Size().Y,
  131. gls.RGBA,
  132. gls.UNSIGNED_BYTE,
  133. gls.RGBA8,
  134. rgba.Pix,
  135. )
  136. }
  137. // SetData sets the texture data
  138. func (t *Texture2D) SetData(width, height int, format int, formatType, iformat int, data interface{}) {
  139. t.width = int32(width)
  140. t.height = int32(height)
  141. t.format = uint32(format)
  142. t.formatType = uint32(formatType)
  143. t.iformat = int32(iformat)
  144. t.data = data
  145. t.updateData = true
  146. }
  147. // SetVisible sets the visibility state of the texture
  148. func (t *Texture2D) SetVisible(state bool) {
  149. if state {
  150. t.udata.visible = 1
  151. } else {
  152. t.udata.visible = 0
  153. }
  154. }
  155. // Visible returns the current visibility state of the texture
  156. func (t *Texture2D) Visible() bool {
  157. if t.udata.visible == 0 {
  158. return false
  159. } else {
  160. return true
  161. }
  162. }
  163. // SetMagFilter sets the filter to be applied when the texture element
  164. // covers more than on pixel. The default value is gls.Linear.
  165. func (t *Texture2D) SetMagFilter(magFilter uint32) {
  166. t.magFilter = magFilter
  167. t.updateParams = true
  168. }
  169. // SetMinFilter sets the filter to be applied when the texture element
  170. // covers less than on pixel. The default value is gls.Linear.
  171. func (t *Texture2D) SetMinFilter(minFilter uint32) {
  172. t.minFilter = minFilter
  173. t.updateParams = true
  174. }
  175. // SetWrapS set the wrapping mode for texture S coordinate
  176. // The default value is GL_CLAMP_TO_EDGE;
  177. func (t *Texture2D) SetWrapS(wrapS uint32) {
  178. t.wrapS = wrapS
  179. t.updateParams = true
  180. }
  181. // SetWrapT set the wrapping mode for texture T coordinate
  182. // The default value is GL_CLAMP_TO_EDGE;
  183. func (t *Texture2D) SetWrapT(wrapT uint32) {
  184. t.wrapT = wrapT
  185. t.updateParams = true
  186. }
  187. // SetRepeat set the repeat factor
  188. func (t *Texture2D) SetRepeat(x, y float32) {
  189. t.udata.repeatX = x
  190. t.udata.repeatY = y
  191. }
  192. // Repeat returns the current X and Y repeat factors
  193. func (t *Texture2D) Repeat() (float32, float32) {
  194. return t.udata.repeatX, t.udata.repeatY
  195. }
  196. // SetOffset sets the offset factor
  197. func (t *Texture2D) SetOffset(x, y float32) {
  198. t.udata.offsetX = x
  199. t.udata.offsetY = y
  200. }
  201. // Offset returns the current X and Y offset factors
  202. func (t *Texture2D) Offset() (float32, float32) {
  203. return t.udata.offsetX, t.udata.offsetY
  204. }
  205. // SetFlipY set the state for flipping the Y coordinate
  206. func (t *Texture2D) SetFlipY(state bool) {
  207. if state {
  208. t.udata.flipY = 1
  209. } else {
  210. t.udata.flipY = 0
  211. }
  212. }
  213. // Width returns the texture width in pixels
  214. func (t *Texture2D) Width() int {
  215. return int(t.width)
  216. }
  217. // Height returns the texture height in pixels
  218. func (t *Texture2D) Height() int {
  219. return int(t.height)
  220. }
  221. // DecodeImage reads and decodes the specified image file into RGBA8.
  222. // The supported image files are PNG, JPEG and GIF.
  223. func DecodeImage(imgfile string) (*image.RGBA, error) {
  224. // Open image file
  225. file, err := os.Open(imgfile)
  226. if err != nil {
  227. return nil, err
  228. }
  229. defer file.Close()
  230. // Decodes image
  231. img, _, err := image.Decode(file)
  232. if err != nil {
  233. return nil, err
  234. }
  235. // Converts image to RGBA format
  236. rgba := image.NewRGBA(img.Bounds())
  237. if rgba.Stride != rgba.Rect.Size().X*4 {
  238. return nil, fmt.Errorf("unsupported stride")
  239. }
  240. draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src)
  241. return rgba, nil
  242. }
  243. // Called by material render setup
  244. func (t *Texture2D) RenderSetup(gs *gls.GLS, idx int) {
  245. // One time initialization
  246. if t.gs == nil {
  247. t.texname = gs.GenTexture()
  248. t.gs = gs
  249. }
  250. // Transfer texture data to OpenGL if necessary
  251. if t.updateData {
  252. // Sets the texture unit for this texture
  253. gs.ActiveTexture(uint32(gls.TEXTURE0 + idx))
  254. gs.BindTexture(gls.TEXTURE_2D, t.texname)
  255. gs.TexImage2D(
  256. gls.TEXTURE_2D, // texture type
  257. 0, // level of detail
  258. t.iformat, // internal format
  259. t.width, // width in texels
  260. t.height, // height in texels
  261. 0, // border must be 0
  262. t.format, // format of supplied texture data
  263. t.formatType, // type of external format color component
  264. t.data, // image data
  265. )
  266. // Generates mipmaps if requested
  267. if t.genMipmap {
  268. gs.GenerateMipmap(gls.TEXTURE_2D)
  269. }
  270. // No data to send
  271. t.updateData = false
  272. }
  273. // Sets the texture unit for this texture
  274. gs.ActiveTexture(uint32(gls.TEXTURE0 + idx))
  275. gs.BindTexture(gls.TEXTURE_2D, t.texname)
  276. // Sets texture parameters if needed
  277. if t.updateParams {
  278. gs.TexParameteri(gls.TEXTURE_2D, gls.TEXTURE_MAG_FILTER, int32(t.magFilter))
  279. gs.TexParameteri(gls.TEXTURE_2D, gls.TEXTURE_MIN_FILTER, int32(t.minFilter))
  280. gs.TexParameteri(gls.TEXTURE_2D, gls.TEXTURE_WRAP_S, int32(t.wrapS))
  281. gs.TexParameteri(gls.TEXTURE_2D, gls.TEXTURE_WRAP_T, int32(t.wrapT))
  282. t.updateParams = false
  283. }
  284. // Transfer texture unit uniform
  285. location := t.uniUnit.LocationIdx(gs, int32(idx))
  286. gs.Uniform1i(location, int32(idx))
  287. // Transfer texture info combined uniform
  288. const vec2count = 3
  289. location = t.uniInfo.LocationIdx(gs, vec2count*int32(idx))
  290. gs.Uniform2fvUP(location, vec2count, unsafe.Pointer(&t.udata))
  291. }