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

628 lines
21 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. glDisable(GL_STENCIL_TEST);
  104. glStencilMask(0x00);
  105. auto viewport = framebuffer->get_dimensions();
  106. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  107. float2 resolution = {static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))};
  108. float time = (time_tween) ? time_tween->interpolate(context->alpha) : 0.0f;
  109. float3 focal_point = (focal_point_tween) ? focal_point_tween->interpolate(context->alpha) : float3{0, 0, 0};
  110. float4x4 view = context->camera->get_view_tween().interpolate(context->alpha);
  111. float4x4 projection = context->camera->get_projection_tween().interpolate(context->alpha);
  112. float4x4 view_projection = projection * view;
  113. float4x4 model_view_projection;
  114. float4x4 model;
  115. float4x4 model_view;
  116. float3x3 normal_model_view;
  117. int active_material_flags = 0;
  118. const ::shader_program* active_shader_program = nullptr;
  119. const ::material* active_material = nullptr;
  120. const parameter_set* parameters = nullptr;
  121. // Reset light counts
  122. ambient_light_count = 0;
  123. point_light_count = 0;
  124. directional_light_count = 0;
  125. spotlight_count = 0;
  126. // Collect lights
  127. const std::list<scene_object_base*>* lights = context->scene->get_objects(light::object_type_id);
  128. for (const scene_object_base* object: *lights)
  129. {
  130. // Skip inactive lights
  131. if (!object->is_active())
  132. continue;
  133. const ::light* light = static_cast<const ::light*>(object);
  134. switch (light->get_light_type())
  135. {
  136. // Add ambient light
  137. case light_type::ambient:
  138. {
  139. if (ambient_light_count < max_ambient_light_count)
  140. {
  141. ambient_light_colors[ambient_light_count] = light->get_scaled_color_tween().interpolate(context->alpha);
  142. ++ambient_light_count;
  143. }
  144. break;
  145. }
  146. // Add point light
  147. case light_type::point:
  148. {
  149. if (point_light_count < max_point_light_count)
  150. {
  151. point_light_colors[point_light_count] = light->get_scaled_color_tween().interpolate(context->alpha);
  152. // Transform position into view-space
  153. float3 position = light->get_transform_tween().interpolate(context->alpha).translation;
  154. float3 view_space_position = math::resize<3>(view * float4{position.x, position.y, position.z, 1.0f});
  155. point_light_positions[point_light_count] = view_space_position;
  156. point_light_attenuations[point_light_count] = static_cast<const point_light*>(light)->get_attenuation_tween().interpolate(context->alpha);
  157. ++point_light_count;
  158. }
  159. break;
  160. }
  161. // Add directional light
  162. case light_type::directional:
  163. {
  164. if (directional_light_count < max_directional_light_count)
  165. {
  166. directional_light_colors[directional_light_count] = light->get_scaled_color_tween().interpolate(context->alpha);
  167. // Transform direction into view-space
  168. float3 direction = static_cast<const directional_light*>(light)->get_direction_tween().interpolate(context->alpha);
  169. float3 view_space_direction = math::normalize(math::resize<3>(view * math::resize<4>(-direction)));
  170. directional_light_directions[directional_light_count] = view_space_direction;
  171. ++directional_light_count;
  172. }
  173. break;
  174. }
  175. // Add spotlight
  176. case light_type::spot:
  177. {
  178. if (spotlight_count < max_spotlight_count)
  179. {
  180. spotlight_colors[spotlight_count] = light->get_scaled_color_tween().interpolate(context->alpha);
  181. // Transform position into view-space
  182. float3 position = light->get_transform_tween().interpolate(context->alpha).translation;
  183. float3 view_space_position = math::resize<3>(view * float4{position.x, position.y, position.z, 1.0f});
  184. spotlight_positions[spotlight_count] = view_space_position;
  185. const ::spotlight* spotlight = static_cast<const ::spotlight*>(light);
  186. // Transform direction into view-space
  187. float3 direction = spotlight->get_direction_tween().interpolate(context->alpha);
  188. float3 view_space_direction = math::normalize(math::resize<3>(view * float4{-direction.x, -direction.y, -direction.z, 0.0f}));
  189. spotlight_directions[spotlight_count] = view_space_direction;
  190. spotlight_attenuations[spotlight_count] = spotlight->get_attenuation_tween().interpolate(context->alpha);
  191. spotlight_cutoffs[spotlight_count] = spotlight->get_cosine_cutoff_tween().interpolate(context->alpha);
  192. ++spotlight_count;
  193. }
  194. break;
  195. }
  196. default:
  197. break;
  198. }
  199. }
  200. float4x4 shadow_map_matrices[4];
  201. float4 shadow_map_split_distances;
  202. if (shadow_map_pass)
  203. {
  204. for (int i = 0; i < 4; ++i)
  205. shadow_map_matrices[i] = shadow_map_pass->get_shadow_matrices()[i];
  206. // Calculate shadow map split distances
  207. for (int i = 0; i < 4; ++i)
  208. shadow_map_split_distances[i] = shadow_map_pass->get_split_distances()[i + 1];
  209. }
  210. // Sort render operations
  211. context->operations.sort(operation_compare);
  212. for (const render_operation& operation: context->operations)
  213. {
  214. // Get operation material
  215. const ::material* material = operation.material;
  216. if (!material)
  217. {
  218. if (fallback_material)
  219. {
  220. // No material specified, use fallback material
  221. material = fallback_material;
  222. }
  223. else
  224. {
  225. // No material specified and no fallback material, skip operation
  226. continue;
  227. }
  228. }
  229. // Switch materials if necessary
  230. if (active_material != material)
  231. {
  232. active_material = material;
  233. // Change rasterizer state according to material flags
  234. std::uint32_t material_flags = active_material->get_flags();
  235. if (active_material_flags != material_flags)
  236. {
  237. if ((material_flags & MATERIAL_FLAG_TRANSLUCENT) != (active_material_flags & MATERIAL_FLAG_TRANSLUCENT))
  238. {
  239. if (material_flags & MATERIAL_FLAG_TRANSLUCENT)
  240. {
  241. glEnable(GL_BLEND);
  242. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  243. }
  244. else
  245. {
  246. glDisable(GL_BLEND);
  247. }
  248. }
  249. if ((material_flags & MATERIAL_FLAG_BACK_FACES) != (active_material_flags & MATERIAL_FLAG_BACK_FACES))
  250. {
  251. if (material_flags & MATERIAL_FLAG_BACK_FACES)
  252. {
  253. glEnable(GL_CULL_FACE);
  254. glCullFace(GL_FRONT);
  255. }
  256. else
  257. {
  258. glEnable(GL_CULL_FACE);
  259. glCullFace(GL_BACK);
  260. }
  261. }
  262. else if ((material_flags & MATERIAL_FLAG_FRONT_AND_BACK_FACES) != (active_material_flags & MATERIAL_FLAG_FRONT_AND_BACK_FACES))
  263. {
  264. if (material_flags & MATERIAL_FLAG_FRONT_AND_BACK_FACES)
  265. {
  266. glDisable(GL_CULL_FACE);
  267. }
  268. else
  269. {
  270. glEnable(GL_CULL_FACE);
  271. glCullFace(GL_BACK);
  272. }
  273. }
  274. if ((material_flags & MATERIAL_FLAG_X_RAY) != (active_material_flags & MATERIAL_FLAG_X_RAY))
  275. {
  276. if (material_flags & MATERIAL_FLAG_X_RAY)
  277. {
  278. glDisable(GL_DEPTH_TEST);
  279. }
  280. else
  281. {
  282. glEnable(GL_DEPTH_TEST);
  283. }
  284. }
  285. if ((material_flags & MATERIAL_FLAG_DECAL_SURFACE) != (active_material_flags & MATERIAL_FLAG_DECAL_SURFACE))
  286. {
  287. if (material_flags & MATERIAL_FLAG_DECAL_SURFACE)
  288. {
  289. glEnable(GL_STENCIL_TEST);
  290. glStencilFunc(GL_ALWAYS, 1, ~0);
  291. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  292. glStencilMask(~0);
  293. }
  294. else
  295. {
  296. glDisable(GL_STENCIL_TEST);
  297. glStencilMask(0);
  298. }
  299. }
  300. if ((material_flags & MATERIAL_FLAG_DECAL) != (active_material_flags & MATERIAL_FLAG_DECAL))
  301. {
  302. if (material_flags & MATERIAL_FLAG_DECAL)
  303. {
  304. glDisable(GL_DEPTH_TEST);
  305. glEnable(GL_STENCIL_TEST);
  306. glStencilFunc(GL_EQUAL, 1, ~0);
  307. glStencilMask(0);
  308. }
  309. else
  310. {
  311. glEnable(GL_DEPTH_TEST);
  312. glDisable(GL_STENCIL_TEST);
  313. glStencilMask(0);
  314. }
  315. }
  316. /*
  317. if ((material_flags & MATERIAL_FLAG_OUTLINE) != (active_material_flags & MATERIAL_FLAG_OUTLINE))
  318. {
  319. if (material_flags & MATERIAL_FLAG_OUTLINE)
  320. {
  321. glEnable(GL_STENCIL_TEST);
  322. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  323. glStencilFunc(GL_ALWAYS, 2, 0xFF);
  324. glStencilMask(0xFF);
  325. }
  326. else
  327. {
  328. glDisable(GL_STENCIL_TEST);
  329. glStencilMask(0x00);
  330. }
  331. }
  332. */
  333. active_material_flags = material_flags;
  334. }
  335. // Switch shaders if necessary
  336. const ::shader_program* shader_program = active_material->get_shader_program();
  337. if (active_shader_program != shader_program)
  338. {
  339. active_shader_program = shader_program;
  340. if (!active_shader_program)
  341. {
  342. continue;
  343. }
  344. // Change shader program
  345. rasterizer->use_program(*active_shader_program);
  346. // Get set of known shader input parameters
  347. if (auto it = parameter_sets.find(active_shader_program); it != parameter_sets.end())
  348. {
  349. parameters = it->second;
  350. }
  351. else
  352. {
  353. parameters = load_parameter_set(active_shader_program);
  354. }
  355. // Upload context-dependent shader parameters
  356. if (parameters->time)
  357. parameters->time->upload(time);
  358. if (parameters->resolution)
  359. parameters->resolution->upload(resolution);
  360. if (parameters->view)
  361. parameters->view->upload(view);
  362. if (parameters->view_projection)
  363. parameters->view_projection->upload(view_projection);
  364. if (parameters->ambient_light_count)
  365. parameters->ambient_light_count->upload(ambient_light_count);
  366. if (parameters->ambient_light_colors)
  367. parameters->ambient_light_colors->upload(0, ambient_light_colors, ambient_light_count);
  368. if (parameters->point_light_count)
  369. parameters->point_light_count->upload(point_light_count);
  370. if (parameters->point_light_colors)
  371. parameters->point_light_colors->upload(0, point_light_colors, point_light_count);
  372. if (parameters->point_light_positions)
  373. parameters->point_light_positions->upload(0, point_light_positions, point_light_count);
  374. if (parameters->point_light_attenuations)
  375. parameters->point_light_attenuations->upload(0, point_light_attenuations, point_light_count);
  376. if (parameters->directional_light_count)
  377. parameters->directional_light_count->upload(directional_light_count);
  378. if (parameters->directional_light_colors)
  379. parameters->directional_light_colors->upload(0, directional_light_colors, directional_light_count);
  380. if (parameters->directional_light_directions)
  381. parameters->directional_light_directions->upload(0, directional_light_directions, directional_light_count);
  382. if (parameters->spotlight_count)
  383. parameters->spotlight_count->upload(spotlight_count);
  384. if (parameters->spotlight_colors)
  385. parameters->spotlight_colors->upload(0, spotlight_colors, spotlight_count);
  386. if (parameters->spotlight_positions)
  387. parameters->spotlight_positions->upload(0, spotlight_positions, spotlight_count);
  388. if (parameters->spotlight_directions)
  389. parameters->spotlight_directions->upload(0, spotlight_directions, spotlight_count);
  390. if (parameters->spotlight_attenuations)
  391. parameters->spotlight_attenuations->upload(0, spotlight_attenuations, spotlight_count);
  392. if (parameters->spotlight_cutoffs)
  393. parameters->spotlight_cutoffs->upload(0, spotlight_cutoffs, spotlight_count);
  394. if (parameters->soft_shadows)
  395. parameters->soft_shadows->upload(soft_shadows_texture);
  396. if (parameters->focal_point)
  397. parameters->focal_point->upload(focal_point);
  398. if (parameters->shadow_map_matrices)
  399. parameters->shadow_map_matrices->upload(0, shadow_map_matrices, 4);
  400. if (parameters->shadow_map_split_distances)
  401. parameters->shadow_map_split_distances->upload(shadow_map_split_distances);
  402. if (parameters->shadow_map && shadow_map)
  403. parameters->shadow_map->upload(shadow_map);
  404. }
  405. // Upload material properties to shader
  406. active_material->upload(context->alpha);
  407. }
  408. // Calculate operation-dependent parameters
  409. model = operation.transform;
  410. model_view_projection = view_projection * model;
  411. model_view = view * model;
  412. normal_model_view = math::transpose(math::inverse(math::resize<3, 3>(model_view)));
  413. // Upload operation-dependent parameters
  414. if (parameters->model)
  415. parameters->model->upload(model);
  416. if (parameters->model_view)
  417. parameters->model_view->upload(model_view);
  418. if (parameters->model_view_projection)
  419. parameters->model_view_projection->upload(model_view_projection);
  420. if (parameters->normal_model_view)
  421. parameters->normal_model_view->upload(normal_model_view);
  422. // Draw geometry
  423. if (operation.instance_count)
  424. rasterizer->draw_arrays_instanced(*operation.vertex_array, operation.drawing_mode, operation.start_index, operation.index_count, operation.instance_count);
  425. else
  426. rasterizer->draw_arrays(*operation.vertex_array, operation.drawing_mode, operation.start_index, operation.index_count);
  427. }
  428. }
  429. void material_pass::set_fallback_material(const material* fallback)
  430. {
  431. this->fallback_material = fallback;
  432. }
  433. void material_pass::set_time_tween(const tween<double>* time)
  434. {
  435. this->time_tween = time;
  436. }
  437. void material_pass::set_focal_point_tween(const tween<float3>* focal_point)
  438. {
  439. this->focal_point_tween = focal_point;
  440. }
  441. const material_pass::parameter_set* material_pass::load_parameter_set(const shader_program* program) const
  442. {
  443. // Allocate a new parameter set
  444. parameter_set* parameters = new parameter_set();
  445. // Connect inputs
  446. parameters->time = program->get_input("time");
  447. parameters->resolution = program->get_input("resolution");
  448. parameters->model = program->get_input("model");
  449. parameters->view = program->get_input("view");
  450. parameters->projection = program->get_input("projection");
  451. parameters->model_view = program->get_input("model_view");
  452. parameters->view_projection = program->get_input("view_projection");
  453. parameters->model_view_projection = program->get_input("model_view_projection");
  454. parameters->normal_model_view = program->get_input("normal_model_view");
  455. parameters->ambient_light_count = program->get_input("ambient_light_count");
  456. parameters->ambient_light_colors = program->get_input("ambient_light_colors");
  457. parameters->point_light_count = program->get_input("point_light_count");
  458. parameters->point_light_colors = program->get_input("point_light_colors");
  459. parameters->point_light_positions = program->get_input("point_light_positions");
  460. parameters->point_light_attenuations = program->get_input("point_light_attenuations");
  461. parameters->directional_light_count = program->get_input("directional_light_count");
  462. parameters->directional_light_colors = program->get_input("directional_light_colors");
  463. parameters->directional_light_directions = program->get_input("directional_light_directions");
  464. parameters->spotlight_count = program->get_input("spotlight_count");
  465. parameters->spotlight_colors = program->get_input("spotlight_colors");
  466. parameters->spotlight_positions = program->get_input("spotlight_positions");
  467. parameters->spotlight_directions = program->get_input("spotlight_directions");
  468. parameters->spotlight_attenuations = program->get_input("spotlight_attenuations");
  469. parameters->spotlight_cutoffs = program->get_input("spotlight_cutoffs");
  470. parameters->soft_shadows = program->get_input("soft_shadows");
  471. parameters->focal_point = program->get_input("focal_point");
  472. parameters->shadow_map_matrices = program->get_input("shadow_map_matrices");
  473. parameters->shadow_map_split_distances = program->get_input("shadow_map_split_distances");
  474. parameters->shadow_map = program->get_input("shadow_map");
  475. // Add parameter set to map of parameter sets
  476. parameter_sets[program] = parameters;
  477. return parameters;
  478. }
  479. bool operation_compare(const render_operation& a, const render_operation& b)
  480. {
  481. if (!a.material)
  482. return false;
  483. else if (!b.material)
  484. return true;
  485. bool xray_a = a.material->get_flags() & MATERIAL_FLAG_X_RAY;
  486. bool xray_b = b.material->get_flags() & MATERIAL_FLAG_X_RAY;
  487. if (xray_a)
  488. {
  489. if (xray_b)
  490. {
  491. // A and B are both xray, render back to front
  492. return (a.depth >= b.depth);
  493. }
  494. else
  495. {
  496. // A is xray, B is not. Render B first
  497. return false;
  498. }
  499. }
  500. else
  501. {
  502. if (xray_b)
  503. {
  504. // A is opaque, B is xray. Render A first
  505. return true;
  506. }
  507. else
  508. {
  509. // Determine transparency
  510. bool transparent_a = a.material->get_flags() & MATERIAL_FLAG_TRANSLUCENT;
  511. bool transparent_b = b.material->get_flags() & MATERIAL_FLAG_TRANSLUCENT;
  512. if (transparent_a)
  513. {
  514. if (transparent_b)
  515. {
  516. // A and B are both transparent, render back to front
  517. return (a.depth >= b.depth);
  518. }
  519. else
  520. {
  521. // A is transparent, B is opaque. Render B first
  522. return false;
  523. }
  524. }
  525. else
  526. {
  527. if (transparent_b)
  528. {
  529. // A is opaque, B is transparent. Render A first
  530. return true;
  531. }
  532. else
  533. {
  534. // A and B are both opaque
  535. if (a.material->get_shader_program() == b.material->get_shader_program())
  536. {
  537. // A and B have the same shader
  538. if (a.vertex_array == b.vertex_array)
  539. {
  540. // A and B have the same VAO, render front to back
  541. return (a.depth < b.depth);
  542. }
  543. else
  544. {
  545. // Sort by VAO
  546. return (a.vertex_array < b.vertex_array);
  547. }
  548. }
  549. else
  550. {
  551. // A and B are both opaque and have different shaders, sort by shader
  552. return (a.material->get_shader_program() < b.material->get_shader_program());
  553. }
  554. }
  555. }
  556. }
  557. }
  558. }