Util_GL_Blitter.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /************************************************************************************
  2. Filename : Util_GL_Blitter.cpp
  3. Content : GL implementation for blitting, supporting scaling & rotation
  4. Created : February 24, 2015
  5. Authors : Reza Nourai
  6. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  7. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  8. you may not use the Oculus VR Rift SDK except in compliance with the License,
  9. which is provided at the time of installation or download, or which
  10. otherwise accompanies this software in either electronic or hard copy form.
  11. You may obtain a copy of the License at
  12. http://www.oculusvr.com/licenses/LICENSE-3.2
  13. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  14. distributed under the License is distributed on an "AS IS" BASIS,
  15. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. See the License for the specific language governing permissions and
  17. limitations under the License.
  18. ************************************************************************************/
  19. #include "Util_GL_Blitter.h"
  20. namespace OVR { namespace GLUtil {
  21. #define AssertOnGLError() { GLuint err = glGetError(); OVR_ASSERT_AND_UNUSED(!err,err); }
  22. //-------------------------------------------------------------------------------------
  23. // ***** CAPI::Blitter
  24. Blitter::Blitter()
  25. : ReadFBO(0)
  26. {
  27. }
  28. Blitter::~Blitter()
  29. {
  30. glDeleteFramebuffers(1, &ReadFBO);
  31. ReadFBO = 0;
  32. }
  33. bool Blitter::Initialize()
  34. {
  35. glGenFramebuffers(1, &ReadFBO);
  36. AssertOnGLError();
  37. return true;
  38. }
  39. bool Blitter::Blt(GLuint sourceTexId)
  40. {
  41. GLenum status = 0;
  42. GLint currentTex2D = 0;
  43. GLint sourceWidth = 0, sourceHeight = 0;
  44. // Store off currently selected tex2d
  45. glGetIntegerv(GL_TEXTURE_BINDING_2D, &currentTex2D);
  46. // Get source texture dimensions
  47. glBindTexture(GL_TEXTURE_2D, sourceTexId);
  48. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &sourceWidth);
  49. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &sourceHeight);
  50. // Put old texture back
  51. glBindTexture(GL_TEXTURE_2D, currentTex2D);
  52. // Save off the current FBOs
  53. GLint currentReadFB;
  54. glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &currentReadFB);
  55. AssertOnGLError();
  56. // setup draw buffer
  57. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  58. glDrawBuffer(GL_BACK);
  59. // setup read buffer
  60. glBindFramebuffer(GL_READ_FRAMEBUFFER, ReadFBO);
  61. AssertOnGLError();
  62. glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, sourceTexId, 0);
  63. glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
  64. AssertOnGLError();
  65. status = glCheckFramebufferStatus(GL_READ_FRAMEBUFFER);
  66. OVR_ASSERT_AND_UNUSED(status == GL_FRAMEBUFFER_COMPLETE, status);
  67. // Do the blt
  68. glBlitFramebuffer(0, sourceHeight, sourceWidth, 0,
  69. 0, 0, sourceWidth, sourceHeight,
  70. GL_COLOR_BUFFER_BIT, GL_NEAREST);
  71. AssertOnGLError();
  72. // Restore the previous FBOs
  73. glBindFramebuffer(GL_READ_FRAMEBUFFER, currentReadFB);
  74. AssertOnGLError();
  75. return true;
  76. }
  77. }} // namespace OVR::GLUtil