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

123 lines
3.7 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.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-attribute.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. // Define position vertex attribute
  62. gl::vertex_attribute position_attribute;
  63. position_attribute.buffer = quad_vbo;
  64. position_attribute.offset = 0;
  65. position_attribute.stride = vertex_stride;
  66. position_attribute.type = gl::vertex_attribute_type::float_32;
  67. position_attribute.components = 3;
  68. // Bind vertex attributes to VAO
  69. quad_vao->bind(render::vertex_attribute::position, position_attribute);
  70. }
  71. simple_render_pass::~simple_render_pass()
  72. {
  73. delete material;
  74. delete quad_vao;
  75. delete quad_vbo;
  76. }
  77. void simple_render_pass::render(render_context* context) const
  78. {
  79. // Bind framebuffer
  80. rasterizer->use_framebuffer(*framebuffer);
  81. // Setup graphics context
  82. glDisable(GL_BLEND);
  83. glDisable(GL_DEPTH_TEST);
  84. glDepthMask(GL_FALSE);
  85. glEnable(GL_CULL_FACE);
  86. glCullFace(GL_BACK);
  87. // Setup viewport
  88. auto viewport = framebuffer->get_dimensions();
  89. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  90. // Change shader program
  91. rasterizer->use_program(*shader_program);
  92. // Get interpolated time
  93. float time = (time_tween) ? time_tween->interpolate(context->alpha) : 0.0f;
  94. // Update material properties
  95. time_property->set_value(time);
  96. resolution_property->set_value({static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))});
  97. // Upload material properties
  98. material->upload(context->alpha);
  99. // Draw quad
  100. rasterizer->draw_arrays(*quad_vao, gl::drawing_mode::triangles, 0, 6);
  101. }
  102. void simple_render_pass::set_time_tween(const tween<double>* time)
  103. {
  104. this->time_tween = time;
  105. }