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

135 lines
4.2 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-type.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-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(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. time_tween(nullptr)
  44. {
  45. shader_program = resource_manager->load<gl::shader_program>("final.glsl");
  46. color_texture_input = shader_program->get_input("color_texture");
  47. bloom_texture_input = shader_program->get_input("bloom_texture");
  48. blue_noise_texture_input = shader_program->get_input("blue_noise_texture");
  49. blue_noise_scale_input = shader_program->get_input("blue_noise_scale");
  50. resolution_input = shader_program->get_input("resolution");
  51. time_input = shader_program->get_input("time");
  52. const float vertex_data[] =
  53. {
  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. 1.0f, -1.0f, 0.0f
  60. };
  61. std::size_t vertex_size = 3;
  62. std::size_t vertex_stride = sizeof(float) * vertex_size;
  63. std::size_t vertex_count = 6;
  64. quad_vbo = new gl::vertex_buffer(sizeof(float) * vertex_size * vertex_count, vertex_data);
  65. quad_vao = new gl::vertex_array();
  66. quad_vao->bind_attribute(VERTEX_POSITION_LOCATION, *quad_vbo, 3, gl::vertex_attribute_type::float_32, vertex_stride, 0);
  67. }
  68. final_pass::~final_pass()
  69. {
  70. delete quad_vao;
  71. delete quad_vbo;
  72. }
  73. void final_pass::render(render_context* context) const
  74. {
  75. rasterizer->use_framebuffer(*framebuffer);
  76. glDisable(GL_BLEND);
  77. glDisable(GL_DEPTH_TEST);
  78. glDepthMask(GL_FALSE);
  79. glEnable(GL_CULL_FACE);
  80. glCullFace(GL_BACK);
  81. auto viewport = framebuffer->get_dimensions();
  82. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  83. float2 resolution = {std::get<0>(viewport), std::get<1>(viewport)};
  84. float time = (time_tween) ? (*time_tween)[context->alpha] : 0.0f;
  85. // Change shader program
  86. rasterizer->use_program(*shader_program);
  87. // Upload shader parameters
  88. color_texture_input->upload(color_texture);
  89. if (bloom_texture && bloom_texture_input)
  90. bloom_texture_input->upload(bloom_texture);
  91. if (blue_noise_texture && blue_noise_texture_input)
  92. blue_noise_texture_input->upload(blue_noise_texture);
  93. if (blue_noise_scale_input)
  94. blue_noise_scale_input->upload(blue_noise_scale);
  95. if (resolution_input)
  96. resolution_input->upload(resolution);
  97. if (time_input)
  98. time_input->upload(time);
  99. // Draw quad
  100. rasterizer->draw_arrays(*quad_vao, gl::drawing_mode::triangles, 0, 6);
  101. }
  102. void final_pass::set_color_texture(const gl::texture_2d* texture)
  103. {
  104. this->color_texture = texture;
  105. }
  106. void final_pass::set_bloom_texture(const gl::texture_2d* texture)
  107. {
  108. this->bloom_texture = texture;
  109. }
  110. void final_pass::set_blue_noise_texture(const gl::texture_2d* texture)
  111. {
  112. this->blue_noise_texture = texture;
  113. blue_noise_scale = 1.0f / static_cast<float>(texture->get_dimensions()[0]);
  114. }
  115. void final_pass::set_time_tween(const tween<double>* time)
  116. {
  117. this->time_tween = time;
  118. }