1
0

CCGrabber.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /****************************************************************************
  2. Copyright (c) 2009 On-Core
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2017 Chukong Technologies 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. #include "2d/CCGrabber.h"
  23. #include "base/ccMacros.h"
  24. #include "renderer/CCTexture2D.h"
  25. NS_CC_BEGIN
  26. Grabber::Grabber(void)
  27. : _FBO(0)
  28. , _oldFBO(0)
  29. {
  30. memset(_oldClearColor, 0, sizeof(_oldClearColor));
  31. // generate FBO
  32. glGenFramebuffers(1, &_FBO);
  33. }
  34. void Grabber::grab(Texture2D *texture)
  35. {
  36. glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_oldFBO);
  37. // bind
  38. glBindFramebuffer(GL_FRAMEBUFFER, _FBO);
  39. // associate texture with FBO
  40. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->getName(), 0);
  41. // check if it worked (probably worth doing :) )
  42. GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  43. if (status != GL_FRAMEBUFFER_COMPLETE)
  44. {
  45. CCASSERT(0, "Frame Grabber: could not attach texture to framebuffer");
  46. }
  47. glBindFramebuffer(GL_FRAMEBUFFER, _oldFBO);
  48. }
  49. void Grabber::beforeRender(Texture2D* /*texture*/)
  50. {
  51. glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_oldFBO);
  52. glBindFramebuffer(GL_FRAMEBUFFER, _FBO);
  53. // save clear color
  54. glGetFloatv(GL_COLOR_CLEAR_VALUE, _oldClearColor);
  55. // FIXME: doesn't work with RGB565.
  56. glClearColor(0, 0, 0, 0);
  57. // BUG #631: To fix #631, uncomment the lines with #631
  58. // Warning: But it Grabber won't work with 2 effects at the same time
  59. // glClearColor(0.0f,0.0f,0.0f,1.0f); // #631
  60. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  61. // glColorMask(true, true, true, false); // #631
  62. }
  63. void Grabber::afterRender(cocos2d::Texture2D* /*texture*/)
  64. {
  65. glBindFramebuffer(GL_FRAMEBUFFER, _oldFBO);
  66. // glColorMask(true, true, true, true); // #631
  67. // Restore clear color
  68. glClearColor(_oldClearColor[0], _oldClearColor[1], _oldClearColor[2], _oldClearColor[3]);
  69. }
  70. Grabber::~Grabber()
  71. {
  72. CCLOGINFO("deallocing Grabber: %p", this);
  73. glDeleteFramebuffers(1, &_FBO);
  74. }
  75. NS_CC_END