💿🐜 Antkeeper source code https://antkeeper.com
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.

92 lines
2.4 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "gl/framebuffer.hpp"
  20. #include "gl/texture-2d.hpp"
  21. #include <glad/glad.h>
  22. namespace gl {
  23. static constexpr GLenum attachment_lut[] =
  24. {
  25. GL_COLOR_ATTACHMENT0,
  26. GL_DEPTH_ATTACHMENT,
  27. GL_STENCIL_ATTACHMENT
  28. };
  29. framebuffer::framebuffer(int width, int height):
  30. gl_framebuffer_id(0),
  31. dimensions({width, height}),
  32. color_attachment(nullptr),
  33. depth_attachment(nullptr),
  34. stencil_attachment(nullptr)
  35. {
  36. glGenFramebuffers(1, &gl_framebuffer_id);
  37. }
  38. framebuffer::framebuffer():
  39. gl_framebuffer_id(0),
  40. dimensions({0, 0}),
  41. color_attachment(nullptr),
  42. depth_attachment(nullptr),
  43. stencil_attachment(nullptr)
  44. {}
  45. framebuffer::~framebuffer()
  46. {
  47. if (gl_framebuffer_id)
  48. {
  49. glDeleteFramebuffers(1, &gl_framebuffer_id);
  50. }
  51. }
  52. void framebuffer::resize(const std::array<int, 2>& dimensions)
  53. {
  54. this->dimensions = dimensions;
  55. }
  56. void framebuffer::attach(framebuffer_attachment_type attachment_type, texture_2d* texture)
  57. {
  58. glBindFramebuffer(GL_FRAMEBUFFER, gl_framebuffer_id);
  59. GLenum gl_attachment = attachment_lut[static_cast<std::size_t>(attachment_type)];
  60. glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attachment, GL_TEXTURE_2D, texture->gl_texture_id, 0);
  61. if (attachment_type == framebuffer_attachment_type::color)
  62. color_attachment = texture;
  63. else if (attachment_type == framebuffer_attachment_type::depth)
  64. depth_attachment = texture;
  65. else if (attachment_type == framebuffer_attachment_type::stencil)
  66. stencil_attachment = texture;
  67. if (!color_attachment)
  68. {
  69. glDrawBuffer(GL_NONE);
  70. glReadBuffer(GL_NONE);
  71. }
  72. else
  73. {
  74. glDrawBuffer(GL_COLOR_ATTACHMENT0);
  75. glReadBuffer(GL_COLOR_ATTACHMENT0);
  76. }
  77. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  78. }
  79. } // namespace gl