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

112 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/passes/outline-pass.hpp"
  20. #include "resources/resource-manager.hpp"
  21. #include "rasterizer/rasterizer.hpp"
  22. #include "rasterizer/framebuffer.hpp"
  23. #include "rasterizer/shader-program.hpp"
  24. #include "rasterizer/shader-input.hpp"
  25. #include "rasterizer/vertex-buffer.hpp"
  26. #include "rasterizer/vertex-array.hpp"
  27. #include "rasterizer/vertex-attribute-type.hpp"
  28. #include "rasterizer/drawing-mode.hpp"
  29. #include "renderer/vertex-attributes.hpp"
  30. #include "renderer/render-context.hpp"
  31. #include "renderer/material.hpp"
  32. #include "renderer/material-flags.hpp"
  33. #include "scene/camera.hpp"
  34. #include "math/math.hpp"
  35. #include <cmath>
  36. #include <glad/glad.h>
  37. outline_pass::outline_pass(::rasterizer* rasterizer, const ::framebuffer* framebuffer, resource_manager* resource_manager):
  38. render_pass(rasterizer, framebuffer),
  39. outline_shader(nullptr)
  40. {
  41. // Load outline shader
  42. outline_shader = resource_manager->load<shader_program>("outline-unskinned.glsl");
  43. model_view_projection_input = outline_shader->get_input("model_view_projection");
  44. outline_width_input = outline_shader->get_input("outline_width");
  45. outline_color_input = outline_shader->get_input("outline_color");
  46. }
  47. outline_pass::~outline_pass()
  48. {}
  49. void outline_pass::render(render_context* context) const
  50. {
  51. rasterizer->use_framebuffer(*framebuffer);
  52. if (outline_color.w < 1.0f)
  53. {
  54. glEnable(GL_BLEND);
  55. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  56. }
  57. else
  58. {
  59. glDisable(GL_BLEND);
  60. }
  61. glDisable(GL_CULL_FACE);
  62. glCullFace(GL_BACK);
  63. glEnable(GL_STENCIL_TEST);
  64. glStencilFunc(GL_NOTEQUAL, 1, 0xFF);
  65. glStencilMask(0x00);
  66. // Determine viewport based on framebuffer resolution
  67. auto viewport = framebuffer->get_dimensions();
  68. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  69. float4x4 view = context->camera->get_view_tween().interpolate(context->alpha);
  70. float4x4 view_projection = context->camera->get_view_projection_tween().interpolate(context->alpha);
  71. float4x4 model_view_projection;
  72. // Perform iterative blur subpass
  73. rasterizer->use_program(*outline_shader);
  74. outline_width_input->upload(outline_width);
  75. outline_color_input->upload(outline_color);
  76. // Render outlines
  77. for (const render_operation& operation: context->operations)
  78. {
  79. const ::material* material = operation.material;
  80. if (!material || !(material->get_flags() & MATERIAL_FLAG_OUTLINE))
  81. continue;
  82. model_view_projection = view_projection * operation.transform;
  83. model_view_projection_input->upload(model_view_projection);
  84. rasterizer->draw_arrays(*operation.vertex_array, operation.drawing_mode, operation.start_index, operation.index_count);
  85. }
  86. glDisable(GL_STENCIL_TEST);
  87. }
  88. void outline_pass::set_outline_width(float width)
  89. {
  90. outline_width = width;
  91. }
  92. void outline_pass::set_outline_color(const float4& color)
  93. {
  94. outline_color = color;
  95. }