texture2D.go 8.4 KB

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