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 
  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         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:function (texture) {
 45         var locGL = this._gl;
 46         this._oldFBO = locGL.getParameter(locGL.FRAMEBUFFER_BINDING);
 47         // bind
 48         locGL.bindFramebuffer(locGL.FRAMEBUFFER, this._FBO);
 49         // associate texture with FBO
 50         locGL.framebufferTexture2D(locGL.FRAMEBUFFER, locGL.COLOR_ATTACHMENT0, locGL.TEXTURE_2D, texture._webTextureObj, 0);
 51 
 52         // check if it worked (probably worth doing :) )
 53         var status = locGL.checkFramebufferStatus(locGL.FRAMEBUFFER);
 54         if (status != locGL.FRAMEBUFFER_COMPLETE)
 55             cc.log("Frame Grabber: could not attach texture to frmaebuffer");
 56         locGL.bindFramebuffer(locGL.FRAMEBUFFER, this._oldFBO);
 57     },
 58 
 59     beforeRender:function (texture) {
 60         var locGL = this._gl;
 61         this._oldFBO = locGL.getParameter(locGL.FRAMEBUFFER_BINDING);
 62         locGL.bindFramebuffer(locGL.FRAMEBUFFER, this._FBO);
 63 
 64         // save clear color
 65         this._oldClearColor = locGL.getParameter(locGL.COLOR_CLEAR_VALUE);
 66 
 67         // BUG XXX: doesn't work with RGB565.
 68         locGL.clearColor(0, 0, 0, 0);
 69 
 70         // BUG #631: To fix #631, uncomment the lines with #631
 71         // Warning: But it CCGrabber won't work with 2 effects at the same time
 72         //  glClearColor(0.0f,0.0f,0.0f,1.0f);    // #631
 73 
 74         locGL.clear(locGL.COLOR_BUFFER_BIT | locGL.DEPTH_BUFFER_BIT);
 75 
 76         //  glColorMask(true, true, true, false);    // #631
 77     },
 78 
 79     afterRender:function (texture) {
 80         var locGL = this._gl;
 81         locGL.bindFramebuffer(locGL.FRAMEBUFFER, this._oldFBO);
 82         locGL.colorMask(true, true, true, true);      // #631
 83     },
 84 
 85     destroy:function(){
 86         this._gl.deleteFramebuffer(this._FBO);
 87     }
 88 });
 89