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

87 lines
2.3 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. color_attachment(nullptr),
  40. depth_attachment(nullptr),
  41. stencil_attachment(nullptr)
  42. {}
  43. framebuffer::~framebuffer()
  44. {
  45. if (gl_framebuffer_id)
  46. {
  47. glDeleteFramebuffers(1, &gl_framebuffer_id);
  48. }
  49. }
  50. void framebuffer::resize(int width, int height)
  51. {
  52. dimensions = {width, height};
  53. }
  54. void framebuffer::attach(framebuffer_attachment_type attachment_type, texture_2d* texture)
  55. {
  56. glBindFramebuffer(GL_FRAMEBUFFER, gl_framebuffer_id);
  57. GLenum gl_attachment = attachment_lut[static_cast<std::size_t>(attachment_type)];
  58. glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attachment, GL_TEXTURE_2D, texture->gl_texture_id, 0);
  59. if (attachment_type == framebuffer_attachment_type::color)
  60. color_attachment = texture;
  61. else if (attachment_type == framebuffer_attachment_type::depth)
  62. depth_attachment = texture;
  63. else if (attachment_type == framebuffer_attachment_type::stencil)
  64. stencil_attachment = texture;
  65. if (!color_attachment)
  66. {
  67. glDrawBuffer(GL_NONE);
  68. glReadBuffer(GL_NONE);
  69. }
  70. else
  71. {
  72. glDrawBuffer(GL_COLOR_ATTACHMENT0);
  73. glReadBuffer(GL_COLOR_ATTACHMENT0);
  74. }
  75. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  76. }