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

114 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.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/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. {
  41. // Create material
  42. material = new ::material(shader_program);
  43. // Add standard properties to material
  44. time_property = material->add_property<float>("time");
  45. resolution_property = material->add_property<float2>("resolution");
  46. const float vertex_data[] =
  47. {
  48. -1.0f, 1.0f, 0.0f,
  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. };
  55. std::size_t vertex_size = 3;
  56. std::size_t vertex_stride = sizeof(float) * vertex_size;
  57. std::size_t vertex_count = 6;
  58. quad_vbo = new gl::vertex_buffer(sizeof(float) * vertex_size * vertex_count, vertex_data);
  59. quad_vao = new gl::vertex_array();
  60. // Define position vertex attribute
  61. gl::vertex_attribute position_attribute;
  62. position_attribute.buffer = quad_vbo;
  63. position_attribute.offset = 0;
  64. position_attribute.stride = vertex_stride;
  65. position_attribute.type = gl::vertex_attribute_type::float_32;
  66. position_attribute.components = 3;
  67. // Bind vertex attributes to VAO
  68. quad_vao->bind(render::vertex_attribute::position, position_attribute);
  69. }
  70. simple_render_pass::~simple_render_pass()
  71. {
  72. delete material;
  73. delete quad_vao;
  74. delete quad_vbo;
  75. }
  76. void simple_render_pass::render(const render::context& ctx, render::queue& queue) const
  77. {
  78. // Bind framebuffer
  79. rasterizer->use_framebuffer(*framebuffer);
  80. // Setup graphics context
  81. glDisable(GL_BLEND);
  82. glDisable(GL_DEPTH_TEST);
  83. glDepthMask(GL_FALSE);
  84. glEnable(GL_CULL_FACE);
  85. glCullFace(GL_BACK);
  86. // Setup viewport
  87. auto viewport = framebuffer->get_dimensions();
  88. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  89. // Change shader program
  90. rasterizer->use_program(*shader_program);
  91. // Update material properties
  92. time_property->set_value(ctx.t);
  93. resolution_property->set_value({static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))});
  94. // Upload material properties
  95. material->upload(ctx.alpha);
  96. // Draw quad
  97. rasterizer->draw_arrays(*quad_vao, gl::drawing_mode::triangles, 0, 6);
  98. }