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

565 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. 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 * math::resize<4>(-direction)));
  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_OUTLINE) != (active_material_flags & MATERIAL_FLAG_OUTLINE))
  286. {
  287. if (material_flags & MATERIAL_FLAG_OUTLINE)
  288. {
  289. glEnable(GL_STENCIL_TEST);
  290. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  291. glStencilFunc(GL_ALWAYS, 1, 0xFF);
  292. glStencilMask(0xFF);
  293. }
  294. else
  295. {
  296. glDisable(GL_STENCIL_TEST);
  297. glStencilMask(0x00);
  298. }
  299. }
  300. active_material_flags = material_flags;
  301. }
  302. // Switch shaders if necessary
  303. const ::shader_program* shader_program = active_material->get_shader_program();
  304. if (active_shader_program != shader_program)
  305. {
  306. active_shader_program = shader_program;
  307. if (!active_shader_program)
  308. {
  309. continue;
  310. }
  311. // Change shader program
  312. rasterizer->use_program(*active_shader_program);
  313. // Get set of known shader input parameters
  314. if (auto it = parameter_sets.find(active_shader_program); it != parameter_sets.end())
  315. {
  316. parameters = it->second;
  317. }
  318. else
  319. {
  320. parameters = load_parameter_set(active_shader_program);
  321. }
  322. // Upload context-dependent shader parameters
  323. if (parameters->time)
  324. parameters->time->upload(time);
  325. if (parameters->resolution)
  326. parameters->resolution->upload(resolution);
  327. if (parameters->view)
  328. parameters->view->upload(view);
  329. if (parameters->view_projection)
  330. parameters->view_projection->upload(view_projection);
  331. if (parameters->ambient_light_count)
  332. parameters->ambient_light_count->upload(ambient_light_count);
  333. if (parameters->ambient_light_colors)
  334. parameters->ambient_light_colors->upload(0, ambient_light_colors, ambient_light_count);
  335. if (parameters->point_light_count)
  336. parameters->point_light_count->upload(point_light_count);
  337. if (parameters->point_light_colors)
  338. parameters->point_light_colors->upload(0, point_light_colors, point_light_count);
  339. if (parameters->point_light_positions)
  340. parameters->point_light_positions->upload(0, point_light_positions, point_light_count);
  341. if (parameters->point_light_attenuations)
  342. parameters->point_light_attenuations->upload(0, point_light_attenuations, point_light_count);
  343. if (parameters->directional_light_count)
  344. parameters->directional_light_count->upload(directional_light_count);
  345. if (parameters->directional_light_colors)
  346. parameters->directional_light_colors->upload(0, directional_light_colors, directional_light_count);
  347. if (parameters->directional_light_directions)
  348. parameters->directional_light_directions->upload(0, directional_light_directions, directional_light_count);
  349. if (parameters->spotlight_count)
  350. parameters->spotlight_count->upload(spotlight_count);
  351. if (parameters->spotlight_colors)
  352. parameters->spotlight_colors->upload(0, spotlight_colors, spotlight_count);
  353. if (parameters->spotlight_positions)
  354. parameters->spotlight_positions->upload(0, spotlight_positions, spotlight_count);
  355. if (parameters->spotlight_directions)
  356. parameters->spotlight_directions->upload(0, spotlight_directions, spotlight_count);
  357. if (parameters->spotlight_attenuations)
  358. parameters->spotlight_attenuations->upload(0, spotlight_attenuations, spotlight_count);
  359. if (parameters->spotlight_cutoffs)
  360. parameters->spotlight_cutoffs->upload(0, spotlight_cutoffs, spotlight_count);
  361. if (parameters->soft_shadows)
  362. parameters->soft_shadows->upload(soft_shadows_texture);
  363. if (parameters->focal_point)
  364. parameters->focal_point->upload(focal_point);
  365. if (parameters->shadow_map_matrices)
  366. parameters->shadow_map_matrices->upload(0, shadow_map_matrices, 4);
  367. if (parameters->shadow_map_split_distances)
  368. parameters->shadow_map_split_distances->upload(shadow_map_split_distances);
  369. if (parameters->shadow_map && shadow_map)
  370. parameters->shadow_map->upload(shadow_map);
  371. }
  372. // Upload material properties to shader
  373. active_material->upload(context->alpha);
  374. }
  375. // Calculate operation-dependent parameters
  376. model = operation.transform;
  377. model_view_projection = view_projection * model;
  378. model_view = view * model;
  379. normal_model_view = math::transpose(math::inverse(math::resize<3, 3>(model_view)));
  380. // Upload operation-dependent parameters
  381. if (parameters->model)
  382. parameters->model->upload(model);
  383. if (parameters->model_view)
  384. parameters->model_view->upload(model_view);
  385. if (parameters->model_view_projection)
  386. parameters->model_view_projection->upload(model_view_projection);
  387. if (parameters->normal_model_view)
  388. parameters->normal_model_view->upload(normal_model_view);
  389. // Draw geometry
  390. if (operation.instance_count)
  391. rasterizer->draw_arrays_instanced(*operation.vertex_array, operation.drawing_mode, operation.start_index, operation.index_count, operation.instance_count);
  392. else
  393. rasterizer->draw_arrays(*operation.vertex_array, operation.drawing_mode, operation.start_index, operation.index_count);
  394. }
  395. }
  396. void material_pass::set_fallback_material(const material* fallback)
  397. {
  398. this->fallback_material = fallback;
  399. }
  400. void material_pass::set_time_tween(const tween<double>* time)
  401. {
  402. this->time_tween = time;
  403. }
  404. void material_pass::set_focal_point_tween(const tween<float3>* focal_point)
  405. {
  406. this->focal_point_tween = focal_point;
  407. }
  408. const material_pass::parameter_set* material_pass::load_parameter_set(const shader_program* program) const
  409. {
  410. // Allocate a new parameter set
  411. parameter_set* parameters = new parameter_set();
  412. // Connect inputs
  413. parameters->time = program->get_input("time");
  414. parameters->resolution = program->get_input("resolution");
  415. parameters->model = program->get_input("model");
  416. parameters->view = program->get_input("view");
  417. parameters->projection = program->get_input("projection");
  418. parameters->model_view = program->get_input("model_view");
  419. parameters->view_projection = program->get_input("view_projection");
  420. parameters->model_view_projection = program->get_input("model_view_projection");
  421. parameters->normal_model_view = program->get_input("normal_model_view");
  422. parameters->ambient_light_count = program->get_input("ambient_light_count");
  423. parameters->ambient_light_colors = program->get_input("ambient_light_colors");
  424. parameters->point_light_count = program->get_input("point_light_count");
  425. parameters->point_light_colors = program->get_input("point_light_colors");
  426. parameters->point_light_positions = program->get_input("point_light_positions");
  427. parameters->point_light_attenuations = program->get_input("point_light_attenuations");
  428. parameters->directional_light_count = program->get_input("directional_light_count");
  429. parameters->directional_light_colors = program->get_input("directional_light_colors");
  430. parameters->directional_light_directions = program->get_input("directional_light_directions");
  431. parameters->spotlight_count = program->get_input("spotlight_count");
  432. parameters->spotlight_colors = program->get_input("spotlight_colors");
  433. parameters->spotlight_positions = program->get_input("spotlight_positions");
  434. parameters->spotlight_directions = program->get_input("spotlight_directions");
  435. parameters->spotlight_attenuations = program->get_input("spotlight_attenuations");
  436. parameters->spotlight_cutoffs = program->get_input("spotlight_cutoffs");
  437. parameters->soft_shadows = program->get_input("soft_shadows");
  438. parameters->focal_point = program->get_input("focal_point");
  439. parameters->shadow_map_matrices = program->get_input("shadow_map_matrices");
  440. parameters->shadow_map_split_distances = program->get_input("shadow_map_split_distances");
  441. parameters->shadow_map = program->get_input("shadow_map");
  442. // Add parameter set to map of parameter sets
  443. parameter_sets[program] = parameters;
  444. return parameters;
  445. }
  446. bool operation_compare(const render_operation& a, const render_operation& b)
  447. {
  448. if (!a.material)
  449. return false;
  450. else if (!b.material)
  451. return true;
  452. // Determine transparency
  453. bool transparent_a = a.material->get_flags() & MATERIAL_FLAG_TRANSLUCENT;
  454. bool transparent_b = b.material->get_flags() & MATERIAL_FLAG_TRANSLUCENT;
  455. if (transparent_a)
  456. {
  457. if (transparent_b)
  458. {
  459. // A and B are both transparent, render back to front
  460. return (a.depth >= b.depth);
  461. }
  462. else
  463. {
  464. // A is transparent, B is opaque. Render B first
  465. return false;
  466. }
  467. }
  468. else
  469. {
  470. if (transparent_b)
  471. {
  472. // A is opaque, B is transparent. Render A first
  473. return true;
  474. }
  475. else
  476. {
  477. // A and B are both opaque
  478. if (a.material->get_shader_program() == b.material->get_shader_program())
  479. {
  480. // A and B have the same shader
  481. if (a.vertex_array == b.vertex_array)
  482. {
  483. // A and B have the same VAO, render front to back
  484. return (a.depth < b.depth);
  485. }
  486. else
  487. {
  488. // Sort by VAO
  489. return (a.vertex_array < b.vertex_array);
  490. }
  491. }
  492. else
  493. {
  494. // A and B are both opaque and have different shaders, sort by shader
  495. return (a.material->get_shader_program() < b.material->get_shader_program());
  496. }
  497. }
  498. }
  499. }