CCGrabber.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * FBO class that grabs the the contents of the screen
  24. * @class
  25. * @extends cc.Class
  26. */
  27. cc.Grabber = cc.Class.extend({
  28. _FBO:null,
  29. _oldFBO:null,
  30. _oldClearColor:null,
  31. _gl:null,
  32. /**
  33. * constructor of cc.Grabber
  34. */
  35. ctor:function () {
  36. cc._checkWebGLRenderMode();
  37. this._gl = cc._renderContext;
  38. this._oldClearColor = [0, 0, 0, 0];
  39. this._oldFBO = null;
  40. // generate FBO
  41. this._FBO = this._gl.createFramebuffer();
  42. },
  43. /**
  44. * grab
  45. * @param {cc.Texture2D} texture
  46. */
  47. grab:function (texture) {
  48. var locGL = this._gl;
  49. this._oldFBO = locGL.getParameter(locGL.FRAMEBUFFER_BINDING);
  50. // bind
  51. locGL.bindFramebuffer(locGL.FRAMEBUFFER, this._FBO);
  52. // associate texture with FBO
  53. locGL.framebufferTexture2D(locGL.FRAMEBUFFER, locGL.COLOR_ATTACHMENT0, locGL.TEXTURE_2D, texture._webTextureObj, 0);
  54. // check if it worked (probably worth doing :) )
  55. var status = locGL.checkFramebufferStatus(locGL.FRAMEBUFFER);
  56. if (status != locGL.FRAMEBUFFER_COMPLETE)
  57. cc.log("Frame Grabber: could not attach texture to frmaebuffer");
  58. locGL.bindFramebuffer(locGL.FRAMEBUFFER, this._oldFBO);
  59. },
  60. /**
  61. * should be invoked before drawing
  62. * @param {cc.Texture2D} texture
  63. */
  64. beforeRender:function (texture) {
  65. var locGL = this._gl;
  66. this._oldFBO = locGL.getParameter(locGL.FRAMEBUFFER_BINDING);
  67. locGL.bindFramebuffer(locGL.FRAMEBUFFER, this._FBO);
  68. // save clear color
  69. this._oldClearColor = locGL.getParameter(locGL.COLOR_CLEAR_VALUE);
  70. // BUG XXX: doesn't work with RGB565.
  71. locGL.clearColor(0, 0, 0, 0);
  72. // BUG #631: To fix #631, uncomment the lines with #631
  73. // Warning: But it CCGrabber won't work with 2 effects at the same time
  74. // glClearColor(0.0f,0.0f,0.0f,1.0f); // #631
  75. locGL.clear(locGL.COLOR_BUFFER_BIT | locGL.DEPTH_BUFFER_BIT);
  76. // glColorMask(true, true, true, false); // #631
  77. },
  78. /**
  79. * should be invoked after drawing
  80. * @param {cc.Texture2D} texture
  81. */
  82. afterRender:function (texture) {
  83. var locGL = this._gl;
  84. locGL.bindFramebuffer(locGL.FRAMEBUFFER, this._oldFBO);
  85. locGL.colorMask(true, true, true, true); // #631
  86. },
  87. /**
  88. * delete FBO
  89. */
  90. destroy:function(){
  91. this._gl.deleteFramebuffer(this._FBO);
  92. }
  93. });