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

149 lines
4.6 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. fill_shader(nullptr),
  40. stroke_shader(nullptr)
  41. {
  42. // Load fill shader
  43. fill_shader = resource_manager->load<shader_program>("outline-fill-unskinned.glsl");
  44. fill_model_view_projection_input = fill_shader->get_input("model_view_projection");
  45. // Load stroke shader
  46. stroke_shader = resource_manager->load<shader_program>("outline-stroke-unskinned.glsl");
  47. stroke_model_view_projection_input = stroke_shader->get_input("model_view_projection");
  48. stroke_width_input = stroke_shader->get_input("width");
  49. stroke_color_input = stroke_shader->get_input("color");
  50. }
  51. outline_pass::~outline_pass()
  52. {}
  53. void outline_pass::render(render_context* context) const
  54. {
  55. rasterizer->use_framebuffer(*framebuffer);
  56. // Determine viewport based on framebuffer resolution
  57. auto viewport = framebuffer->get_dimensions();
  58. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  59. // Get camera matrices
  60. float4x4 view = context->camera->get_view_tween().interpolate(context->alpha);
  61. float4x4 view_projection = context->camera->get_view_projection_tween().interpolate(context->alpha);
  62. float4x4 model_view_projection;
  63. glEnable(GL_CULL_FACE);
  64. glCullFace(GL_BACK);
  65. glDisable(GL_DEPTH_TEST);
  66. glEnable(GL_STENCIL_TEST);
  67. // Render fill
  68. {
  69. glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  70. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  71. glStencilFunc(GL_ALWAYS, 2, 0xFF);
  72. glStencilMask(0xFF);
  73. glDisable(GL_BLEND);
  74. // Setup fill shader
  75. rasterizer->use_program(*fill_shader);
  76. // Render fills
  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. fill_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. }
  87. // Render stroke
  88. {
  89. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  90. if (outline_color.w < 1.0f)
  91. {
  92. glEnable(GL_BLEND);
  93. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  94. }
  95. else
  96. {
  97. glDisable(GL_BLEND);
  98. }
  99. glStencilFunc(GL_NOTEQUAL, 2, 0xFF);
  100. glStencilMask(0x00);
  101. // Setup stroke shader
  102. rasterizer->use_program(*stroke_shader);
  103. stroke_width_input->upload(outline_width);
  104. stroke_color_input->upload(outline_color);
  105. // Render strokes
  106. for (const render_operation& operation: context->operations)
  107. {
  108. const ::material* material = operation.material;
  109. if (!material || !(material->get_flags() & MATERIAL_FLAG_OUTLINE))
  110. continue;
  111. model_view_projection = view_projection * operation.transform;
  112. stroke_model_view_projection_input->upload(model_view_projection);
  113. rasterizer->draw_arrays(*operation.vertex_array, operation.drawing_mode, operation.start_index, operation.index_count);
  114. }
  115. }
  116. glDisable(GL_STENCIL_TEST);
  117. }
  118. void outline_pass::set_outline_width(float width)
  119. {
  120. outline_width = width;
  121. }
  122. void outline_pass::set_outline_color(const float4& color)
  123. {
  124. outline_color = color;
  125. }