texture2D.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. //uTexture gls.Uniform1i // Texture unit uniform
  35. //uFlipY gls.Uniform1i // Flip Y coordinate flag uniform
  36. //uVisible gls.Uniform1i // Texture visible uniform
  37. //uOffset gls.Uniform2f // Texture offset uniform
  38. //uRepeat gls.Uniform2f // Texture repeat uniform
  39. }
  40. const (
  41. iOffsetX = 0
  42. iOffsetY = 1
  43. iRepeatX = 3
  44. iRepeatY = 4
  45. iFlipY = 6
  46. iVisible = 7
  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
  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.uTexture.Init("MatTexture")
  62. t.uTexinfo.Init("MatTexinfo")
  63. t.uTexinfo.Set(iOffsetX, 0)
  64. t.uTexinfo.Set(iOffsetY, 0)
  65. t.uTexinfo.Set(iRepeatX, 1)
  66. t.uTexinfo.Set(iRepeatY, 1)
  67. t.uTexinfo.Set(iFlipY, 1)
  68. t.uTexinfo.Set(iVisible, 1)
  69. //t.uFlipY.Init("MatTexFlipY")
  70. //t.uVisible.Init("MatTexVisible")
  71. //t.uOffset.Init("MatTexOffset")
  72. //t.uRepeat.Init("MatTexRepeat")
  73. //t.uRepeat.Set(1, 1)
  74. //t.uOffset.Set(0, 0)
  75. //t.uVisible.Set(1)
  76. //t.uFlipY.Set(1)
  77. return t
  78. }
  79. // NewTexture2DFromImage creates and returns a pointer to a new Texture2D
  80. // using the specified image file as data.
  81. // Supported image formats are: PNG, JPEG and GIF.
  82. func NewTexture2DFromImage(imgfile string) (*Texture2D, error) {
  83. // Decodes image file into RGBA8
  84. rgba, err := DecodeImage(imgfile)
  85. if err != nil {
  86. return nil, err
  87. }
  88. t := newTexture2D()
  89. t.SetFromRGBA(rgba)
  90. return t, nil
  91. }
  92. // NewTexture2DFromRGBA creates a new texture from a pointer to an RGBA image object.
  93. func NewTexture2DFromRGBA(rgba *image.RGBA) *Texture2D {
  94. t := newTexture2D()
  95. t.SetFromRGBA(rgba)
  96. return t
  97. }
  98. // NewFromData creates a new texture from data
  99. func NewTexture2DFromData(width, height int, format int, formatType, iformat int, data interface{}) *Texture2D {
  100. t := newTexture2D()
  101. t.SetData(width, height, format, formatType, iformat, data)
  102. return t
  103. }
  104. // Incref increments the reference count for this texture
  105. // and returns a pointer to the geometry.
  106. // It should be used when this texture is shared by another
  107. // material.
  108. func (t *Texture2D) Incref() *Texture2D {
  109. t.refcount++
  110. return t
  111. }
  112. // Dispose decrements this texture reference count and
  113. // if necessary releases OpenGL resources and C memory
  114. // associated with this texture.
  115. func (t *Texture2D) Dispose() {
  116. if t.refcount > 1 {
  117. t.refcount--
  118. return
  119. }
  120. if t.gs != nil {
  121. t.gs.DeleteTextures(t.texname)
  122. t.gs = nil
  123. }
  124. }
  125. // SetImage sets a new image for this texture
  126. func (t *Texture2D) SetImage(imgfile string) error {
  127. // Decodes image file into RGBA8
  128. rgba, err := DecodeImage(imgfile)
  129. if err != nil {
  130. return err
  131. }
  132. t.SetFromRGBA(rgba)
  133. return nil
  134. }
  135. // SetFromRGBA sets the texture data from the speficied image.RGBA object
  136. func (t *Texture2D) SetFromRGBA(rgba *image.RGBA) {
  137. t.SetData(
  138. rgba.Rect.Size().X,
  139. rgba.Rect.Size().Y,
  140. gls.RGBA,
  141. gls.UNSIGNED_BYTE,
  142. gls.RGBA8,
  143. rgba.Pix,
  144. )
  145. }
  146. // SetData sets the texture data
  147. func (t *Texture2D) SetData(width, height int, format int, formatType, iformat int, data interface{}) {
  148. t.width = int32(width)
  149. t.height = int32(height)
  150. t.format = uint32(format)
  151. t.formatType = uint32(formatType)
  152. t.iformat = int32(iformat)
  153. t.data = data
  154. t.updateData = true
  155. }
  156. // SetVisible sets the visibility state of the texture
  157. func (t *Texture2D) SetVisible(state bool) {
  158. if state {
  159. t.uTexinfo.Set(iVisible, 1)
  160. } else {
  161. t.uTexinfo.Set(iVisible, 0)
  162. }
  163. }
  164. // Visible returns the current visibility state of the texture
  165. func (t *Texture2D) Visible() bool {
  166. if t.uTexinfo.Get(iVisible) == 0 {
  167. return false
  168. } else {
  169. return true
  170. }
  171. }
  172. // SetMagFilter sets the filter to be applied when the texture element
  173. // covers more than on pixel. The default value is gls.Linear.
  174. func (t *Texture2D) SetMagFilter(magFilter uint32) {
  175. t.magFilter = magFilter
  176. t.updateParams = true
  177. }
  178. // SetMinFilter sets the filter to be applied when the texture element
  179. // covers less than on pixel. The default value is gls.Linear.
  180. func (t *Texture2D) SetMinFilter(minFilter uint32) {
  181. t.minFilter = minFilter
  182. t.updateParams = true
  183. }
  184. // SetWrapS set the wrapping mode for texture S coordinate
  185. // The default value is GL_CLAMP_TO_EDGE;
  186. func (t *Texture2D) SetWrapS(wrapS uint32) {
  187. t.wrapS = wrapS
  188. t.updateParams = true
  189. }
  190. // SetWrapT set the wrapping mode for texture T coordinate
  191. // The default value is GL_CLAMP_TO_EDGE;
  192. func (t *Texture2D) SetWrapT(wrapT uint32) {
  193. t.wrapT = wrapT
  194. t.updateParams = true
  195. }
  196. // SetRepeat set the repeat factor
  197. func (t *Texture2D) SetRepeat(x, y float32) {
  198. t.uTexinfo.Set(iRepeatX, x)
  199. t.uTexinfo.Set(iRepeatY, y)
  200. //t.uRepeat.Set(x, y)
  201. }
  202. // Repeat returns the current X and Y repeat factors
  203. func (t *Texture2D) Repeat() (float32, float32) {
  204. return t.uTexinfo.Get(iRepeatX), t.uTexinfo.Get(iRepeatY)
  205. //return t.uRepeat.Get()
  206. }
  207. // SetOffset sets the offset factor
  208. func (t *Texture2D) SetOffset(x, y float32) {
  209. t.uTexinfo.Set(iOffsetX, x)
  210. t.uTexinfo.Set(iOffsetY, y)
  211. //t.uOffset.Set(x, y)
  212. }
  213. // Offset returns the current X and Y offset factors
  214. func (t *Texture2D) Offset() (float32, float32) {
  215. return t.uTexinfo.Get(iOffsetX), t.uTexinfo.Get(iOffsetY)
  216. //return t.uOffset.Get()
  217. }
  218. // SetFlipY set the state for flipping the Y coordinate
  219. func (t *Texture2D) SetFlipY(state bool) {
  220. if state {
  221. t.uTexinfo.Set(iFlipY, 1)
  222. //t.uFlipY.Set(1)
  223. } else {
  224. t.uTexinfo.Set(iFlipY, 0)
  225. //t.uFlipY.Set(0)
  226. }
  227. }
  228. // Width returns the texture width in pixels
  229. func (t *Texture2D) Width() int {
  230. return int(t.width)
  231. }
  232. // Height returns the texture height in pixels
  233. func (t *Texture2D) Height() int {
  234. return int(t.height)
  235. }
  236. // DecodeImage reads and decodes the specified image file into RGBA8.
  237. // The supported image files are PNG, JPEG and GIF.
  238. func DecodeImage(imgfile string) (*image.RGBA, error) {
  239. // Open image file
  240. file, err := os.Open(imgfile)
  241. if err != nil {
  242. return nil, err
  243. }
  244. defer file.Close()
  245. // Decodes image
  246. img, _, err := image.Decode(file)
  247. if err != nil {
  248. return nil, err
  249. }
  250. // Converts image to RGBA format
  251. rgba := image.NewRGBA(img.Bounds())
  252. if rgba.Stride != rgba.Rect.Size().X*4 {
  253. return nil, fmt.Errorf("unsupported stride")
  254. }
  255. draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src)
  256. return rgba, nil
  257. }
  258. // Called by material render setup
  259. func (t *Texture2D) RenderSetup(gs *gls.GLS, idx int) {
  260. // One time initialization
  261. if t.gs == nil {
  262. t.texname = gs.GenTexture()
  263. t.gs = gs
  264. }
  265. // Transfer texture data to OpenGL if necessary
  266. if t.updateData {
  267. // Sets the texture unit for this texture
  268. gs.ActiveTexture(uint32(gls.TEXTURE0 + idx))
  269. gs.BindTexture(gls.TEXTURE_2D, t.texname)
  270. gs.TexImage2D(
  271. gls.TEXTURE_2D, // texture type
  272. 0, // level of detail
  273. t.iformat, // internal format
  274. t.width, // width in texels
  275. t.height, // height in texels
  276. 0, // border must be 0
  277. t.format, // format of supplied texture data
  278. t.formatType, // type of external format color component
  279. t.data, // image data
  280. )
  281. // Generates mipmaps if requested
  282. if t.genMipmap {
  283. gs.GenerateMipmap(gls.TEXTURE_2D)
  284. }
  285. // No data to send
  286. t.updateData = false
  287. }
  288. // Sets the texture unit for this texture
  289. gs.ActiveTexture(uint32(gls.TEXTURE0 + idx))
  290. gs.BindTexture(gls.TEXTURE_2D, t.texname)
  291. // Sets texture parameters if needed
  292. if t.updateParams {
  293. gs.TexParameteri(gls.TEXTURE_2D, gls.TEXTURE_MAG_FILTER, int32(t.magFilter))
  294. gs.TexParameteri(gls.TEXTURE_2D, gls.TEXTURE_MIN_FILTER, int32(t.minFilter))
  295. gs.TexParameteri(gls.TEXTURE_2D, gls.TEXTURE_WRAP_S, int32(t.wrapS))
  296. gs.TexParameteri(gls.TEXTURE_2D, gls.TEXTURE_WRAP_T, int32(t.wrapT))
  297. t.updateParams = false
  298. }
  299. // Transfer uniforms
  300. t.uTexture.Set(int32(idx))
  301. t.uTexture.TransferIdx(gs, idx)
  302. t.uTexinfo.TransferIdx(gs, idx)
  303. // t.uTexture.Set(int32(idx))
  304. // t.uTexture.TransferIdx(gs, idx)
  305. // t.uFlipY.TransferIdx(gs, idx)
  306. // t.uVisible.TransferIdx(gs, idx)
  307. // t.uOffset.TransferIdx(gs, idx)
  308. // t.uRepeat.TransferIdx(gs, idx)
  309. }