init_render_to_texture.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "init_render_to_texture.h"
  9. #include <cassert>
  10. IGL_INLINE void igl::opengl::init_render_to_texture(
  11. const size_t width,
  12. const size_t height,
  13. GLuint & tex_id,
  14. GLuint & fbo_id,
  15. GLuint & dfbo_id)
  16. {
  17. using namespace std;
  18. // Delete if already exists
  19. glDeleteTextures(1,&tex_id);
  20. glDeleteFramebuffersEXT(1,&fbo_id);
  21. glDeleteFramebuffersEXT(1,&dfbo_id);
  22. // http://www.opengl.org/wiki/Framebuffer_Object_Examples#Quick_example.2C_render_to_texture_.282D.29
  23. glGenTextures(1, &tex_id);
  24. glBindTexture(GL_TEXTURE_2D, tex_id);
  25. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  26. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  27. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  28. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  29. //NULL means reserve texture memory, but texels are undefined
  30. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, width, height, 0, GL_BGRA, GL_FLOAT, NULL);
  31. glBindTexture(GL_TEXTURE_2D, 0);
  32. glGenFramebuffersEXT(1, &fbo_id);
  33. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_id);
  34. //Attach 2D texture to this FBO
  35. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex_id, 0);
  36. glGenRenderbuffersEXT(1, &dfbo_id);
  37. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, dfbo_id);
  38. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height);
  39. //Attach depth buffer to FBO (for this example it's not really needed, but if
  40. //drawing a 3D scene it would be necessary to attach something)
  41. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, dfbo_id);
  42. //Does the GPU support current FBO configuration?
  43. GLenum status;
  44. status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  45. assert(status == GL_FRAMEBUFFER_COMPLETE_EXT);
  46. // Unbind to clean up
  47. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
  48. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  49. }