You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.6KB

  1. /*
  2. * TSDGLFrameBuffer.js
  3. * Keynote HTML Player
  4. *
  5. * Created by Tungwei Cheng
  6. * Copyright (c) 2018 Apple Inc. All rights reserved.
  7. */
  8. var TSDGLFrameBuffer = Class.create({
  9. initialize: function(gl, size, textureCount) {
  10. this.gl = gl;
  11. // framebuffer size
  12. this.size = size;
  13. // number of framebuffer-attachable texture images
  14. this.textureCount = textureCount;
  15. // current texture index
  16. this.currentTextureIndex = 0;
  17. // create framebuffer-attachable texture images
  18. this.setupFramebuffer(gl, size, textureCount);
  19. },
  20. setupFramebuffer: function(gl, size, textureCount) {
  21. // create and bind frame buffer object
  22. var buffer = this.buffer = gl.createFramebuffer();
  23. gl.bindFramebuffer(gl.FRAMEBUFFER, buffer);
  24. var textures = this.textures = [];
  25. // set up framebuffer texture(s)
  26. for (var i = 0; i < textureCount; i++) {
  27. var texture = gl.createTexture();
  28. // bind texture
  29. gl.bindTexture(gl.TEXTURE_2D, texture);
  30. gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
  31. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
  32. // setup texture parameters
  33. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  34. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  35. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  36. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  37. // specify the texture size for memory allocation
  38. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, size.width, size.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  39. // unbind texture
  40. gl.bindTexture(gl.TEXTURE_2D, null);
  41. textures.push(texture);
  42. }
  43. // bind current texture to the framebuffer
  44. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, textures[this.currentTextureIndex], 0);
  45. // unbind framebuffer
  46. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  47. },
  48. currentGLTexture: function() {
  49. var texture = this.textures[this.currentTextureIndex];
  50. return texture;
  51. },
  52. setCurrentTextureToNext: function() {
  53. var textureCount = this.textureCount;
  54. if (this.textureCount > 0) {
  55. var currentTextureIndex = this.currentTextureIndex;
  56. var nextTextureIndex = (currentTextureIndex + 1) % textureCount;
  57. this.currentTextureIndex = nextTextureIndex;
  58. // bind the framebuffer to the next texture
  59. this.bindFramebuffer();
  60. }
  61. },
  62. bindFramebuffer: function() {
  63. var gl = this.gl;
  64. //bind framebuffer
  65. gl.bindFramebuffer(gl.FRAMEBUFFER, this.buffer);
  66. // bind current texture
  67. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.textures[this.currentTextureIndex], 0);
  68. },
  69. currentGLFramebuffer: function() {
  70. var gl = this.gl;
  71. var framebuffer = gl.getParameter(gl.FRAMEBUFFER_BINDING);
  72. return framebuffer;
  73. },
  74. unbindFramebufferAndBindGLFramebuffer: function(currentGLFramebuffer) {
  75. var gl = this.gl;
  76. gl.bindFramebuffer(gl.FRAMEBUFFER, currentGLFramebuffer);
  77. }
  78. });
  79. TSDGLFrameBuffer.currentGLFramebuffer = function(gl) {
  80. // use getParameter in WebGL as there is no getIntegerv implementation
  81. var framebuffer = gl.getParameter(gl.FRAMEBUFFER_BINDING);
  82. // this will return null if drawing buffer is the display default
  83. return framebuffer;
  84. };