LayeredFramebuffer.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <iostream>
  2. #include "LayeredFramebuffer.h"
  3. LayeredFramebuffer::LayeredFramebuffer(int width, int height, int depth)
  4. : texture(0), buffer(0) {
  5. glGenTextures(1, &texture);
  6. glBindTexture(GL_TEXTURE_3D, texture);
  7. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  8. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  9. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  10. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
  11. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  12. glTexImage3D(GL_TEXTURE_3D, 0, GL_R32F, width, height, depth, 0, GL_RED,
  13. GL_FLOAT, nullptr);
  14. glGenFramebuffers(1, &buffer);
  15. glBindFramebuffer(GL_FRAMEBUFFER, buffer);
  16. GLenum c = GL_COLOR_ATTACHMENT0;
  17. glFramebufferTexture3D(GL_FRAMEBUFFER, c, GL_TEXTURE_3D, texture, 0, 0);
  18. glDrawBuffers(1, &c);
  19. }
  20. LayeredFramebuffer::~LayeredFramebuffer() {
  21. glDeleteFramebuffers(1, &buffer);
  22. glDeleteTextures(1, &texture);
  23. }
  24. void LayeredFramebuffer::bindAndClear() {
  25. glBindFramebuffer(GL_FRAMEBUFFER, buffer);
  26. glClear(GL_COLOR_BUFFER_BIT);
  27. }
  28. void LayeredFramebuffer::bindTextureTo(int unit) const {
  29. glActiveTexture(GL_TEXTURE0 + unit);
  30. glBindTexture(GL_TEXTURE_3D, texture);
  31. }
  32. void LayeredFramebuffer::bindLayer(int layer) {
  33. glBindFramebuffer(GL_FRAMEBUFFER, buffer);
  34. glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D,
  35. texture, 0, layer);
  36. }
  37. bool LayeredFramebuffer::hasError() const {
  38. GLenum error = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  39. if(error == GL_FRAMEBUFFER_COMPLETE) {
  40. return false;
  41. }
  42. switch(error) {
  43. case GL_FRAMEBUFFER_UNDEFINED:
  44. std::cout << "undefined framebuffer\n";
  45. return true;
  46. case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
  47. std::cout << "incomplete framebuffer attachment\n";
  48. return true;
  49. case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
  50. std::cout << "incomplete missing framebuffer attachment\n";
  51. return true;
  52. case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
  53. std::cout << "incomplete framebuffer draw buffer\n";
  54. return true;
  55. case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
  56. std::cout << "incomplete framebuffer read buffer\n";
  57. return true;
  58. case GL_FRAMEBUFFER_UNSUPPORTED:
  59. std::cout << "unsupported framebuffer\n";
  60. return true;
  61. case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
  62. std::cout << "incomplete framebuffer multisample\n";
  63. return true;
  64. case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS:
  65. std::cout << "incomplete framebuffer layer targets\n";
  66. return true;
  67. }
  68. return "unknown error";
  69. }