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

115 lines
3.5 KiB

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