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

138 lines
4.3 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 "renderer/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 "renderer/vertex-attribute.hpp"
  33. #include "renderer/context.hpp"
  34. #include "math/math.hpp"
  35. #include <cmath>
  36. #include <glad/glad.h>
  37. final_pass::final_pass(gl::rasterizer* rasterizer, const gl::framebuffer* framebuffer, resource_manager* resource_manager):
  38. render_pass(rasterizer, framebuffer),
  39. color_texture(nullptr),
  40. bloom_texture(nullptr),
  41. blue_noise_texture(nullptr),
  42. blue_noise_scale(1.0)
  43. {
  44. shader_program = resource_manager->load<gl::shader_program>("final.glsl");
  45. color_texture_input = shader_program->get_input("color_texture");
  46. bloom_texture_input = shader_program->get_input("bloom_texture");
  47. blue_noise_texture_input = shader_program->get_input("blue_noise_texture");
  48. blue_noise_scale_input = shader_program->get_input("blue_noise_scale");
  49. resolution_input = shader_program->get_input("resolution");
  50. time_input = shader_program->get_input("time");
  51. const float vertex_data[] =
  52. {
  53. -1.0f, 1.0f, 0.0f,
  54. -1.0f, -1.0f, 0.0f,
  55. 1.0f, 1.0f, 0.0f,
  56. 1.0f, 1.0f, 0.0f,
  57. -1.0f, -1.0f, 0.0f,
  58. 1.0f, -1.0f, 0.0f
  59. };
  60. std::size_t vertex_size = 3;
  61. std::size_t vertex_stride = sizeof(float) * vertex_size;
  62. std::size_t vertex_count = 6;
  63. quad_vbo = new gl::vertex_buffer(sizeof(float) * vertex_size * vertex_count, vertex_data);
  64. quad_vao = new gl::vertex_array();
  65. // Define position vertex attribute
  66. gl::vertex_attribute position_attribute;
  67. position_attribute.buffer = quad_vbo;
  68. position_attribute.offset = 0;
  69. position_attribute.stride = vertex_stride;
  70. position_attribute.type = gl::vertex_attribute_type::float_32;
  71. position_attribute.components = 3;
  72. // Bind vertex attributes to VAO
  73. quad_vao->bind(render::vertex_attribute::position, position_attribute);
  74. }
  75. final_pass::~final_pass()
  76. {
  77. delete quad_vao;
  78. delete quad_vbo;
  79. }
  80. void final_pass::render(const render::context& ctx, render::queue& queue) const
  81. {
  82. rasterizer->use_framebuffer(*framebuffer);
  83. glDisable(GL_BLEND);
  84. glDisable(GL_DEPTH_TEST);
  85. glDepthMask(GL_FALSE);
  86. glEnable(GL_CULL_FACE);
  87. glCullFace(GL_BACK);
  88. auto viewport = framebuffer->get_dimensions();
  89. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  90. float2 resolution = {std::get<0>(viewport), std::get<1>(viewport)};
  91. // Change shader program
  92. rasterizer->use_program(*shader_program);
  93. // Upload shader parameters
  94. color_texture_input->upload(color_texture);
  95. if (bloom_texture && bloom_texture_input)
  96. bloom_texture_input->upload(bloom_texture);
  97. if (blue_noise_texture && blue_noise_texture_input)
  98. blue_noise_texture_input->upload(blue_noise_texture);
  99. if (blue_noise_scale_input)
  100. blue_noise_scale_input->upload(blue_noise_scale);
  101. if (resolution_input)
  102. resolution_input->upload(resolution);
  103. if (time_input)
  104. time_input->upload(ctx.t);
  105. // Draw quad
  106. rasterizer->draw_arrays(*quad_vao, gl::drawing_mode::triangles, 0, 6);
  107. }
  108. void final_pass::set_color_texture(const gl::texture_2d* texture)
  109. {
  110. this->color_texture = texture;
  111. }
  112. void final_pass::set_bloom_texture(const gl::texture_2d* texture)
  113. {
  114. this->bloom_texture = texture;
  115. }
  116. void final_pass::set_blue_noise_texture(const gl::texture_2d* texture)
  117. {
  118. this->blue_noise_texture = texture;
  119. blue_noise_scale = 1.0f / static_cast<float>(texture->get_dimensions()[0]);
  120. }