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

547 lines
19 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/material-pass.hpp"
  20. #include "configuration.hpp"
  21. #include "resources/resource-manager.hpp"
  22. #include "rasterizer/rasterizer.hpp"
  23. #include "rasterizer/framebuffer.hpp"
  24. #include "rasterizer/shader.hpp"
  25. #include "rasterizer/shader-type.hpp"
  26. #include "rasterizer/shader-program.hpp"
  27. #include "rasterizer/shader-input.hpp"
  28. #include "rasterizer/vertex-buffer.hpp"
  29. #include "rasterizer/vertex-array.hpp"
  30. #include "rasterizer/vertex-attribute-type.hpp"
  31. #include "rasterizer/drawing-mode.hpp"
  32. #include "rasterizer/texture-2d.hpp"
  33. #include "rasterizer/texture-wrapping.hpp"
  34. #include "rasterizer/texture-filter.hpp"
  35. #include "renderer/vertex-attributes.hpp"
  36. #include "renderer/material-flags.hpp"
  37. #include "renderer/model.hpp"
  38. #include "renderer/render-context.hpp"
  39. #include "scene/camera.hpp"
  40. #include "scene/scene.hpp"
  41. #include "scene/ambient-light.hpp"
  42. #include "scene/directional-light.hpp"
  43. #include "scene/point-light.hpp"
  44. #include "scene/spotlight.hpp"
  45. #include "scene/scene.hpp"
  46. #include "configuration.hpp"
  47. #include "math/math.hpp"
  48. #include <cmath>
  49. #include <glad/glad.h>
  50. #include "shadow-map-pass.hpp"
  51. static bool operation_compare(const render_operation& a, const render_operation& b);
  52. material_pass::material_pass(::rasterizer* rasterizer, const ::framebuffer* framebuffer, resource_manager* resource_manager):
  53. render_pass(rasterizer, framebuffer),
  54. fallback_material(nullptr),
  55. time_tween(nullptr),
  56. focal_point_tween(nullptr),
  57. shadow_map_pass(nullptr),
  58. shadow_map(nullptr)
  59. {
  60. soft_shadows_texture = resource_manager->load<texture_2d>("tree-shadow.png");
  61. soft_shadows_texture->set_wrapping(texture_wrapping::clamp, texture_wrapping::clamp);
  62. soft_shadows_texture->set_filters(texture_min_filter::linear_mipmap_linear, texture_mag_filter::linear);
  63. max_ambient_light_count = MATERIAL_PASS_MAX_AMBIENT_LIGHT_COUNT;
  64. max_point_light_count = MATERIAL_PASS_MAX_POINT_LIGHT_COUNT;
  65. max_directional_light_count = MATERIAL_PASS_MAX_DIRECTIONAL_LIGHT_COUNT;
  66. max_spotlight_count = MATERIAL_PASS_MAX_SPOTLIGHT_COUNT;
  67. ambient_light_colors = new float3[max_ambient_light_count];
  68. point_light_colors = new float3[max_point_light_count];
  69. point_light_positions = new float3[max_point_light_count];
  70. point_light_attenuations = new float3[max_point_light_count];
  71. directional_light_colors = new float3[max_directional_light_count];
  72. directional_light_directions = new float3[max_directional_light_count];
  73. spotlight_colors = new float3[max_spotlight_count];
  74. spotlight_positions = new float3[max_spotlight_count];
  75. spotlight_directions = new float3[max_spotlight_count];
  76. spotlight_attenuations = new float3[max_spotlight_count];
  77. spotlight_cutoffs = new float2[max_spotlight_count];
  78. }
  79. material_pass::~material_pass()
  80. {
  81. delete[] ambient_light_colors;
  82. delete[] point_light_colors;
  83. delete[] point_light_positions;
  84. delete[] point_light_attenuations;
  85. delete[] directional_light_colors;
  86. delete[] directional_light_directions;
  87. delete[] spotlight_colors;
  88. delete[] spotlight_positions;
  89. delete[] spotlight_directions;
  90. delete[] spotlight_attenuations;
  91. delete[] spotlight_cutoffs;
  92. }
  93. void material_pass::render(render_context* context) const
  94. {
  95. rasterizer->use_framebuffer(*framebuffer);
  96. glDisable(GL_BLEND);
  97. glEnable(GL_DEPTH_TEST);
  98. glDepthFunc(GL_LESS);
  99. glDepthMask(GL_TRUE);
  100. glDepthFunc(GL_LESS);
  101. glEnable(GL_CULL_FACE);
  102. glCullFace(GL_BACK);
  103. auto viewport = framebuffer->get_dimensions();
  104. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  105. float2 resolution = {static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))};
  106. float time = (time_tween) ? time_tween->interpolate(context->alpha) : 0.0f;
  107. float3 focal_point = (focal_point_tween) ? focal_point_tween->interpolate(context->alpha) : float3{0, 0, 0};
  108. float4x4 view = context->camera->get_view_tween().interpolate(context->alpha);
  109. float4x4 projection = context->camera->get_projection_tween().interpolate(context->alpha);
  110. float4x4 view_projection = projection * view;
  111. float4x4 model_view_projection;
  112. float4x4 model;
  113. float4x4 model_view;
  114. float3x3 normal_model_view;
  115. int active_material_flags = 0;
  116. const ::shader_program* active_shader_program = nullptr;
  117. const ::material* active_material = nullptr;
  118. const parameter_set* parameters = nullptr;
  119. // Reset light counts
  120. ambient_light_count = 0;
  121. point_light_count = 0;
  122. directional_light_count = 0;
  123. spotlight_count = 0;
  124. // Collect lights
  125. const std::list<scene_object_base*>* lights = context->scene->get_objects(light::object_type_id);
  126. for (const scene_object_base* object: *lights)
  127. {
  128. // Skip inactive lights
  129. if (!object->is_active())
  130. continue;
  131. const ::light* light = static_cast<const ::light*>(object);
  132. switch (light->get_light_type())
  133. {
  134. // Add ambient light
  135. case light_type::ambient:
  136. {
  137. if (ambient_light_count < max_ambient_light_count)
  138. {
  139. ambient_light_colors[ambient_light_count] = light->get_scaled_color_tween().interpolate(context->alpha);
  140. ++ambient_light_count;
  141. }
  142. break;
  143. }
  144. // Add point light
  145. case light_type::point:
  146. {
  147. if (point_light_count < max_point_light_count)
  148. {
  149. point_light_colors[point_light_count] = light->get_scaled_color_tween().interpolate(context->alpha);
  150. // Transform position into view-space
  151. float3 position = light->get_transform_tween().interpolate(context->alpha).translation;
  152. float3 view_space_position = math::resize<3>(view * float4{position.x, position.y, position.z, 1.0f});
  153. point_light_positions[point_light_count] = view_space_position;
  154. point_light_attenuations[point_light_count] = static_cast<const point_light*>(light)->get_attenuation_tween().interpolate(context->alpha);
  155. ++point_light_count;
  156. }
  157. break;
  158. }
  159. // Add directional light
  160. case light_type::directional:
  161. {
  162. if (directional_light_count < max_directional_light_count)
  163. {
  164. directional_light_colors[directional_light_count] = light->get_scaled_color_tween().interpolate(context->alpha);
  165. // Transform direction into view-space
  166. float3 direction = static_cast<const directional_light*>(light)->get_direction_tween().interpolate(context->alpha);
  167. float3 view_space_direction = math::normalize(math::resize<3>(view * math::resize<4>(-direction)));
  168. directional_light_directions[directional_light_count] = view_space_direction;
  169. ++directional_light_count;
  170. }
  171. break;
  172. }
  173. // Add spotlight
  174. case light_type::spot:
  175. {
  176. if (spotlight_count < max_spotlight_count)
  177. {
  178. spotlight_colors[spotlight_count] = light->get_scaled_color_tween().interpolate(context->alpha);
  179. // Transform position into view-space
  180. float3 position = light->get_transform_tween().interpolate(context->alpha).translation;
  181. float3 view_space_position = math::resize<3>(view * float4{position.x, position.y, position.z, 1.0f});
  182. spotlight_positions[spotlight_count] = view_space_position;
  183. const ::spotlight* spotlight = static_cast<const ::spotlight*>(light);
  184. // Transform direction into view-space
  185. float3 direction = spotlight->get_direction_tween().interpolate(context->alpha);
  186. float3 view_space_direction = math::normalize(math::resize<3>(view * math::resize<4>(-direction)));
  187. spotlight_directions[spotlight_count] = view_space_direction;
  188. spotlight_attenuations[spotlight_count] = spotlight->get_attenuation_tween().interpolate(context->alpha);
  189. spotlight_cutoffs[spotlight_count] = spotlight->get_cosine_cutoff_tween().interpolate(context->alpha);
  190. ++spotlight_count;
  191. }
  192. break;
  193. }
  194. default:
  195. break;
  196. }
  197. }
  198. float4x4 shadow_map_matrices[4];
  199. float4 shadow_map_split_distances;
  200. if (shadow_map_pass)
  201. {
  202. for (int i = 0; i < 4; ++i)
  203. shadow_map_matrices[i] = shadow_map_pass->get_shadow_matrices()[i];
  204. // Calculate shadow map split distances
  205. for (int i = 0; i < 4; ++i)
  206. shadow_map_split_distances[i] = shadow_map_pass->get_split_distances()[i + 1];
  207. }
  208. // Sort render operations
  209. context->operations.sort(operation_compare);
  210. for (const render_operation& operation: context->operations)
  211. {
  212. // Get operation material
  213. const ::material* material = operation.material;
  214. if (!material)
  215. {
  216. if (fallback_material)
  217. {
  218. // No material specified, use fallback material
  219. material = fallback_material;
  220. }
  221. else
  222. {
  223. // No material specified and no fallback material, skip operation
  224. continue;
  225. }
  226. }
  227. // Switch materials if necessary
  228. if (active_material != material)
  229. {
  230. active_material = material;
  231. // Change rasterizer state according to material flags
  232. std::uint32_t material_flags = active_material->get_flags();
  233. if (active_material_flags != material_flags)
  234. {
  235. if ((material_flags & MATERIAL_FLAG_TRANSLUCENT) != (active_material_flags & MATERIAL_FLAG_TRANSLUCENT))
  236. {
  237. if (material_flags & MATERIAL_FLAG_TRANSLUCENT)
  238. {
  239. glEnable(GL_BLEND);
  240. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  241. }
  242. else
  243. {
  244. glDisable(GL_BLEND);
  245. }
  246. }
  247. if ((material_flags & MATERIAL_FLAG_BACK_FACES) != (active_material_flags & MATERIAL_FLAG_BACK_FACES))
  248. {
  249. if (material_flags & MATERIAL_FLAG_BACK_FACES)
  250. {
  251. glEnable(GL_CULL_FACE);
  252. glCullFace(GL_FRONT);
  253. }
  254. else
  255. {
  256. glEnable(GL_CULL_FACE);
  257. glCullFace(GL_BACK);
  258. }
  259. }
  260. else if ((material_flags & MATERIAL_FLAG_FRONT_AND_BACK_FACES) != (active_material_flags & MATERIAL_FLAG_FRONT_AND_BACK_FACES))
  261. {
  262. if (material_flags & MATERIAL_FLAG_FRONT_AND_BACK_FACES)
  263. {
  264. glDisable(GL_CULL_FACE);
  265. }
  266. else
  267. {
  268. glEnable(GL_CULL_FACE);
  269. glCullFace(GL_BACK);
  270. }
  271. }
  272. if ((material_flags & MATERIAL_FLAG_X_RAY) != (active_material_flags & MATERIAL_FLAG_X_RAY))
  273. {
  274. if (material_flags & MATERIAL_FLAG_X_RAY)
  275. {
  276. glDisable(GL_DEPTH_TEST);
  277. }
  278. else
  279. {
  280. glEnable(GL_DEPTH_TEST);
  281. }
  282. }
  283. active_material_flags = material_flags;
  284. }
  285. // Switch shaders if necessary
  286. const ::shader_program* shader_program = active_material->get_shader_program();
  287. if (active_shader_program != shader_program)
  288. {
  289. active_shader_program = shader_program;
  290. if (!active_shader_program)
  291. {
  292. continue;
  293. }
  294. // Change shader program
  295. rasterizer->use_program(*active_shader_program);
  296. // Get set of known shader input parameters
  297. if (auto it = parameter_sets.find(active_shader_program); it != parameter_sets.end())
  298. {
  299. parameters = it->second;
  300. }
  301. else
  302. {
  303. parameters = load_parameter_set(active_shader_program);
  304. }
  305. // Upload context-dependent shader parameters
  306. if (parameters->time)
  307. parameters->time->upload(time);
  308. if (parameters->resolution)
  309. parameters->resolution->upload(resolution);
  310. if (parameters->view)
  311. parameters->view->upload(view);
  312. if (parameters->view_projection)
  313. parameters->view_projection->upload(view_projection);
  314. if (parameters->ambient_light_count)
  315. parameters->ambient_light_count->upload(ambient_light_count);
  316. if (parameters->ambient_light_colors)
  317. parameters->ambient_light_colors->upload(0, ambient_light_colors, ambient_light_count);
  318. if (parameters->point_light_count)
  319. parameters->point_light_count->upload(point_light_count);
  320. if (parameters->point_light_colors)
  321. parameters->point_light_colors->upload(0, point_light_colors, point_light_count);
  322. if (parameters->point_light_positions)
  323. parameters->point_light_positions->upload(0, point_light_positions, point_light_count);
  324. if (parameters->point_light_attenuations)
  325. parameters->point_light_attenuations->upload(0, point_light_attenuations, point_light_count);
  326. if (parameters->directional_light_count)
  327. parameters->directional_light_count->upload(directional_light_count);
  328. if (parameters->directional_light_colors)
  329. parameters->directional_light_colors->upload(0, directional_light_colors, directional_light_count);
  330. if (parameters->directional_light_directions)
  331. parameters->directional_light_directions->upload(0, directional_light_directions, directional_light_count);
  332. if (parameters->spotlight_count)
  333. parameters->spotlight_count->upload(spotlight_count);
  334. if (parameters->spotlight_colors)
  335. parameters->spotlight_colors->upload(0, spotlight_colors, spotlight_count);
  336. if (parameters->spotlight_positions)
  337. parameters->spotlight_positions->upload(0, spotlight_positions, spotlight_count);
  338. if (parameters->spotlight_directions)
  339. parameters->spotlight_directions->upload(0, spotlight_directions, spotlight_count);
  340. if (parameters->spotlight_attenuations)
  341. parameters->spotlight_attenuations->upload(0, spotlight_attenuations, spotlight_count);
  342. if (parameters->spotlight_cutoffs)
  343. parameters->spotlight_cutoffs->upload(0, spotlight_cutoffs, spotlight_count);
  344. if (parameters->soft_shadows)
  345. parameters->soft_shadows->upload(soft_shadows_texture);
  346. if (parameters->focal_point)
  347. parameters->focal_point->upload(focal_point);
  348. if (parameters->shadow_map_matrices)
  349. parameters->shadow_map_matrices->upload(0, shadow_map_matrices, 4);
  350. if (parameters->shadow_map_split_distances)
  351. parameters->shadow_map_split_distances->upload(shadow_map_split_distances);
  352. if (parameters->shadow_map && shadow_map)
  353. parameters->shadow_map->upload(shadow_map);
  354. }
  355. // Upload material properties to shader
  356. active_material->upload(context->alpha);
  357. }
  358. // Calculate operation-dependent parameters
  359. model = operation.transform;
  360. model_view_projection = view_projection * model;
  361. model_view = view * model;
  362. normal_model_view = math::transpose(math::inverse(math::resize<3, 3>(model_view)));
  363. // Upload operation-dependent parameters
  364. if (parameters->model)
  365. parameters->model->upload(model);
  366. if (parameters->model_view)
  367. parameters->model_view->upload(model_view);
  368. if (parameters->model_view_projection)
  369. parameters->model_view_projection->upload(model_view_projection);
  370. if (parameters->normal_model_view)
  371. parameters->normal_model_view->upload(normal_model_view);
  372. // Draw geometry
  373. if (operation.instance_count)
  374. rasterizer->draw_arrays_instanced(*operation.vertex_array, operation.drawing_mode, operation.start_index, operation.index_count, operation.instance_count);
  375. else
  376. rasterizer->draw_arrays(*operation.vertex_array, operation.drawing_mode, operation.start_index, operation.index_count);
  377. }
  378. }
  379. void material_pass::set_fallback_material(const material* fallback)
  380. {
  381. this->fallback_material = fallback;
  382. }
  383. void material_pass::set_time_tween(const tween<double>* time)
  384. {
  385. this->time_tween = time;
  386. }
  387. void material_pass::set_focal_point_tween(const tween<float3>* focal_point)
  388. {
  389. this->focal_point_tween = focal_point;
  390. }
  391. const material_pass::parameter_set* material_pass::load_parameter_set(const shader_program* program) const
  392. {
  393. // Allocate a new parameter set
  394. parameter_set* parameters = new parameter_set();
  395. // Connect inputs
  396. parameters->time = program->get_input("time");
  397. parameters->resolution = program->get_input("resolution");
  398. parameters->model = program->get_input("model");
  399. parameters->view = program->get_input("view");
  400. parameters->projection = program->get_input("projection");
  401. parameters->model_view = program->get_input("model_view");
  402. parameters->view_projection = program->get_input("view_projection");
  403. parameters->model_view_projection = program->get_input("model_view_projection");
  404. parameters->normal_model_view = program->get_input("normal_model_view");
  405. parameters->ambient_light_count = program->get_input("ambient_light_count");
  406. parameters->ambient_light_colors = program->get_input("ambient_light_colors");
  407. parameters->point_light_count = program->get_input("point_light_count");
  408. parameters->point_light_colors = program->get_input("point_light_colors");
  409. parameters->point_light_positions = program->get_input("point_light_positions");
  410. parameters->point_light_attenuations = program->get_input("point_light_attenuations");
  411. parameters->directional_light_count = program->get_input("directional_light_count");
  412. parameters->directional_light_colors = program->get_input("directional_light_colors");
  413. parameters->directional_light_directions = program->get_input("directional_light_directions");
  414. parameters->spotlight_count = program->get_input("spotlight_count");
  415. parameters->spotlight_colors = program->get_input("spotlight_colors");
  416. parameters->spotlight_positions = program->get_input("spotlight_positions");
  417. parameters->spotlight_directions = program->get_input("spotlight_directions");
  418. parameters->spotlight_attenuations = program->get_input("spotlight_attenuations");
  419. parameters->spotlight_cutoffs = program->get_input("spotlight_cutoffs");
  420. parameters->soft_shadows = program->get_input("soft_shadows");
  421. parameters->focal_point = program->get_input("focal_point");
  422. parameters->shadow_map_matrices = program->get_input("shadow_map_matrices");
  423. parameters->shadow_map_split_distances = program->get_input("shadow_map_split_distances");
  424. parameters->shadow_map = program->get_input("shadow_map");
  425. // Add parameter set to map of parameter sets
  426. parameter_sets[program] = parameters;
  427. return parameters;
  428. }
  429. bool operation_compare(const render_operation& a, const render_operation& b)
  430. {
  431. if (!a.material)
  432. return false;
  433. else if (!b.material)
  434. return true;
  435. // Determine transparency
  436. bool transparent_a = a.material->get_flags() & MATERIAL_FLAG_TRANSLUCENT;
  437. bool transparent_b = b.material->get_flags() & MATERIAL_FLAG_TRANSLUCENT;
  438. if (transparent_a)
  439. {
  440. if (transparent_b)
  441. {
  442. // A and B are both transparent, render back to front
  443. return (a.depth >= b.depth);
  444. }
  445. else
  446. {
  447. // A is transparent, B is opaque. Render B first
  448. return false;
  449. }
  450. }
  451. else
  452. {
  453. if (transparent_b)
  454. {
  455. // A is opaque, B is transparent. Render A first
  456. return true;
  457. }
  458. else
  459. {
  460. // A and B are both opaque
  461. if (a.material->get_shader_program() == b.material->get_shader_program())
  462. {
  463. // A and B have the same shader
  464. if (a.vertex_array == b.vertex_array)
  465. {
  466. // A and B have the same VAO, render front to back
  467. return (a.depth < b.depth);
  468. }
  469. else
  470. {
  471. // Sort by VAO
  472. return (a.vertex_array < b.vertex_array);
  473. }
  474. }
  475. else
  476. {
  477. // A and B are both opaque and have different shaders, sort by shader
  478. return (a.material->get_shader_program() < b.material->get_shader_program());
  479. }
  480. }
  481. }
  482. }