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

145 lines
4.5 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/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. // Define position vertex attribute
  67. gl::vertex_attribute position_attribute;
  68. position_attribute.buffer = quad_vbo;
  69. position_attribute.offset = 0;
  70. position_attribute.stride = vertex_stride;
  71. position_attribute.type = gl::vertex_attribute_type::float_32;
  72. position_attribute.components = 3;
  73. // Bind vertex attributes to VAO
  74. quad_vao->bind(render::vertex_attribute::position, position_attribute);
  75. }
  76. final_pass::~final_pass()
  77. {
  78. delete quad_vao;
  79. delete quad_vbo;
  80. }
  81. void final_pass::render(render_context* context) const
  82. {
  83. rasterizer->use_framebuffer(*framebuffer);
  84. glDisable(GL_BLEND);
  85. glDisable(GL_DEPTH_TEST);
  86. glDepthMask(GL_FALSE);
  87. glEnable(GL_CULL_FACE);
  88. glCullFace(GL_BACK);
  89. auto viewport = framebuffer->get_dimensions();
  90. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  91. float2 resolution = {std::get<0>(viewport), std::get<1>(viewport)};
  92. float time = (time_tween) ? (*time_tween)[context->alpha] : 0.0f;
  93. // Change shader program
  94. rasterizer->use_program(*shader_program);
  95. // Upload shader parameters
  96. color_texture_input->upload(color_texture);
  97. if (bloom_texture && bloom_texture_input)
  98. bloom_texture_input->upload(bloom_texture);
  99. if (blue_noise_texture && blue_noise_texture_input)
  100. blue_noise_texture_input->upload(blue_noise_texture);
  101. if (blue_noise_scale_input)
  102. blue_noise_scale_input->upload(blue_noise_scale);
  103. if (resolution_input)
  104. resolution_input->upload(resolution);
  105. if (time_input)
  106. time_input->upload(time);
  107. // Draw quad
  108. rasterizer->draw_arrays(*quad_vao, gl::drawing_mode::triangles, 0, 6);
  109. }
  110. void final_pass::set_color_texture(const gl::texture_2d* texture)
  111. {
  112. this->color_texture = texture;
  113. }
  114. void final_pass::set_bloom_texture(const gl::texture_2d* texture)
  115. {
  116. this->bloom_texture = texture;
  117. }
  118. void final_pass::set_blue_noise_texture(const gl::texture_2d* texture)
  119. {
  120. this->blue_noise_texture = texture;
  121. blue_noise_scale = 1.0f / static_cast<float>(texture->get_dimensions()[0]);
  122. }
  123. void final_pass::set_time_tween(const tween<double>* time)
  124. {
  125. this->time_tween = time;
  126. }