init_render_to_texture.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "gl.h"
  10. #include <cassert>
  11. IGL_INLINE void igl::opengl::init_render_to_texture(
  12. const size_t width,
  13. const size_t height,
  14. const bool depth_texture,
  15. GLuint & tex_id,
  16. GLuint & fbo_id,
  17. GLuint & d_id)
  18. {
  19. using namespace std;
  20. // http://www.opengl.org/wiki/Framebuffer_Object_Examples#Quick_example.2C_render_to_texture_.282D.29
  21. const auto & gen_tex = [](GLuint & tex_id)
  22. {
  23. glGenTextures(1, &tex_id);
  24. glBindTexture(GL_TEXTURE_2D, tex_id);
  25. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  26. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  27. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  28. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  29. };
  30. gen_tex(tex_id);
  31. //NULL means reserve texture memory, but texels are undefined
  32. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_BGRA, GL_FLOAT, NULL);
  33. glBindTexture(GL_TEXTURE_2D, 0);
  34. glGenFramebuffers(1, &fbo_id);
  35. glBindFramebuffer(GL_FRAMEBUFFER, fbo_id);
  36. //Attach 2D texture to this FBO
  37. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex_id, 0);
  38. if(depth_texture)
  39. {
  40. // Generate a depth texture
  41. gen_tex(d_id);
  42. glTexImage2D(
  43. GL_TEXTURE_2D,
  44. 0,
  45. GL_DEPTH_COMPONENT32,
  46. width,
  47. height,
  48. 0,
  49. GL_DEPTH_COMPONENT,
  50. GL_FLOAT,
  51. NULL);
  52. glFramebufferTexture2D(
  53. GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_TEXTURE_2D,d_id,0);
  54. }else
  55. {
  56. // Attach a depth buffer
  57. glGenRenderbuffers(1, &d_id);
  58. glBindRenderbuffer(GL_RENDERBUFFER, d_id);
  59. glRenderbufferStorage(GL_RENDERBUFFER,GL_DEPTH_COMPONENT24,width,height);
  60. glFramebufferRenderbuffer(
  61. GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,d_id);
  62. }
  63. //Does the GPU support current FBO configuration?
  64. GLenum status;
  65. status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  66. assert(status == GL_FRAMEBUFFER_COMPLETE);
  67. // Unbind to clean up
  68. if(!depth_texture)
  69. {
  70. glBindRenderbuffer(GL_RENDERBUFFER, 0);
  71. }
  72. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  73. }
  74. IGL_INLINE void igl::opengl::init_render_to_texture(
  75. const size_t width,
  76. const size_t height,
  77. GLuint & tex_id,
  78. GLuint & fbo_id,
  79. GLuint & dfbo_id)
  80. {
  81. return init_render_to_texture(width,height,false,tex_id,fbo_id,dfbo_id);
  82. }