texture2D.go 9.0 KB

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