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 /**
 28  * FBO class that grabs the the contents of the screen
 29  * @class
 30  * @extends cc.Class
 31  */
 32 cc.Grabber = cc.Class.extend({
 33     _FBO:null,
 34     _oldFBO:null,
 35     _oldClearColor:null,
 36 
 37     _gl:null,
 38 
 39     /**
 40      * constructor of cc.Grabber
 41      */
 42     ctor:function () {
 43         cc.sys._checkWebGLRenderMode();
 44         this._gl = cc._renderContext;
 45         this._oldClearColor = [0, 0, 0, 0];
 46         this._oldFBO = null;
 47         // generate FBO
 48         this._FBO = this._gl.createFramebuffer();
 49     },
 50 
 51     /**
 52      * grab
 53      * @param {cc.Texture2D} texture
 54      */
 55     grab:function (texture) {
 56         var locGL = this._gl;
 57         this._oldFBO = locGL.getParameter(locGL.FRAMEBUFFER_BINDING);
 58         // bind
 59         locGL.bindFramebuffer(locGL.FRAMEBUFFER, this._FBO);
 60         // associate texture with FBO
 61         locGL.framebufferTexture2D(locGL.FRAMEBUFFER, locGL.COLOR_ATTACHMENT0, locGL.TEXTURE_2D, texture._webTextureObj, 0);
 62 
 63         // check if it worked (probably worth doing :) )
 64         var status = locGL.checkFramebufferStatus(locGL.FRAMEBUFFER);
 65         if (status !== locGL.FRAMEBUFFER_COMPLETE)
 66             cc.log("Frame Grabber: could not attach texture to frmaebuffer");
 67         locGL.bindFramebuffer(locGL.FRAMEBUFFER, this._oldFBO);
 68     },
 69 
 70     /**
 71      * should be invoked before drawing
 72      * @param {cc.Texture2D} texture
 73      */
 74     beforeRender:function (texture) {
 75         var locGL = this._gl;
 76         this._oldFBO = locGL.getParameter(locGL.FRAMEBUFFER_BINDING);
 77         locGL.bindFramebuffer(locGL.FRAMEBUFFER, this._FBO);
 78 
 79         // save clear color
 80         this._oldClearColor = locGL.getParameter(locGL.COLOR_CLEAR_VALUE);
 81 
 82         // BUG XXX: doesn't work with RGB565.
 83         locGL.clearColor(0, 0, 0, 0);
 84 
 85         // BUG #631: To fix #631, uncomment the lines with #631
 86         // Warning: But it CCGrabber won't work with 2 effects at the same time
 87         //  glClearColor(0.0f,0.0f,0.0f,1.0f);    // #631
 88 
 89         locGL.clear(locGL.COLOR_BUFFER_BIT | locGL.DEPTH_BUFFER_BIT);
 90 
 91         //  glColorMask(true, true, true, false);    // #631
 92     },
 93 
 94     /**
 95      * should be invoked after drawing
 96      * @param {cc.Texture2D} texture
 97      */
 98     afterRender:function (texture) {
 99         var locGL = this._gl;
100         locGL.bindFramebuffer(locGL.FRAMEBUFFER, this._oldFBO);
101         locGL.colorMask(true, true, true, true);      // #631
102     },
103 
104     /**
105      * delete FBO
106      */
107     destroy:function(){
108         this._gl.deleteFramebuffer(this._FBO);
109     }
110 });
111