texture2D.go 9.1 KB

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