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

294 lines
10 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/passes/shadow-map-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/drawing-mode.hpp"
  26. #include "renderer/render-context.hpp"
  27. #include "renderer/material.hpp"
  28. #include "renderer/material-flags.hpp"
  29. #include "scene/camera.hpp"
  30. #include "scene/light.hpp"
  31. #include "geometry/view-frustum.hpp"
  32. #include "geometry/aabb.hpp"
  33. #include "configuration.hpp"
  34. #include "math/math.hpp"
  35. #include <cmath>
  36. #include <glad/glad.h>
  37. static bool operation_compare(const render_operation& a, const render_operation& b);
  38. void shadow_map_pass::distribute_frustum_splits(float* split_distances, std::size_t split_count, float split_scheme, float near, float far)
  39. {
  40. // Calculate split distances
  41. for (std::size_t i = 0; i < split_count; ++i)
  42. {
  43. float part = static_cast<float>(i + 1) / static_cast<float>(split_count + 1);
  44. // Calculate uniform split distance
  45. float uniform_split_distance = near + (far - near) * part;
  46. // Calculate logarithmic split distance
  47. float log_split_distance = near * std::pow(far / near, part);
  48. // Interpolate between uniform and logarithmic split distances
  49. split_distances[i] = log_split_distance * split_scheme + uniform_split_distance * (1.0f - split_scheme);
  50. }
  51. }
  52. shadow_map_pass::shadow_map_pass(::rasterizer* rasterizer, const ::framebuffer* framebuffer, resource_manager* resource_manager):
  53. render_pass(rasterizer, framebuffer),
  54. split_scheme_weight(0.5f),
  55. light(nullptr)
  56. {
  57. // Load skinned shader program
  58. unskinned_shader_program = resource_manager->load<::shader_program>("depth-unskinned.glsl");
  59. unskinned_model_view_projection_input = unskinned_shader_program->get_input("model_view_projection");
  60. // Load unskinned shader program
  61. skinned_shader_program = resource_manager->load<::shader_program>("depth-skinned.glsl");
  62. skinned_model_view_projection_input = skinned_shader_program->get_input("model_view_projection");
  63. // Calculate bias-tile matrices
  64. float4x4 bias_matrix = math::translate(math::identity4x4<float>, float3{0.5f, 0.5f, 0.5f}) * math::scale(math::identity4x4<float>, float3{0.5f, 0.5f, 0.5f});
  65. float4x4 tile_scale = math::scale(math::identity4x4<float>, float3{0.5f, 0.5f, 1.0f});
  66. for (int i = 0; i < 4; ++i)
  67. {
  68. float x = static_cast<float>(i % 2) * 0.5f;
  69. float y = static_cast<float>(i / 2) * 0.5f;
  70. float4x4 tile_matrix = math::translate(math::identity4x4<float>, float3{x, y, 0.0f}) * tile_scale;
  71. bias_tile_matrices[i] = tile_matrix * bias_matrix;
  72. }
  73. }
  74. shadow_map_pass::~shadow_map_pass()
  75. {}
  76. void shadow_map_pass::render(render_context* context) const
  77. {
  78. // Abort if no directional light was set
  79. if (!light)
  80. {
  81. return;
  82. }
  83. rasterizer->use_framebuffer(*framebuffer);
  84. // Disable blending
  85. glDisable(GL_BLEND);
  86. // Enable depth testing
  87. glEnable(GL_DEPTH_TEST);
  88. glDepthFunc(GL_LESS);
  89. glDepthMask(GL_TRUE);
  90. // Disable face culling
  91. glDisable(GL_CULL_FACE);
  92. // For half-z buffer
  93. //glDepthRange(-1.0f, 1.0f);
  94. // Get camera
  95. const scene::camera& camera = *context->camera;
  96. // Calculate distances to the depth clipping planes of each frustum split
  97. float clip_near = camera.get_clip_near_tween().interpolate(context->alpha);
  98. float clip_far = camera.get_clip_far_tween().interpolate(context->alpha);
  99. split_distances[0] = clip_near;
  100. split_distances[4] = clip_far;
  101. distribute_frustum_splits(&split_distances[1], 3, split_scheme_weight, clip_near, clip_far);
  102. // Calculate viewports for each shadow map
  103. const int shadow_map_resolution = std::get<0>(framebuffer->get_dimensions()) / 2;
  104. float4 shadow_map_viewports[4];
  105. for (int i = 0; i < 4; ++i)
  106. {
  107. int x = i % 2;
  108. int y = i / 2;
  109. float4& viewport = shadow_map_viewports[i];
  110. viewport[0] = static_cast<float>(x * shadow_map_resolution);
  111. viewport[1] = static_cast<float>(y * shadow_map_resolution);
  112. viewport[2] = static_cast<float>(shadow_map_resolution);
  113. viewport[3] = static_cast<float>(shadow_map_resolution);
  114. }
  115. // Calculate a view-projection matrix from the directional light's transform
  116. math::transform<float> light_transform = light->get_transform_tween().interpolate(context->alpha);
  117. float3 forward = light_transform.rotation * global_forward;
  118. float3 up = light_transform.rotation * global_up;
  119. float4x4 light_view = math::look_at(light_transform.translation, light_transform.translation + forward, up);
  120. float4x4 light_projection = math::ortho_half_z(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
  121. float4x4 light_view_projection = light_projection * light_view;
  122. // Get the camera's view matrix
  123. float4x4 camera_view = camera.get_view_tween().interpolate(context->alpha);
  124. float4x4 crop_matrix;
  125. float4x4 cropped_view_projection;
  126. float4x4 model_view_projection;
  127. // Sort render operations
  128. context->operations.sort(operation_compare);
  129. shader_program* active_shader_program = nullptr;
  130. for (int i = 0; i < 4; ++i)
  131. {
  132. // Set viewport for this shadow map
  133. const float4& viewport = shadow_map_viewports[i];
  134. rasterizer->set_viewport(viewport[0], viewport[1], viewport[2], viewport[3]);
  135. // Calculate projection matrix for view camera subfrustum
  136. const float subfrustum_near = split_distances[i];
  137. const float subfrustum_far = split_distances[i + 1];
  138. float4x4 subfrustum_projection = math::perspective_half_z(camera.get_fov(), camera.get_aspect_ratio(), subfrustum_near, subfrustum_far);
  139. // Calculate view camera subfrustum
  140. view_frustum<float> subfrustum(subfrustum_projection * camera_view);
  141. // Create AABB containing the view camera subfrustum corners
  142. const std::array<vector<float, 3>, 8>& subfrustum_corners = subfrustum.get_corners();
  143. aabb<float> subfrustum_aabb = {subfrustum_corners[0], subfrustum_corners[0]};
  144. for (int j = 1; j < 8; ++j)
  145. {
  146. for (int k = 0; k < 3; ++k)
  147. {
  148. subfrustum_aabb.min_point[k] = std::min<float>(subfrustum_aabb.min_point[k], subfrustum_corners[j][k]);
  149. subfrustum_aabb.max_point[k] = std::max<float>(subfrustum_aabb.max_point[k], subfrustum_corners[j][k]);
  150. }
  151. }
  152. // Transform subfrustum AABB into the light clip space
  153. aabb<float> cropping_bounds = aabb<float>::transform(subfrustum_aabb, light_view_projection);
  154. // Calculate scale
  155. float3 scale;
  156. scale.x = 2.0f / (cropping_bounds.max_point.x - cropping_bounds.min_point.x);
  157. scale.y = 2.0f / (cropping_bounds.max_point.y - cropping_bounds.min_point.y);
  158. scale.z = 1.0f / (cropping_bounds.max_point.z - cropping_bounds.min_point.z);
  159. //scale.z = 2.0f / (cropping_bounds.max_point.z - cropping_bounds.min_point.z);
  160. // Quantize scale
  161. float scale_quantizer = 64.0f;
  162. scale.x = 1.0f / std::ceil(1.0f / scale.x * scale_quantizer) * scale_quantizer;
  163. scale.y = 1.0f / std::ceil(1.0f / scale.y * scale_quantizer) * scale_quantizer;
  164. // Calculate offset
  165. float3 offset;
  166. offset.x = (cropping_bounds.max_point.x + cropping_bounds.min_point.x) * scale.x * -0.5f;
  167. offset.y = (cropping_bounds.max_point.y + cropping_bounds.min_point.y) * scale.y * -0.5f;
  168. offset.z = -cropping_bounds.min_point.z * scale.z;
  169. //offset.z = (cropping_bounds.max_point.z + cropping_bounds.min_point.z) * scale.z * -0.5f;
  170. // Quantize offset
  171. float half_shadow_map_resolution = static_cast<float>(shadow_map_resolution) * 0.5f;
  172. offset.x = std::ceil(offset.x * half_shadow_map_resolution) / half_shadow_map_resolution;
  173. offset.y = std::ceil(offset.y * half_shadow_map_resolution) / half_shadow_map_resolution;
  174. // Crop the light view-projection matrix
  175. crop_matrix = math::translate(math::identity4x4<float>, offset) * math::scale(math::identity4x4<float>, scale);
  176. cropped_view_projection = crop_matrix * light_view_projection;
  177. // Calculate shadow matrix
  178. shadow_matrices[i] = bias_tile_matrices[i] * cropped_view_projection;
  179. for (const render_operation& operation: context->operations)
  180. {
  181. // Skip materials which don't cast shadows
  182. const ::material* material = operation.material;
  183. if (material && (material->get_flags() & MATERIAL_FLAG_NOT_SHADOW_CASTER))
  184. {
  185. continue;
  186. }
  187. // Switch shader programs if necessary
  188. ::shader_program* shader_program = (operation.pose != nullptr) ? skinned_shader_program : unskinned_shader_program;
  189. if (active_shader_program != shader_program)
  190. {
  191. active_shader_program = shader_program;
  192. rasterizer->use_program(*active_shader_program);
  193. }
  194. // Calculate model-view-projection matrix
  195. model_view_projection = cropped_view_projection * operation.transform;
  196. // Upload operation-dependent parameters to shader program
  197. if (active_shader_program == unskinned_shader_program)
  198. {
  199. unskinned_model_view_projection_input->upload(model_view_projection);
  200. }
  201. else if (active_shader_program == skinned_shader_program)
  202. {
  203. skinned_model_view_projection_input->upload(model_view_projection);
  204. }
  205. // Draw geometry
  206. rasterizer->draw_arrays(*operation.vertex_array, operation.drawing_mode, operation.start_index, operation.index_count);
  207. }
  208. }
  209. }
  210. void shadow_map_pass::set_split_scheme_weight(float weight)
  211. {
  212. split_scheme_weight = weight;
  213. }
  214. void shadow_map_pass::set_light(const scene::directional_light* light)
  215. {
  216. this->light = light;
  217. }
  218. bool operation_compare(const render_operation& a, const render_operation& b)
  219. {
  220. // Determine transparency
  221. bool skinned_a = (a.pose != nullptr);
  222. bool skinned_b = (b.pose != nullptr);
  223. if (skinned_a)
  224. {
  225. if (skinned_b)
  226. {
  227. // A and B are both skinned, sort by VAO
  228. return (a.vertex_array < b.vertex_array);
  229. }
  230. else
  231. {
  232. // A is skinned, B is unskinned. Render B first
  233. return false;
  234. }
  235. }
  236. else
  237. {
  238. if (skinned_b)
  239. {
  240. // A is unskinned, B is skinned. Render A first
  241. return true;
  242. }
  243. else
  244. {
  245. // A and B are both unskinned, sort by VAO
  246. return (a.vertex_array < b.vertex_array);
  247. }
  248. }
  249. }