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

110 lines
3.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 "renderer/passes/final-pass.hpp"
  20. #include "resources/resource-manager.hpp"
  21. #include "rasterizer/rasterizer.hpp"
  22. #include "rasterizer/framebuffer.hpp"
  23. #include "rasterizer/shader-program.hpp"
  24. #include "rasterizer/shader-input.hpp"
  25. #include "rasterizer/vertex-buffer.hpp"
  26. #include "rasterizer/vertex-array.hpp"
  27. #include "rasterizer/vertex-attribute-type.hpp"
  28. #include "rasterizer/drawing-mode.hpp"
  29. #include "rasterizer/texture-2d.hpp"
  30. #include "rasterizer/texture-wrapping.hpp"
  31. #include "rasterizer/texture-filter.hpp"
  32. #include "renderer/vertex-attributes.hpp"
  33. #include "renderer/render-context.hpp"
  34. #include "math/math.hpp"
  35. #include <cmath>
  36. #include <glad/glad.h>
  37. final_pass::final_pass(::rasterizer* rasterizer, const ::framebuffer* framebuffer, resource_manager* resource_manager):
  38. render_pass(rasterizer, framebuffer),
  39. color_texture(nullptr),
  40. bloom_texture(nullptr)
  41. {
  42. shader_program = resource_manager->load<::shader_program>("final.glsl");
  43. color_texture_input = shader_program->get_input("color_texture");
  44. bloom_texture_input = shader_program->get_input("bloom_texture");
  45. resolution_input = shader_program->get_input("resolution");
  46. const float vertex_data[] =
  47. {
  48. -1.0f, 1.0f, 0.0f,
  49. -1.0f, -1.0f, 0.0f,
  50. 1.0f, 1.0f, 0.0f,
  51. 1.0f, 1.0f, 0.0f,
  52. -1.0f, -1.0f, 0.0f,
  53. 1.0f, -1.0f, 0.0f
  54. };
  55. std::size_t vertex_size = 3;
  56. std::size_t vertex_stride = sizeof(float) * vertex_size;
  57. std::size_t vertex_count = 6;
  58. quad_vbo = new vertex_buffer(sizeof(float) * vertex_size * vertex_count, vertex_data);
  59. quad_vao = new vertex_array();
  60. quad_vao->bind_attribute(VERTEX_POSITION_LOCATION, *quad_vbo, 3, vertex_attribute_type::float_32, vertex_stride, 0);
  61. }
  62. final_pass::~final_pass()
  63. {
  64. delete quad_vao;
  65. delete quad_vbo;
  66. }
  67. void final_pass::render(render_context* context) const
  68. {
  69. rasterizer->use_framebuffer(*framebuffer);
  70. glDisable(GL_BLEND);
  71. glDisable(GL_DEPTH_TEST);
  72. glDepthMask(GL_FALSE);
  73. glEnable(GL_CULL_FACE);
  74. glCullFace(GL_BACK);
  75. auto viewport = framebuffer->get_dimensions();
  76. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  77. float2 resolution = {std::get<0>(viewport), std::get<1>(viewport)};
  78. // Change shader program
  79. rasterizer->use_program(*shader_program);
  80. // Upload shader parameters
  81. color_texture_input->upload(color_texture);
  82. bloom_texture_input->upload(bloom_texture);
  83. resolution_input->upload(resolution);
  84. // Draw quad
  85. rasterizer->draw_arrays(*quad_vao, drawing_mode::triangles, 0, 6);
  86. }
  87. void final_pass::set_color_texture(const texture_2d* texture)
  88. {
  89. this->color_texture = texture;
  90. }
  91. void final_pass::set_bloom_texture(const texture_2d* texture)
  92. {
  93. this->bloom_texture = texture;
  94. }