CCGrabber.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga 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. /** FBO class that grabs the the contents of the screen */
  23. cc.Grabber = cc.Class.extend({
  24. _FBO:null,
  25. _oldFBO:null,
  26. _oldClearColor:null,
  27. _gl:null,
  28. ctor:function () {
  29. this._gl = cc.renderContext;
  30. this._oldClearColor = [0, 0, 0, 0];
  31. this._oldFBO = null;
  32. // generate FBO
  33. this._FBO = this._gl.createFramebuffer();
  34. },
  35. grab:function (texture) {
  36. var locGL = this._gl;
  37. this._oldFBO = locGL.getParameter(locGL.FRAMEBUFFER_BINDING);
  38. // bind
  39. locGL.bindFramebuffer(locGL.FRAMEBUFFER, this._FBO);
  40. // associate texture with FBO
  41. locGL.framebufferTexture2D(locGL.FRAMEBUFFER, locGL.COLOR_ATTACHMENT0, locGL.TEXTURE_2D, texture._webTextureObj, 0);
  42. // check if it worked (probably worth doing :) )
  43. var status = locGL.checkFramebufferStatus(locGL.FRAMEBUFFER);
  44. if (status != locGL.FRAMEBUFFER_COMPLETE)
  45. cc.log("Frame Grabber: could not attach texture to frmaebuffer");
  46. locGL.bindFramebuffer(locGL.FRAMEBUFFER, this._oldFBO);
  47. },
  48. beforeRender:function (texture) {
  49. var locGL = this._gl;
  50. this._oldFBO = locGL.getParameter(locGL.FRAMEBUFFER_BINDING);
  51. locGL.bindFramebuffer(locGL.FRAMEBUFFER, this._FBO);
  52. // save clear color
  53. this._oldClearColor = locGL.getParameter(locGL.COLOR_CLEAR_VALUE);
  54. // BUG XXX: doesn't work with RGB565.
  55. locGL.clearColor(0, 0, 0, 0);
  56. // BUG #631: To fix #631, uncomment the lines with #631
  57. // Warning: But it CCGrabber won't work with 2 effects at the same time
  58. // glClearColor(0.0f,0.0f,0.0f,1.0f); // #631
  59. locGL.clear(locGL.COLOR_BUFFER_BIT | locGL.DEPTH_BUFFER_BIT);
  60. // glColorMask(true, true, true, false); // #631
  61. },
  62. afterRender:function (texture) {
  63. var locGL = this._gl;
  64. locGL.bindFramebuffer(locGL.FRAMEBUFFER, this._oldFBO);
  65. locGL.colorMask(true, true, true, true); // #631
  66. },
  67. destroy:function(){
  68. this._gl.deleteFramebuffer(this._FBO);
  69. }
  70. });