texture2D.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. "github.com/g3n/engine/gls"
  8. "image"
  9. "image/draw"
  10. _ "image/gif"
  11. _ "image/jpeg"
  12. _ "image/png"
  13. "os"
  14. )
  15. type Texture2D struct {
  16. gs *gls.GLS // Pointer to OpenGL state
  17. refcount int // Current number of references
  18. texname uint32 // Texture handle
  19. magFilter uint32 // magnification filter
  20. minFilter uint32 // minification filter
  21. wrapS uint32 // wrap mode for s coordinate
  22. wrapT uint32 // wrap mode for t coordinate
  23. iformat int32 // internal format
  24. width int32 // texture width in pixels
  25. height int32 // texture height in pixels
  26. format uint32 // format of the pixel data
  27. formatType uint32 // type of the pixel data
  28. updateData bool // texture data needs to be sent
  29. updateParams bool // texture parameters needs to be sent
  30. genMipmap bool // generate mipmaps flag
  31. data interface{} // array with texture data
  32. uTexture gls.Uniform1i // Texture unit uniform
  33. uTexinfo gls.UniformMatrix3f // uniform 3x3 array with texture info
  34. }
  35. const (
  36. iOffsetX = 0
  37. iOffsetY = 1
  38. iRepeatX = 3
  39. iRepeatY = 4
  40. iFlipY = 6
  41. iVisible = 7
  42. )
  43. func newTexture2D() *Texture2D {
  44. t := new(Texture2D)
  45. t.gs = nil
  46. t.refcount = 1
  47. t.texname = 0
  48. t.magFilter = gls.LINEAR
  49. t.minFilter = gls.LINEAR
  50. t.wrapS = gls.CLAMP_TO_EDGE
  51. t.wrapT = gls.CLAMP_TO_EDGE
  52. t.updateData = false
  53. t.updateParams = true
  54. t.genMipmap = true
  55. // Initialize Uniform elements
  56. t.uTexture.Init("MatTexture")
  57. t.uTexinfo.Init("MatTexinfo")
  58. t.uTexinfo.Set(iOffsetX, 0)
  59. t.uTexinfo.Set(iOffsetY, 0)
  60. t.uTexinfo.Set(iRepeatX, 1)
  61. t.uTexinfo.Set(iRepeatY, 1)
  62. t.uTexinfo.Set(iFlipY, 1)
  63. t.uTexinfo.Set(iVisible, 1)
  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. // NewFromData 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. // SetImage sets a new image for this texture
  113. func (t *Texture2D) SetImage(imgfile string) error {
  114. // Decodes image file into RGBA8
  115. rgba, err := DecodeImage(imgfile)
  116. if err != nil {
  117. return err
  118. }
  119. t.SetFromRGBA(rgba)
  120. return nil
  121. }
  122. // SetFromRGBA sets the texture data from the speficied image.RGBA object
  123. func (t *Texture2D) SetFromRGBA(rgba *image.RGBA) {
  124. t.SetData(
  125. rgba.Rect.Size().X,
  126. rgba.Rect.Size().Y,
  127. gls.RGBA,
  128. gls.UNSIGNED_BYTE,
  129. gls.RGBA8,
  130. rgba.Pix,
  131. )
  132. }
  133. // SetData sets the texture data
  134. func (t *Texture2D) SetData(width, height int, format int, formatType, iformat int, data interface{}) {
  135. t.width = int32(width)
  136. t.height = int32(height)
  137. t.format = uint32(format)
  138. t.formatType = uint32(formatType)
  139. t.iformat = int32(iformat)
  140. t.data = data
  141. t.updateData = true
  142. }
  143. // SetVisible sets the visibility state of the texture
  144. func (t *Texture2D) SetVisible(state bool) {
  145. if state {
  146. t.uTexinfo.Set(iVisible, 1)
  147. } else {
  148. t.uTexinfo.Set(iVisible, 0)
  149. }
  150. }
  151. // Visible returns the current visibility state of the texture
  152. func (t *Texture2D) Visible() bool {
  153. if t.uTexinfo.Get(iVisible) == 0 {
  154. return false
  155. } else {
  156. return true
  157. }
  158. }
  159. // SetMagFilter sets the filter to be applied when the texture element
  160. // covers more than on pixel. The default value is gls.Linear.
  161. func (t *Texture2D) SetMagFilter(magFilter uint32) {
  162. t.magFilter = magFilter
  163. t.updateParams = true
  164. }
  165. // SetMinFilter sets the filter to be applied when the texture element
  166. // covers less than on pixel. The default value is gls.Linear.
  167. func (t *Texture2D) SetMinFilter(minFilter uint32) {
  168. t.minFilter = minFilter
  169. t.updateParams = true
  170. }
  171. // SetWrapS set the wrapping mode for texture S coordinate
  172. // The default value is GL_CLAMP_TO_EDGE;
  173. func (t *Texture2D) SetWrapS(wrapS uint32) {
  174. t.wrapS = wrapS
  175. t.updateParams = true
  176. }
  177. // SetWrapT set the wrapping mode for texture T coordinate
  178. // The default value is GL_CLAMP_TO_EDGE;
  179. func (t *Texture2D) SetWrapT(wrapT uint32) {
  180. t.wrapT = wrapT
  181. t.updateParams = true
  182. }
  183. // SetRepeat set the repeat factor
  184. func (t *Texture2D) SetRepeat(x, y float32) {
  185. t.uTexinfo.Set(iRepeatX, x)
  186. t.uTexinfo.Set(iRepeatY, y)
  187. }
  188. // Repeat returns the current X and Y repeat factors
  189. func (t *Texture2D) Repeat() (float32, float32) {
  190. return t.uTexinfo.Get(iRepeatX), t.uTexinfo.Get(iRepeatY)
  191. }
  192. // SetOffset sets the offset factor
  193. func (t *Texture2D) SetOffset(x, y float32) {
  194. t.uTexinfo.Set(iOffsetX, x)
  195. t.uTexinfo.Set(iOffsetY, y)
  196. }
  197. // Offset returns the current X and Y offset factors
  198. func (t *Texture2D) Offset() (float32, float32) {
  199. return t.uTexinfo.Get(iOffsetX), t.uTexinfo.Get(iOffsetY)
  200. }
  201. // SetFlipY set the state for flipping the Y coordinate
  202. func (t *Texture2D) SetFlipY(state bool) {
  203. if state {
  204. t.uTexinfo.Set(iFlipY, 1)
  205. } else {
  206. t.uTexinfo.Set(iFlipY, 0)
  207. }
  208. }
  209. // Width returns the texture width in pixels
  210. func (t *Texture2D) Width() int {
  211. return int(t.width)
  212. }
  213. // Height returns the texture height in pixels
  214. func (t *Texture2D) Height() int {
  215. return int(t.height)
  216. }
  217. // DecodeImage reads and decodes the specified image file into RGBA8.
  218. // The supported image files are PNG, JPEG and GIF.
  219. func DecodeImage(imgfile string) (*image.RGBA, error) {
  220. // Open image file
  221. file, err := os.Open(imgfile)
  222. if err != nil {
  223. return nil, err
  224. }
  225. defer file.Close()
  226. // Decodes image
  227. img, _, err := image.Decode(file)
  228. if err != nil {
  229. return nil, err
  230. }
  231. // Converts image to RGBA format
  232. rgba := image.NewRGBA(img.Bounds())
  233. if rgba.Stride != rgba.Rect.Size().X*4 {
  234. return nil, fmt.Errorf("unsupported stride")
  235. }
  236. draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src)
  237. return rgba, nil
  238. }
  239. // Called by material render setup
  240. func (t *Texture2D) RenderSetup(gs *gls.GLS, idx int) {
  241. // One time initialization
  242. if t.gs == nil {
  243. t.texname = gs.GenTexture()
  244. t.gs = gs
  245. }
  246. // Transfer texture data to OpenGL if necessary
  247. if t.updateData {
  248. // Sets the texture unit for this texture
  249. gs.ActiveTexture(uint32(gls.TEXTURE0 + idx))
  250. gs.BindTexture(gls.TEXTURE_2D, t.texname)
  251. gs.TexImage2D(
  252. gls.TEXTURE_2D, // texture type
  253. 0, // level of detail
  254. t.iformat, // internal format
  255. t.width, // width in texels
  256. t.height, // height in texels
  257. 0, // border must be 0
  258. t.format, // format of supplied texture data
  259. t.formatType, // type of external format color component
  260. t.data, // image data
  261. )
  262. // Generates mipmaps if requested
  263. if t.genMipmap {
  264. gs.GenerateMipmap(gls.TEXTURE_2D)
  265. }
  266. // No data to send
  267. t.updateData = false
  268. }
  269. // Sets the texture unit for this texture
  270. gs.ActiveTexture(uint32(gls.TEXTURE0 + idx))
  271. gs.BindTexture(gls.TEXTURE_2D, t.texname)
  272. // Sets texture parameters if needed
  273. if t.updateParams {
  274. gs.TexParameteri(gls.TEXTURE_2D, gls.TEXTURE_MAG_FILTER, int32(t.magFilter))
  275. gs.TexParameteri(gls.TEXTURE_2D, gls.TEXTURE_MIN_FILTER, int32(t.minFilter))
  276. gs.TexParameteri(gls.TEXTURE_2D, gls.TEXTURE_WRAP_S, int32(t.wrapS))
  277. gs.TexParameteri(gls.TEXTURE_2D, gls.TEXTURE_WRAP_T, int32(t.wrapT))
  278. t.updateParams = false
  279. }
  280. // Transfer uniforms
  281. t.uTexture.Set(int32(idx))
  282. t.uTexture.TransferIdx(gs, idx)
  283. t.uTexinfo.TransferIdx(gs, idx)
  284. }