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

159 lines
4.7 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. /*
  2. * Copyright (C) 2023 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 "render/passes/final-pass.hpp"
  20. #include "resources/resource-manager.hpp"
  21. #include "gl/rasterizer.hpp"
  22. #include "gl/framebuffer.hpp"
  23. #include "gl/shader-program.hpp"
  24. #include "gl/shader-input.hpp"
  25. #include "gl/vertex-buffer.hpp"
  26. #include "gl/vertex-array.hpp"
  27. #include "gl/vertex-attribute.hpp"
  28. #include "gl/drawing-mode.hpp"
  29. #include "gl/texture-2d.hpp"
  30. #include "gl/texture-wrapping.hpp"
  31. #include "gl/texture-filter.hpp"
  32. #include "render/vertex-attribute.hpp"
  33. #include "render/context.hpp"
  34. #include <cmath>
  35. #include <glad/glad.h>
  36. namespace render {
  37. final_pass::final_pass(gl::rasterizer* rasterizer, const gl::framebuffer* framebuffer, resource_manager* resource_manager):
  38. pass(rasterizer, framebuffer),
  39. color_texture(nullptr),
  40. bloom_texture(nullptr),
  41. bloom_weight(0.04f),
  42. blue_noise_texture(nullptr),
  43. blue_noise_scale(1.0f)
  44. {
  45. // Load shader template
  46. shader_template = resource_manager->load<render::shader_template>("final.glsl");
  47. // Build shader program
  48. shader_program = shader_template->build();
  49. color_texture_input = shader_program->get_input("color_texture");
  50. bloom_texture_input = shader_program->get_input("bloom_texture");
  51. bloom_weight_input = shader_program->get_input("bloom_weight");
  52. blue_noise_texture_input = shader_program->get_input("blue_noise_texture");
  53. blue_noise_scale_input = shader_program->get_input("blue_noise_scale");
  54. resolution_input = shader_program->get_input("resolution");
  55. time_input = shader_program->get_input("time");
  56. const float vertex_data[] =
  57. {
  58. -1.0f, 1.0f,
  59. -1.0f, -1.0f,
  60. 1.0f, 1.0f,
  61. 1.0f, 1.0f,
  62. -1.0f, -1.0f,
  63. 1.0f, -1.0f
  64. };
  65. std::size_t vertex_size = 2;
  66. std::size_t vertex_stride = sizeof(float) * vertex_size;
  67. std::size_t vertex_count = 6;
  68. quad_vbo = new gl::vertex_buffer(sizeof(float) * vertex_size * vertex_count, vertex_data);
  69. quad_vao = new gl::vertex_array();
  70. // Define position vertex attribute
  71. gl::vertex_attribute position_attribute;
  72. position_attribute.buffer = quad_vbo;
  73. position_attribute.offset = 0;
  74. position_attribute.stride = vertex_stride;
  75. position_attribute.type = gl::vertex_attribute_type::float_32;
  76. position_attribute.components = 2;
  77. // Bind vertex attributes to VAO
  78. quad_vao->bind(render::vertex_attribute::position, position_attribute);
  79. }
  80. final_pass::~final_pass()
  81. {
  82. delete quad_vao;
  83. delete quad_vbo;
  84. delete shader_program;
  85. /// @TODO
  86. // resource_manager->unload("final.glsl");
  87. }
  88. void final_pass::render(const render::context& ctx, render::queue& queue) const
  89. {
  90. rasterizer->use_framebuffer(*framebuffer);
  91. glDisable(GL_BLEND);
  92. glDisable(GL_DEPTH_TEST);
  93. glDepthMask(GL_FALSE);
  94. glEnable(GL_CULL_FACE);
  95. glCullFace(GL_BACK);
  96. auto viewport = framebuffer->get_dimensions();
  97. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  98. float2 resolution = {std::get<0>(viewport), std::get<1>(viewport)};
  99. // Change shader program
  100. rasterizer->use_program(*shader_program);
  101. // Upload shader parameters
  102. color_texture_input->upload(color_texture);
  103. if (bloom_texture && bloom_texture_input)
  104. bloom_texture_input->upload(bloom_texture);
  105. if (bloom_weight_input)
  106. bloom_weight_input->upload(bloom_weight);
  107. if (blue_noise_texture && blue_noise_texture_input)
  108. blue_noise_texture_input->upload(blue_noise_texture);
  109. if (blue_noise_scale_input)
  110. blue_noise_scale_input->upload(blue_noise_scale);
  111. if (resolution_input)
  112. resolution_input->upload(resolution);
  113. if (time_input)
  114. time_input->upload(ctx.t);
  115. // Draw quad
  116. rasterizer->draw_arrays(*quad_vao, gl::drawing_mode::triangles, 0, 6);
  117. }
  118. void final_pass::set_color_texture(const gl::texture_2d* texture)
  119. {
  120. this->color_texture = texture;
  121. }
  122. void final_pass::set_bloom_texture(const gl::texture_2d* texture) noexcept
  123. {
  124. this->bloom_texture = texture;
  125. }
  126. void final_pass::set_bloom_weight(float weight) noexcept
  127. {
  128. this->bloom_weight = weight;
  129. }
  130. void final_pass::set_blue_noise_texture(const gl::texture_2d* texture)
  131. {
  132. this->blue_noise_texture = texture;
  133. blue_noise_scale = 1.0f / static_cast<float>(texture->get_dimensions()[0]);
  134. }
  135. } // namespace render