Source: dependencies/pixi-gl-core/GLFramebuffer.js

dependencies/pixi-gl-core/GLFramebuffer.js

  1. var Texture = require('./GLTexture');
  2. /**
  3. * Helper class to create a webGL Framebuffer
  4. *
  5. * @class
  6. * @memberof PIXI.glCore
  7. * @param gl {WebGLRenderingContext} The current WebGL rendering context
  8. * @param width {Number} the width of the drawing area of the frame buffer
  9. * @param height {Number} the height of the drawing area of the frame buffer
  10. */
  11. var Framebuffer = function(gl, width, height)
  12. {
  13. /**
  14. * The current WebGL rendering context
  15. *
  16. * @member {WebGLRenderingContext}
  17. */
  18. this.gl = gl;
  19. /**
  20. * The frame buffer
  21. *
  22. * @member {WebGLFramebuffer}
  23. */
  24. this.framebuffer = gl.createFramebuffer();
  25. /**
  26. * The stencil buffer
  27. *
  28. * @member {WebGLRenderbuffer}
  29. */
  30. this.stencil = null;
  31. /**
  32. * The stencil buffer
  33. *
  34. * @member {PIXI.glCore.GLTexture}
  35. */
  36. this.texture = null;
  37. /**
  38. * The width of the drawing area of the buffer
  39. *
  40. * @member {Number}
  41. */
  42. this.width = width || 100;
  43. /**
  44. * The height of the drawing area of the buffer
  45. *
  46. * @member {Number}
  47. */
  48. this.height = height || 100;
  49. };
  50. /**
  51. * Adds a texture to the frame buffer
  52. * @param texture {PIXI.glCore.GLTexture}
  53. */
  54. Framebuffer.prototype.enableTexture = function(texture)
  55. {
  56. var gl = this.gl;
  57. this.texture = texture || new Texture(gl);
  58. this.texture.bind();
  59. //gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, this.width, this.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  60. this.bind();
  61. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.texture.texture, 0);
  62. };
  63. /**
  64. * Initialises the stencil buffer
  65. */
  66. Framebuffer.prototype.enableStencil = function()
  67. {
  68. if(this.stencil)return;
  69. var gl = this.gl;
  70. this.stencil = gl.createRenderbuffer();
  71. gl.bindRenderbuffer(gl.RENDERBUFFER, this.stencil);
  72. // TODO.. this is depth AND stencil?
  73. gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, this.stencil);
  74. gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_STENCIL, this.width , this.height );
  75. };
  76. /**
  77. * Erases the drawing area and fills it with a colour
  78. * @param r {Number} the red value of the clearing colour
  79. * @param g {Number} the green value of the clearing colour
  80. * @param b {Number} the blue value of the clearing colour
  81. * @param a {Number} the alpha value of the clearing colour
  82. */
  83. Framebuffer.prototype.clear = function( r, g, b, a )
  84. {
  85. this.bind();
  86. var gl = this.gl;
  87. gl.clearColor(r, g, b, a);
  88. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  89. };
  90. /**
  91. * Binds the frame buffer to the WebGL context
  92. */
  93. Framebuffer.prototype.bind = function()
  94. {
  95. var gl = this.gl;
  96. gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer );
  97. };
  98. /**
  99. * Unbinds the frame buffer to the WebGL context
  100. */
  101. Framebuffer.prototype.unbind = function()
  102. {
  103. var gl = this.gl;
  104. gl.bindFramebuffer(gl.FRAMEBUFFER, null );
  105. };
  106. /**
  107. * Resizes the drawing area of the buffer to the given width and height
  108. * @param width {Number} the new width
  109. * @param height {Number} the new height
  110. */
  111. Framebuffer.prototype.resize = function(width, height)
  112. {
  113. var gl = this.gl;
  114. this.width = width;
  115. this.height = height;
  116. if ( this.texture )
  117. {
  118. this.texture.uploadData(null, width, height);
  119. }
  120. if ( this.stencil )
  121. {
  122. // update the stencil buffer width and height
  123. gl.bindRenderbuffer(gl.RENDERBUFFER, this.stencil);
  124. gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_STENCIL, width, height);
  125. }
  126. };
  127. /**
  128. * Destroys this buffer
  129. */
  130. Framebuffer.prototype.destroy = function()
  131. {
  132. var gl = this.gl;
  133. //TODO
  134. if(this.texture)
  135. {
  136. this.texture.destroy();
  137. }
  138. gl.deleteFramebuffer(this.framebuffer);
  139. this.gl = null;
  140. this.stencil = null;
  141. this.texture = null;
  142. };
  143. /**
  144. * Creates a frame buffer with a texture containing the given data
  145. * @static
  146. * @param gl {WebGLRenderingContext} The current WebGL rendering context
  147. * @param width {Number} the width of the drawing area of the frame buffer
  148. * @param height {Number} the height of the drawing area of the frame buffer
  149. * @param data {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} an array of data
  150. */
  151. Framebuffer.createRGBA = function(gl, width, height, data)
  152. {
  153. var texture = Texture.fromData(gl, null, width, height);
  154. texture.enableNearestScaling();
  155. texture.enableWrapClamp();
  156. //now create the framebuffer object and attach the texture to it.
  157. var fbo = new Framebuffer(gl, width, height);
  158. fbo.enableTexture(texture);
  159. //fbo.enableStencil(); // get this back on soon!
  160. //fbo.enableStencil(); // get this back on soon!
  161. fbo.unbind();
  162. return fbo;
  163. };
  164. /**
  165. * Creates a frame buffer with a texture containing the given data
  166. * @static
  167. * @param gl {WebGLRenderingContext} The current WebGL rendering context
  168. * @param width {Number} the width of the drawing area of the frame buffer
  169. * @param height {Number} the height of the drawing area of the frame buffer
  170. * @param data {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} an array of data
  171. */
  172. Framebuffer.createFloat32 = function(gl, width, height, data)
  173. {
  174. // create a new texture..
  175. var texture = new Texture.fromData(gl, data, width, height);
  176. texture.enableNearestScaling();
  177. texture.enableWrapClamp();
  178. //now create the framebuffer object and attach the texture to it.
  179. var fbo = new Framebuffer(gl, width, height);
  180. fbo.enableTexture(texture);
  181. fbo.unbind();
  182. return fbo;
  183. };
  184. module.exports = Framebuffer;