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