texture2D.go 10 KB

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