💿🐜 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.

88 lines
2.4 KiB

  1. /*
  2. * Copyright (C) 2020 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 "rasterizer/framebuffer.hpp"
  20. #include "rasterizer/texture-2d.hpp"
  21. #include <glad/glad.h>
  22. static constexpr GLenum attachment_lut[] =
  23. {
  24. GL_COLOR_ATTACHMENT0,
  25. GL_DEPTH_ATTACHMENT,
  26. GL_STENCIL_ATTACHMENT
  27. };
  28. framebuffer::framebuffer(int width, int height):
  29. gl_framebuffer_id(0),
  30. dimensions({width, height}),
  31. color_attachment(nullptr),
  32. depth_attachment(nullptr),
  33. stencil_attachment(nullptr)
  34. {
  35. glGenFramebuffers(1, &gl_framebuffer_id);
  36. }
  37. framebuffer::framebuffer():
  38. gl_framebuffer_id(0),
  39. dimensions({0, 0}),
  40. color_attachment(nullptr),
  41. depth_attachment(nullptr),
  42. stencil_attachment(nullptr)
  43. {}
  44. framebuffer::~framebuffer()
  45. {
  46. if (gl_framebuffer_id)
  47. {
  48. glDeleteFramebuffers(1, &gl_framebuffer_id);
  49. }
  50. }
  51. void framebuffer::resize(const std::array<int, 2>& dimensions)
  52. {
  53. this->dimensions = dimensions;
  54. }
  55. void framebuffer::attach(framebuffer_attachment_type attachment_type, texture_2d* texture)
  56. {
  57. glBindFramebuffer(GL_FRAMEBUFFER, gl_framebuffer_id);
  58. GLenum gl_attachment = attachment_lut[static_cast<std::size_t>(attachment_type)];
  59. glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attachment, GL_TEXTURE_2D, texture->gl_texture_id, 0);
  60. if (attachment_type == framebuffer_attachment_type::color)
  61. color_attachment = texture;
  62. else if (attachment_type == framebuffer_attachment_type::depth)
  63. depth_attachment = texture;
  64. else if (attachment_type == framebuffer_attachment_type::stencil)
  65. stencil_attachment = texture;
  66. if (!color_attachment)
  67. {
  68. glDrawBuffer(GL_NONE);
  69. glReadBuffer(GL_NONE);
  70. }
  71. else
  72. {
  73. glDrawBuffer(GL_COLOR_ATTACHMENT0);
  74. glReadBuffer(GL_COLOR_ATTACHMENT0);
  75. }
  76. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  77. }