texture2D.go 10 KB

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