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

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