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

113 lines
3.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/simple-render-pass.hpp"
  20. #include "gl/rasterizer.hpp"
  21. #include "gl/framebuffer.hpp"
  22. #include "gl/shader-program.hpp"
  23. #include "gl/shader-input.hpp"
  24. #include "gl/vertex-buffer.hpp"
  25. #include "gl/vertex-array.hpp"
  26. #include "gl/vertex-attribute-type.hpp"
  27. #include "gl/drawing-mode.hpp"
  28. #include "gl/texture-2d.hpp"
  29. #include "gl/texture-wrapping.hpp"
  30. #include "gl/texture-filter.hpp"
  31. #include "renderer/vertex-attributes.hpp"
  32. #include "renderer/render-context.hpp"
  33. #include "renderer/material.hpp"
  34. #include "renderer/material-property.hpp"
  35. #include "math/math.hpp"
  36. #include <glad/glad.h>
  37. simple_render_pass::simple_render_pass(gl::rasterizer* rasterizer, const gl::framebuffer* framebuffer, gl::shader_program* shader_program):
  38. render_pass(rasterizer, framebuffer),
  39. shader_program(shader_program),
  40. time_tween(nullptr)
  41. {
  42. // Create material
  43. material = new ::material(shader_program);
  44. // Add standard properties to material
  45. time_property = material->add_property<float>("time");
  46. resolution_property = material->add_property<float2>("resolution");
  47. const float vertex_data[] =
  48. {
  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. 1.0f, -1.0f, 0.0f
  55. };
  56. std::size_t vertex_size = 3;
  57. std::size_t vertex_stride = sizeof(float) * vertex_size;
  58. std::size_t vertex_count = 6;
  59. quad_vbo = new gl::vertex_buffer(sizeof(float) * vertex_size * vertex_count, vertex_data);
  60. quad_vao = new gl::vertex_array();
  61. quad_vao->bind_attribute(VERTEX_POSITION_LOCATION, *quad_vbo, 3, gl::vertex_attribute_type::float_32, vertex_stride, 0);
  62. }
  63. simple_render_pass::~simple_render_pass()
  64. {
  65. delete material;
  66. delete quad_vao;
  67. delete quad_vbo;
  68. }
  69. void simple_render_pass::render(render_context* context) const
  70. {
  71. // Bind framebuffer
  72. rasterizer->use_framebuffer(*framebuffer);
  73. // Setup graphics context
  74. glDisable(GL_BLEND);
  75. glDisable(GL_DEPTH_TEST);
  76. glDepthMask(GL_FALSE);
  77. glEnable(GL_CULL_FACE);
  78. glCullFace(GL_BACK);
  79. // Setup viewport
  80. auto viewport = framebuffer->get_dimensions();
  81. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  82. // Change shader program
  83. rasterizer->use_program(*shader_program);
  84. // Get interpolated time
  85. float time = (time_tween) ? time_tween->interpolate(context->alpha) : 0.0f;
  86. // Update material properties
  87. time_property->set_value(time);
  88. resolution_property->set_value({static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))});
  89. // Upload material properties
  90. material->upload(context->alpha);
  91. // Draw quad
  92. rasterizer->draw_arrays(*quad_vao, gl::drawing_mode::triangles, 0, 6);
  93. }
  94. void simple_render_pass::set_time_tween(const tween<double>* time)
  95. {
  96. this->time_tween = time;
  97. }