No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

framebuffer.cpp 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* The MIT License
  2. *
  3. * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  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. #define GL_GLEXT_PROTOTYPES
  24. #include "framebuffer.h"
  25. #include <GL/glext.h>
  26. #include <GL/glu.h>
  27. #include "platform.h"
  28. #include "graphics.h"
  29. FrameBuffer::FrameBuffer(int width, int height) :
  30. m_fbo(0)
  31. {
  32. m_width = width;
  33. m_height = height;
  34. // texture where the framebuffer will be store
  35. m_frameTexture = TexturePtr(new Texture(width, height, 4));
  36. // we want a smooth framebuffer
  37. m_frameTexture->enableBilinearFilter();
  38. // use FBO only if supported
  39. if(Platform::isExtensionSupported("EXT_framebuffer_object")) {
  40. m_fallbackOldImp = false;
  41. // generate FBO
  42. glGenFramebuffersEXT(1, &m_fbo);
  43. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fbo);
  44. // attach 2D texture to this FBO
  45. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_frameTexture->getTextureId(), 0);
  46. // must be called before checking
  47. glDrawBuffer(GL_NONE);
  48. glReadBuffer(GL_NONE);
  49. GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  50. switch(status) {
  51. case GL_FRAMEBUFFER_COMPLETE_EXT:
  52. //ok
  53. break;
  54. default:
  55. m_fallbackOldImp = true;
  56. break;
  57. }
  58. } else {
  59. // otherwise fallback to copy texture from screen implementation
  60. m_fallbackOldImp = true;
  61. }
  62. }
  63. FrameBuffer::~FrameBuffer()
  64. {
  65. if(m_fbo)
  66. glDeleteFramebuffersEXT(1, &m_fbo);
  67. }
  68. void FrameBuffer::bind()
  69. {
  70. if(!m_fallbackOldImp) {
  71. // bind framebuffer
  72. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fbo);
  73. // must be called before rendering to framebuffer
  74. glDrawBuffer(GL_NONE);
  75. glReadBuffer(GL_NONE);
  76. }
  77. // clear framebuffer
  78. glClearColor(0.0, 0.0, 0.0, 0.0);
  79. glClear(GL_COLOR_BUFFER_BIT);
  80. // setup framebuffer viewport
  81. g_graphics.setViewport(m_width, m_height);
  82. }
  83. void FrameBuffer::unbind()
  84. {
  85. if(!m_fallbackOldImp) {
  86. // bind back buffer again
  87. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  88. // must be called to render to back buffer again
  89. glDrawBuffer(GL_BACK);
  90. glReadBuffer(GL_BACK);
  91. } else {
  92. // copy screen to texture
  93. m_frameTexture->copyFromScreen(0, 0, 0, 0, m_width, m_height);
  94. // clear screen
  95. glClearColor(0.0, 0.0, 0.0, 0.0);
  96. glClear(GL_COLOR_BUFFER_BIT);
  97. }
  98. // restore graphics viewport
  99. g_graphics.restoreViewport();
  100. }