CCGrabber.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /****************************************************************************
  2. Copyright (c) 2009 On-Core
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2016 Chukong Technologies Inc.
  5. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #include "2d/CCGrabber.h"
  24. #include "base/ccMacros.h"
  25. #include "renderer/CCTexture2D.h"
  26. NS_CC_BEGIN
  27. Grabber::Grabber(void)
  28. : _FBO(0)
  29. , _oldFBO(0)
  30. {
  31. memset(_oldClearColor, 0, sizeof(_oldClearColor));
  32. // generate FBO
  33. glGenFramebuffers(1, &_FBO);
  34. }
  35. void Grabber::grab(Texture2D *texture)
  36. {
  37. glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_oldFBO);
  38. // bind
  39. glBindFramebuffer(GL_FRAMEBUFFER, _FBO);
  40. // associate texture with FBO
  41. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->getName(), 0);
  42. // check if it worked (probably worth doing :) )
  43. GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  44. if (status != GL_FRAMEBUFFER_COMPLETE)
  45. {
  46. CCASSERT(0, "Frame Grabber: could not attach texture to framebuffer");
  47. }
  48. glBindFramebuffer(GL_FRAMEBUFFER, _oldFBO);
  49. }
  50. void Grabber::beforeRender(Texture2D* /*texture*/)
  51. {
  52. glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_oldFBO);
  53. glBindFramebuffer(GL_FRAMEBUFFER, _FBO);
  54. // save clear color
  55. glGetFloatv(GL_COLOR_CLEAR_VALUE, _oldClearColor);
  56. // FIXME: doesn't work with RGB565.
  57. glClearColor(0, 0, 0, 0);
  58. // BUG #631: To fix #631, uncomment the lines with #631
  59. // Warning: But it Grabber won't work with 2 effects at the same time
  60. // glClearColor(0.0f,0.0f,0.0f,1.0f); // #631
  61. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  62. // glColorMask(true, true, true, false); // #631
  63. }
  64. void Grabber::afterRender(cocos2d::Texture2D* /*texture*/)
  65. {
  66. glBindFramebuffer(GL_FRAMEBUFFER, _oldFBO);
  67. // glColorMask(true, true, true, true); // #631
  68. // Restore clear color
  69. glClearColor(_oldClearColor[0], _oldClearColor[1], _oldClearColor[2], _oldClearColor[3]);
  70. }
  71. Grabber::~Grabber()
  72. {
  73. CCLOGINFO("deallocing Grabber: %p", this);
  74. glDeleteFramebuffers(1, &_FBO);
  75. }
  76. NS_CC_END