texture2D.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. "github.com/g3n/engine/gls"
  14. )
  15. // Texture2D represents a texture
  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_MIPMAP_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. // NewTexture2DFromData 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. // GetUniformNames returns the names of the uniforms in the shader for sampler and texture info.
  117. func (t *Texture2D) GetUniformNames() (sampler, info string) {
  118. return t.uniUnit.Name(), t.uniInfo.Name()
  119. }
  120. // SetImage sets a new image for this texture
  121. func (t *Texture2D) SetImage(imgfile string) error {
  122. // Decodes image file into RGBA8
  123. rgba, err := DecodeImage(imgfile)
  124. if err != nil {
  125. return err
  126. }
  127. t.SetFromRGBA(rgba)
  128. return nil
  129. }
  130. // SetFromRGBA sets the texture data from the specified image.RGBA object
  131. func (t *Texture2D) SetFromRGBA(rgba *image.RGBA) {
  132. t.SetData(
  133. rgba.Rect.Size().X,
  134. rgba.Rect.Size().Y,
  135. gls.RGBA,
  136. gls.UNSIGNED_BYTE,
  137. gls.RGBA8,
  138. rgba.Pix,
  139. )
  140. }
  141. // SetData sets the texture data
  142. func (t *Texture2D) SetData(width, height int, format int, formatType, iformat int, data interface{}) {
  143. t.width = int32(width)
  144. t.height = int32(height)
  145. t.format = uint32(format)
  146. t.formatType = uint32(formatType)
  147. t.iformat = int32(iformat)
  148. t.data = data
  149. t.updateData = true
  150. }
  151. // SetVisible sets the visibility state of the texture
  152. func (t *Texture2D) SetVisible(state bool) {
  153. if state {
  154. t.udata.visible = 1
  155. } else {
  156. t.udata.visible = 0
  157. }
  158. }
  159. // Visible returns the current visibility state of the texture
  160. func (t *Texture2D) Visible() bool {
  161. if t.udata.visible == 0 {
  162. return false
  163. }
  164. return true
  165. }
  166. // SetMagFilter sets the filter to be applied when the texture element
  167. // covers more than on pixel. The default value is gls.Linear.
  168. func (t *Texture2D) SetMagFilter(magFilter uint32) {
  169. t.magFilter = magFilter
  170. t.updateParams = true
  171. }
  172. // SetMinFilter sets the filter to be applied when the texture element
  173. // covers less than on pixel. The default value is gls.Linear.
  174. func (t *Texture2D) SetMinFilter(minFilter uint32) {
  175. t.minFilter = minFilter
  176. t.updateParams = true
  177. }
  178. // SetWrapS set the wrapping mode for texture S coordinate
  179. // The default value is GL_CLAMP_TO_EDGE;
  180. func (t *Texture2D) SetWrapS(wrapS uint32) {
  181. t.wrapS = wrapS
  182. t.updateParams = true
  183. }
  184. // SetWrapT set the wrapping mode for texture T coordinate
  185. // The default value is GL_CLAMP_TO_EDGE;
  186. func (t *Texture2D) SetWrapT(wrapT uint32) {
  187. t.wrapT = wrapT
  188. t.updateParams = true
  189. }
  190. // SetRepeat set the repeat factor
  191. func (t *Texture2D) SetRepeat(x, y float32) {
  192. t.udata.repeatX = x
  193. t.udata.repeatY = y
  194. }
  195. // Repeat returns the current X and Y repeat factors
  196. func (t *Texture2D) Repeat() (float32, float32) {
  197. return t.udata.repeatX, t.udata.repeatY
  198. }
  199. // SetOffset sets the offset factor
  200. func (t *Texture2D) SetOffset(x, y float32) {
  201. t.udata.offsetX = x
  202. t.udata.offsetY = y
  203. }
  204. // Offset returns the current X and Y offset factors
  205. func (t *Texture2D) Offset() (float32, float32) {
  206. return t.udata.offsetX, t.udata.offsetY
  207. }
  208. // SetFlipY set the state for flipping the Y coordinate
  209. func (t *Texture2D) SetFlipY(state bool) {
  210. if state {
  211. t.udata.flipY = 1
  212. } else {
  213. t.udata.flipY = 0
  214. }
  215. }
  216. // Width returns the texture width in pixels
  217. func (t *Texture2D) Width() int {
  218. return int(t.width)
  219. }
  220. // Height returns the texture height in pixels
  221. func (t *Texture2D) Height() int {
  222. return int(t.height)
  223. }
  224. // DecodeImage reads and decodes the specified image file into RGBA8.
  225. // The supported image files are PNG, JPEG and GIF.
  226. func DecodeImage(imgfile string) (*image.RGBA, error) {
  227. // Open image file
  228. file, err := os.Open(imgfile)
  229. if err != nil {
  230. return nil, err
  231. }
  232. defer file.Close()
  233. // Decodes image
  234. img, _, err := image.Decode(file)
  235. if err != nil {
  236. return nil, err
  237. }
  238. // Converts image to RGBA format
  239. rgba := image.NewRGBA(img.Bounds())
  240. if rgba.Stride != rgba.Rect.Size().X*4 {
  241. return nil, fmt.Errorf("unsupported stride")
  242. }
  243. draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src)
  244. return rgba, nil
  245. }
  246. // RenderSetup is called by the material render setup
  247. func (t *Texture2D) RenderSetup(gs *gls.GLS, slotIdx, uniIdx int) { // Could have as input - TEXTURE0 (slot) and uni location
  248. // One time initialization
  249. if t.gs == nil {
  250. t.texname = gs.GenTexture()
  251. t.gs = gs
  252. }
  253. // Sets the texture unit for this texture
  254. gs.ActiveTexture(uint32(gls.TEXTURE0 + slotIdx))
  255. gs.BindTexture(gls.TEXTURE_2D, t.texname)
  256. // Transfer texture data to OpenGL if necessary
  257. if t.updateData {
  258. gs.TexImage2D(
  259. gls.TEXTURE_2D, // texture type
  260. 0, // level of detail
  261. t.iformat, // internal format
  262. t.width, // width in texels
  263. t.height, // height in texels
  264. t.format, // format of supplied texture data
  265. t.formatType, // type of external format color component
  266. t.data, // image data
  267. )
  268. // Generates mipmaps if requested
  269. if t.genMipmap {
  270. gs.GenerateMipmap(gls.TEXTURE_2D)
  271. }
  272. // No data to send
  273. t.updateData = false
  274. }
  275. // Sets texture parameters if needed
  276. if t.updateParams {
  277. gs.TexParameteri(gls.TEXTURE_2D, gls.TEXTURE_MAG_FILTER, int32(t.magFilter))
  278. gs.TexParameteri(gls.TEXTURE_2D, gls.TEXTURE_MIN_FILTER, int32(t.minFilter))
  279. gs.TexParameteri(gls.TEXTURE_2D, gls.TEXTURE_WRAP_S, int32(t.wrapS))
  280. gs.TexParameteri(gls.TEXTURE_2D, gls.TEXTURE_WRAP_T, int32(t.wrapT))
  281. t.updateParams = false
  282. }
  283. // Transfer texture unit uniform
  284. var location int32
  285. if uniIdx == 0 {
  286. location = t.uniUnit.Location(gs)
  287. } else {
  288. location = t.uniUnit.LocationIdx(gs, int32(uniIdx))
  289. }
  290. gs.Uniform1i(location, int32(slotIdx))
  291. // Transfer texture info combined uniform
  292. const vec2count = 3
  293. location = t.uniInfo.LocationIdx(gs, vec2count*int32(uniIdx))
  294. gs.Uniform2fv(location, vec2count, &t.udata.offsetX)
  295. }