Texture.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2019 Erik De Rijcke
  2. //
  3. // This file is part of Greenfield.
  4. //
  5. // Greenfield is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // Greenfield is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with Greenfield. If not, see <https://www.gnu.org/licenses/>.
  17. /**
  18. * Represents a WebGL texture object.
  19. */
  20. export default class Texture {
  21. /**
  22. * @param {!WebGLRenderingContext}gl
  23. * @param {!number}format
  24. * @return {!Texture}
  25. */
  26. static create (gl, format) {
  27. const texture = gl.createTexture()
  28. gl.bindTexture(gl.TEXTURE_2D, texture)
  29. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
  30. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
  31. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
  32. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
  33. gl.bindTexture(gl.TEXTURE_2D, null)
  34. return new Texture(gl, format, texture)
  35. }
  36. /**
  37. * Use Texture.create(..) instead.
  38. * @param {WebGLRenderingContext}gl
  39. * @param {number}format
  40. * @param {WebGLTexture}texture
  41. * @private
  42. */
  43. constructor (gl, format, texture) {
  44. /**
  45. * @type {WebGLRenderingContext}
  46. */
  47. this.gl = gl
  48. /**
  49. * @type {WebGLTexture}
  50. */
  51. this.texture = texture
  52. /**
  53. * @type {number}
  54. */
  55. this.format = format
  56. }
  57. /**
  58. * @param {!Uint8Array|HTMLVideoElement}buffer
  59. * @param {!Rect}geo
  60. * @param {number}stride
  61. */
  62. subImage2dBuffer (buffer, x, y, width, height) {
  63. const gl = this.gl
  64. gl.bindTexture(gl.TEXTURE_2D, this.texture)
  65. gl.texSubImage2D(gl.TEXTURE_2D, 0, x, y, width, height, this.format, gl.UNSIGNED_BYTE, buffer)
  66. gl.bindTexture(gl.TEXTURE_2D, null)
  67. }
  68. /**
  69. * @param {!Uint8Array|HTMLVideoElement}buffer
  70. * @param {number}width
  71. * @param {number}height
  72. */
  73. image2dBuffer (buffer, width, height) {
  74. const gl = this.gl
  75. gl.bindTexture(gl.TEXTURE_2D, this.texture)
  76. gl.texImage2D(gl.TEXTURE_2D, 0, this.format, width, height, 0, this.format, gl.UNSIGNED_BYTE, buffer)
  77. gl.bindTexture(gl.TEXTURE_2D, null)
  78. }
  79. delete () {
  80. this.gl.deleteTexture(this.texture)
  81. this.texture = null
  82. }
  83. }