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

711 lines
24 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/material-pass.hpp"
  20. #include "configuration.hpp"
  21. #include "resources/resource-manager.hpp"
  22. #include "gl/rasterizer.hpp"
  23. #include "gl/framebuffer.hpp"
  24. #include "gl/shader.hpp"
  25. #include "gl/shader-type.hpp"
  26. #include "gl/shader-program.hpp"
  27. #include "gl/shader-input.hpp"
  28. #include "gl/vertex-buffer.hpp"
  29. #include "gl/vertex-array.hpp"
  30. #include "gl/vertex-attribute-type.hpp"
  31. #include "gl/drawing-mode.hpp"
  32. #include "gl/texture-2d.hpp"
  33. #include "gl/texture-wrapping.hpp"
  34. #include "gl/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/collection.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 "configuration.hpp"
  46. #include "math/math.hpp"
  47. #include <cmath>
  48. #include <glad/glad.h>
  49. #include "shadow-map-pass.hpp"
  50. static bool operation_compare(const render_operation& a, const render_operation& b);
  51. material_pass::material_pass(gl::rasterizer* rasterizer, const gl::framebuffer* framebuffer, resource_manager* resource_manager):
  52. render_pass(rasterizer, framebuffer),
  53. fallback_material(nullptr),
  54. time_tween(nullptr),
  55. mouse_position({0.0f, 0.0f}),
  56. focal_point_tween(nullptr),
  57. shadow_map_pass(nullptr),
  58. shadow_map(nullptr),
  59. shadow_strength(1.0f)
  60. {
  61. max_ambient_light_count = MATERIAL_PASS_MAX_AMBIENT_LIGHT_COUNT;
  62. max_point_light_count = MATERIAL_PASS_MAX_POINT_LIGHT_COUNT;
  63. max_directional_light_count = MATERIAL_PASS_MAX_DIRECTIONAL_LIGHT_COUNT;
  64. max_spotlight_count = MATERIAL_PASS_MAX_SPOTLIGHT_COUNT;
  65. ambient_light_colors = new float3[max_ambient_light_count];
  66. point_light_colors = new float3[max_point_light_count];
  67. point_light_positions = new float3[max_point_light_count];
  68. point_light_attenuations = new float3[max_point_light_count];
  69. directional_light_colors = new float3[max_directional_light_count];
  70. directional_light_directions = new float3[max_directional_light_count];
  71. directional_light_matrices = new float4x4[max_directional_light_count];
  72. directional_light_textures = new const gl::texture_2d*[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[] directional_light_matrices;
  88. delete[] directional_light_textures;
  89. delete[] spotlight_colors;
  90. delete[] spotlight_positions;
  91. delete[] spotlight_directions;
  92. delete[] spotlight_attenuations;
  93. delete[] spotlight_cutoffs;
  94. }
  95. void material_pass::render(render_context* context) const
  96. {
  97. rasterizer->use_framebuffer(*framebuffer);
  98. glDisable(GL_BLEND);
  99. glEnable(GL_DEPTH_TEST);
  100. glDepthMask(GL_TRUE);
  101. glDepthFunc(GL_GREATER);
  102. glEnable(GL_CULL_FACE);
  103. glCullFace(GL_BACK);
  104. glDisable(GL_STENCIL_TEST);
  105. glStencilMask(0x00);
  106. // For half-z buffer
  107. glDepthRange(-1.0f, 1.0f);
  108. auto viewport = framebuffer->get_dimensions();
  109. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  110. float2 resolution = {static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))};
  111. float time = (time_tween) ? time_tween->interpolate(context->alpha) : 0.0f;
  112. float3 focal_point = (focal_point_tween) ? focal_point_tween->interpolate(context->alpha) : float3{0, 0, 0};
  113. float4x4 view = context->camera->get_view_tween().interpolate(context->alpha);
  114. float4x4 projection = context->camera->get_projection_tween().interpolate(context->alpha);
  115. float4x4 view_projection = projection * view;
  116. float4x4 model_view_projection;
  117. float4x4 model;
  118. float4x4 model_view;
  119. float3x3 normal_model_view;
  120. float2 clip_depth;
  121. clip_depth[0] = context->camera->get_clip_near_tween().interpolate(context->alpha);
  122. clip_depth[1] = context->camera->get_clip_far_tween().interpolate(context->alpha);
  123. float log_depth_coef = 2.0f / std::log2(clip_depth[1] + 1.0f);
  124. int active_material_flags = 0;
  125. const gl::shader_program* active_shader_program = nullptr;
  126. const ::material* active_material = nullptr;
  127. const parameter_set* parameters = nullptr;
  128. // Reset light counts
  129. ambient_light_count = 0;
  130. point_light_count = 0;
  131. directional_light_count = 0;
  132. spotlight_count = 0;
  133. // Collect lights
  134. const std::list<scene::object_base*>* lights = context->collection->get_objects(scene::light::object_type_id);
  135. for (const scene::object_base* object: *lights)
  136. {
  137. // Skip inactive lights
  138. if (!object->is_active())
  139. continue;
  140. const scene::light* light = static_cast<const scene::light*>(object);
  141. switch (light->get_light_type())
  142. {
  143. // Add ambient light
  144. case scene::light_type::ambient:
  145. {
  146. if (ambient_light_count < max_ambient_light_count)
  147. {
  148. ambient_light_colors[ambient_light_count] = light->get_scaled_color_tween().interpolate(context->alpha);
  149. ++ambient_light_count;
  150. }
  151. break;
  152. }
  153. // Add point light
  154. case scene::light_type::point:
  155. {
  156. if (point_light_count < max_point_light_count)
  157. {
  158. point_light_colors[point_light_count] = light->get_scaled_color_tween().interpolate(context->alpha);
  159. // Transform position into view-space
  160. float3 position = light->get_transform_tween().interpolate(context->alpha).translation;
  161. float3 view_space_position = math::resize<3>(view * float4{position.x, position.y, position.z, 1.0f});
  162. point_light_positions[point_light_count] = view_space_position;
  163. point_light_attenuations[point_light_count] = static_cast<const scene::point_light*>(light)->get_attenuation_tween().interpolate(context->alpha);
  164. ++point_light_count;
  165. }
  166. break;
  167. }
  168. // Add directional light
  169. case scene::light_type::directional:
  170. {
  171. if (directional_light_count < max_directional_light_count)
  172. {
  173. const scene::directional_light* directional_light = static_cast<const scene::directional_light*>(light);
  174. directional_light_colors[directional_light_count] = light->get_scaled_color_tween().interpolate(context->alpha);
  175. // Transform direction into view-space
  176. float3 direction = static_cast<const scene::directional_light*>(light)->get_direction_tween().interpolate(context->alpha);
  177. float3 view_space_direction = math::normalize(math::resize<3>(view * math::resize<4>(-direction)));
  178. directional_light_directions[directional_light_count] = view_space_direction;
  179. // Calculate a view-projection matrix from the directional light's transform
  180. math::transform<float> light_transform = light->get_transform_tween().interpolate(context->alpha);
  181. float3 forward = light_transform.rotation * global_forward;
  182. float3 up = light_transform.rotation * global_up;
  183. float4x4 light_view = math::look_at(light_transform.translation, light_transform.translation + forward, up);
  184. float4x4 light_projection = math::ortho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
  185. float4x4 bias = math::translate(math::identity4x4<float>, {0.5f, 0.5f, 0.5f}) * math::scale(math::identity4x4<float>, {0.5f, 0.5f, 0.5f});
  186. //float scale_u = 1.0f;
  187. //float scale_v = 1.0f;
  188. //float4x4 light_projection = math::ortho(-scale_u, scale_u, -scale_v, scale_v, -1.0f, 1.0f);
  189. directional_light_matrices[directional_light_count] = bias * light_projection * light_view;
  190. directional_light_textures[directional_light_count] = directional_light->get_light_texture();
  191. ++directional_light_count;
  192. }
  193. break;
  194. }
  195. // Add spotlight
  196. case scene::light_type::spot:
  197. {
  198. if (spotlight_count < max_spotlight_count)
  199. {
  200. spotlight_colors[spotlight_count] = light->get_scaled_color_tween().interpolate(context->alpha);
  201. // Transform position into view-space
  202. float3 position = light->get_transform_tween().interpolate(context->alpha).translation;
  203. float3 view_space_position = math::resize<3>(view * float4{position.x, position.y, position.z, 1.0f});
  204. spotlight_positions[spotlight_count] = view_space_position;
  205. const scene::spotlight* spotlight = static_cast<const scene::spotlight*>(light);
  206. // Transform direction into view-space
  207. float3 direction = spotlight->get_direction_tween().interpolate(context->alpha);
  208. float3 view_space_direction = math::normalize(math::resize<3>(view * float4{-direction.x, -direction.y, -direction.z, 0.0f}));
  209. spotlight_directions[spotlight_count] = view_space_direction;
  210. spotlight_attenuations[spotlight_count] = spotlight->get_attenuation_tween().interpolate(context->alpha);
  211. spotlight_cutoffs[spotlight_count] = spotlight->get_cosine_cutoff_tween().interpolate(context->alpha);
  212. ++spotlight_count;
  213. }
  214. break;
  215. }
  216. default:
  217. break;
  218. }
  219. }
  220. float4x4 shadow_map_matrices[4];
  221. float4 shadow_map_split_distances;
  222. if (shadow_map_pass)
  223. {
  224. for (int i = 0; i < 4; ++i)
  225. shadow_map_matrices[i] = shadow_map_pass->get_shadow_matrices()[i];
  226. // Calculate shadow map split distances
  227. for (int i = 0; i < 4; ++i)
  228. shadow_map_split_distances[i] = shadow_map_pass->get_split_distances()[i + 1];
  229. }
  230. // Sort render operations
  231. context->operations.sort(operation_compare);
  232. for (const render_operation& operation: context->operations)
  233. {
  234. // Get operation material
  235. const ::material* material = operation.material;
  236. if (!material)
  237. {
  238. if (fallback_material)
  239. {
  240. // No material specified, use fallback material
  241. material = fallback_material;
  242. }
  243. else
  244. {
  245. // No material specified and no fallback material, skip operation
  246. continue;
  247. }
  248. }
  249. // Switch materials if necessary
  250. if (active_material != material)
  251. {
  252. active_material = material;
  253. // Change rasterizer state according to material flags
  254. std::uint32_t material_flags = active_material->get_flags();
  255. if (active_material_flags != material_flags)
  256. {
  257. if ((material_flags & MATERIAL_FLAG_TRANSLUCENT) != (active_material_flags & MATERIAL_FLAG_TRANSLUCENT))
  258. {
  259. if (material_flags & MATERIAL_FLAG_TRANSLUCENT)
  260. {
  261. glEnable(GL_BLEND);
  262. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  263. }
  264. else
  265. {
  266. glDisable(GL_BLEND);
  267. }
  268. }
  269. if ((material_flags & MATERIAL_FLAG_BACK_FACES) != (active_material_flags & MATERIAL_FLAG_BACK_FACES))
  270. {
  271. if (material_flags & MATERIAL_FLAG_BACK_FACES)
  272. {
  273. glEnable(GL_CULL_FACE);
  274. glCullFace(GL_FRONT);
  275. }
  276. else
  277. {
  278. glEnable(GL_CULL_FACE);
  279. glCullFace(GL_BACK);
  280. }
  281. }
  282. else if ((material_flags & MATERIAL_FLAG_FRONT_AND_BACK_FACES) != (active_material_flags & MATERIAL_FLAG_FRONT_AND_BACK_FACES))
  283. {
  284. if (material_flags & MATERIAL_FLAG_FRONT_AND_BACK_FACES)
  285. {
  286. glDisable(GL_CULL_FACE);
  287. }
  288. else
  289. {
  290. glEnable(GL_CULL_FACE);
  291. glCullFace(GL_BACK);
  292. }
  293. }
  294. if ((material_flags & MATERIAL_FLAG_X_RAY) != (active_material_flags & MATERIAL_FLAG_X_RAY))
  295. {
  296. if (material_flags & MATERIAL_FLAG_X_RAY)
  297. {
  298. glDisable(GL_DEPTH_TEST);
  299. }
  300. else
  301. {
  302. glEnable(GL_DEPTH_TEST);
  303. }
  304. }
  305. if ((material_flags & MATERIAL_FLAG_DECAL_SURFACE) != (active_material_flags & MATERIAL_FLAG_DECAL_SURFACE))
  306. {
  307. if (material_flags & MATERIAL_FLAG_DECAL_SURFACE)
  308. {
  309. glEnable(GL_STENCIL_TEST);
  310. glStencilFunc(GL_ALWAYS, 1, ~0);
  311. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  312. glStencilMask(~0);
  313. }
  314. else
  315. {
  316. glDisable(GL_STENCIL_TEST);
  317. glStencilMask(0);
  318. }
  319. }
  320. if ((material_flags & MATERIAL_FLAG_DECAL) != (active_material_flags & MATERIAL_FLAG_DECAL))
  321. {
  322. if (material_flags & MATERIAL_FLAG_DECAL)
  323. {
  324. glEnable(GL_DEPTH_TEST);
  325. glDepthFunc(GL_GEQUAL);
  326. glDepthMask(GL_FALSE);
  327. glEnable(GL_STENCIL_TEST);
  328. glStencilFunc(GL_EQUAL, 1, ~0);
  329. //glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO);
  330. //glStencilMask(~0);
  331. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  332. glStencilMask(0);
  333. }
  334. else
  335. {
  336. glEnable(GL_DEPTH_TEST);
  337. glDepthFunc(GL_GREATER);
  338. glDepthMask(GL_TRUE);
  339. glDisable(GL_STENCIL_TEST);
  340. glStencilMask(0);
  341. }
  342. }
  343. /*
  344. if ((material_flags & MATERIAL_FLAG_OUTLINE) != (active_material_flags & MATERIAL_FLAG_OUTLINE))
  345. {
  346. if (material_flags & MATERIAL_FLAG_OUTLINE)
  347. {
  348. glEnable(GL_STENCIL_TEST);
  349. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  350. glStencilFunc(GL_ALWAYS, 2, 0xFF);
  351. glStencilMask(0xFF);
  352. }
  353. else
  354. {
  355. glDisable(GL_STENCIL_TEST);
  356. glStencilMask(0x00);
  357. }
  358. }
  359. */
  360. active_material_flags = material_flags;
  361. }
  362. // Switch shaders if necessary
  363. const gl::shader_program* shader_program = active_material->get_shader_program();
  364. if (active_shader_program != shader_program)
  365. {
  366. active_shader_program = shader_program;
  367. if (!active_shader_program)
  368. {
  369. continue;
  370. }
  371. // Change shader program
  372. rasterizer->use_program(*active_shader_program);
  373. // Get set of known shader input parameters
  374. if (auto it = parameter_sets.find(active_shader_program); it != parameter_sets.end())
  375. {
  376. parameters = it->second;
  377. }
  378. else
  379. {
  380. parameters = load_parameter_set(active_shader_program);
  381. }
  382. // Upload context-dependent shader parameters
  383. if (parameters->time)
  384. parameters->time->upload(time);
  385. if (parameters->mouse)
  386. parameters->mouse->upload(mouse_position);
  387. if (parameters->resolution)
  388. parameters->resolution->upload(resolution);
  389. if (parameters->view)
  390. parameters->view->upload(view);
  391. if (parameters->view_projection)
  392. parameters->view_projection->upload(view_projection);
  393. if (parameters->ambient_light_count)
  394. parameters->ambient_light_count->upload(ambient_light_count);
  395. if (parameters->ambient_light_colors)
  396. parameters->ambient_light_colors->upload(0, ambient_light_colors, ambient_light_count);
  397. if (parameters->point_light_count)
  398. parameters->point_light_count->upload(point_light_count);
  399. if (parameters->point_light_colors)
  400. parameters->point_light_colors->upload(0, point_light_colors, point_light_count);
  401. if (parameters->point_light_positions)
  402. parameters->point_light_positions->upload(0, point_light_positions, point_light_count);
  403. if (parameters->point_light_attenuations)
  404. parameters->point_light_attenuations->upload(0, point_light_attenuations, point_light_count);
  405. if (parameters->directional_light_count)
  406. parameters->directional_light_count->upload(directional_light_count);
  407. if (parameters->directional_light_colors)
  408. parameters->directional_light_colors->upload(0, directional_light_colors, directional_light_count);
  409. if (parameters->directional_light_directions)
  410. parameters->directional_light_directions->upload(0, directional_light_directions, directional_light_count);
  411. if (parameters->directional_light_matrices)
  412. parameters->directional_light_matrices->upload(0, directional_light_matrices, directional_light_count);
  413. if (parameters->directional_light_textures)
  414. parameters->directional_light_textures->upload(0, directional_light_textures, directional_light_count);
  415. if (parameters->spotlight_count)
  416. parameters->spotlight_count->upload(spotlight_count);
  417. if (parameters->spotlight_colors)
  418. parameters->spotlight_colors->upload(0, spotlight_colors, spotlight_count);
  419. if (parameters->spotlight_positions)
  420. parameters->spotlight_positions->upload(0, spotlight_positions, spotlight_count);
  421. if (parameters->spotlight_directions)
  422. parameters->spotlight_directions->upload(0, spotlight_directions, spotlight_count);
  423. if (parameters->spotlight_attenuations)
  424. parameters->spotlight_attenuations->upload(0, spotlight_attenuations, spotlight_count);
  425. if (parameters->spotlight_cutoffs)
  426. parameters->spotlight_cutoffs->upload(0, spotlight_cutoffs, spotlight_count);
  427. if (parameters->focal_point)
  428. parameters->focal_point->upload(focal_point);
  429. if (parameters->shadow_map_matrices)
  430. parameters->shadow_map_matrices->upload(0, shadow_map_matrices, 4);
  431. if (parameters->shadow_map_split_distances)
  432. parameters->shadow_map_split_distances->upload(shadow_map_split_distances);
  433. if (parameters->shadow_map && shadow_map)
  434. parameters->shadow_map->upload(shadow_map);
  435. if (parameters->shadow_strength)
  436. parameters->shadow_strength->upload(shadow_strength);
  437. }
  438. // Upload material properties to shader
  439. active_material->upload(context->alpha);
  440. }
  441. // Calculate operation-dependent parameters
  442. model = operation.transform;
  443. model_view_projection = view_projection * model;
  444. model_view = view * model;
  445. normal_model_view = math::transpose(math::inverse(math::resize<3, 3>(model_view)));
  446. // Upload operation-dependent parameters
  447. if (parameters->model)
  448. parameters->model->upload(model);
  449. if (parameters->model_view)
  450. parameters->model_view->upload(model_view);
  451. if (parameters->model_view_projection)
  452. parameters->model_view_projection->upload(model_view_projection);
  453. if (parameters->normal_model_view)
  454. parameters->normal_model_view->upload(normal_model_view);
  455. if (parameters->clip_depth)
  456. parameters->clip_depth->upload(clip_depth);
  457. if (parameters->log_depth_coef)
  458. parameters->log_depth_coef->upload(log_depth_coef);
  459. // Draw geometry
  460. if (operation.instance_count)
  461. rasterizer->draw_arrays_instanced(*operation.vertex_array, operation.drawing_mode, operation.start_index, operation.index_count, operation.instance_count);
  462. else
  463. rasterizer->draw_arrays(*operation.vertex_array, operation.drawing_mode, operation.start_index, operation.index_count);
  464. }
  465. }
  466. void material_pass::set_fallback_material(const material* fallback)
  467. {
  468. this->fallback_material = fallback;
  469. }
  470. void material_pass::set_time_tween(const tween<double>* time)
  471. {
  472. this->time_tween = time;
  473. }
  474. void material_pass::set_shadow_strength(float strength)
  475. {
  476. this->shadow_strength = strength;
  477. }
  478. void material_pass::set_focal_point_tween(const tween<float3>* focal_point)
  479. {
  480. this->focal_point_tween = focal_point;
  481. }
  482. const material_pass::parameter_set* material_pass::load_parameter_set(const gl::shader_program* program) const
  483. {
  484. // Allocate a new parameter set
  485. parameter_set* parameters = new parameter_set();
  486. // Connect inputs
  487. parameters->time = program->get_input("time");
  488. parameters->mouse = program->get_input("mouse");
  489. parameters->resolution = program->get_input("resolution");
  490. parameters->model = program->get_input("model");
  491. parameters->view = program->get_input("view");
  492. parameters->projection = program->get_input("projection");
  493. parameters->model_view = program->get_input("model_view");
  494. parameters->view_projection = program->get_input("view_projection");
  495. parameters->model_view_projection = program->get_input("model_view_projection");
  496. parameters->normal_model_view = program->get_input("normal_model_view");
  497. parameters->clip_depth = program->get_input("clip_depth");
  498. parameters->log_depth_coef = program->get_input("log_depth_coef");
  499. parameters->ambient_light_count = program->get_input("ambient_light_count");
  500. parameters->ambient_light_colors = program->get_input("ambient_light_colors");
  501. parameters->point_light_count = program->get_input("point_light_count");
  502. parameters->point_light_colors = program->get_input("point_light_colors");
  503. parameters->point_light_positions = program->get_input("point_light_positions");
  504. parameters->point_light_attenuations = program->get_input("point_light_attenuations");
  505. parameters->directional_light_count = program->get_input("directional_light_count");
  506. parameters->directional_light_colors = program->get_input("directional_light_colors");
  507. parameters->directional_light_directions = program->get_input("directional_light_directions");
  508. parameters->directional_light_matrices = program->get_input("directional_light_matrices");
  509. parameters->directional_light_textures = program->get_input("directional_light_textures");
  510. parameters->spotlight_count = program->get_input("spotlight_count");
  511. parameters->spotlight_colors = program->get_input("spotlight_colors");
  512. parameters->spotlight_positions = program->get_input("spotlight_positions");
  513. parameters->spotlight_directions = program->get_input("spotlight_directions");
  514. parameters->spotlight_attenuations = program->get_input("spotlight_attenuations");
  515. parameters->spotlight_cutoffs = program->get_input("spotlight_cutoffs");
  516. parameters->focal_point = program->get_input("focal_point");
  517. parameters->shadow_map_matrices = program->get_input("shadow_map_matrices");
  518. parameters->shadow_map_split_distances = program->get_input("shadow_map_split_distances");
  519. parameters->shadow_map = program->get_input("shadow_map");
  520. parameters->shadow_strength = program->get_input("shadow_strength");
  521. // Add parameter set to map of parameter sets
  522. parameter_sets[program] = parameters;
  523. return parameters;
  524. }
  525. void material_pass::handle_event(const mouse_moved_event& event)
  526. {
  527. mouse_position = {static_cast<float>(event.x), static_cast<float>(event.y)};
  528. }
  529. bool operation_compare(const render_operation& a, const render_operation& b)
  530. {
  531. if (!a.material)
  532. return false;
  533. else if (!b.material)
  534. return true;
  535. bool xray_a = a.material->get_flags() & MATERIAL_FLAG_X_RAY;
  536. bool xray_b = b.material->get_flags() & MATERIAL_FLAG_X_RAY;
  537. if (xray_a)
  538. {
  539. if (xray_b)
  540. {
  541. // A and B are both xray, render back to front
  542. return (a.depth >= b.depth);
  543. }
  544. else
  545. {
  546. // A is xray, B is not. Render B first
  547. return false;
  548. }
  549. }
  550. else
  551. {
  552. if (xray_b)
  553. {
  554. // A is opaque, B is xray. Render A first
  555. return true;
  556. }
  557. else
  558. {
  559. // Determine transparency
  560. bool transparent_a = a.material->get_flags() & MATERIAL_FLAG_TRANSLUCENT;
  561. bool transparent_b = b.material->get_flags() & MATERIAL_FLAG_TRANSLUCENT;
  562. if (transparent_a)
  563. {
  564. if (transparent_b)
  565. {
  566. // Determine decal status
  567. bool decal_a = a.material->get_flags() & MATERIAL_FLAG_DECAL;
  568. bool decal_b = b.material->get_flags() & MATERIAL_FLAG_DECAL;
  569. if (decal_a)
  570. {
  571. if (decal_b)
  572. {
  573. // A and B are both transparent decals, render back to front
  574. return (a.depth >= b.depth);
  575. }
  576. else
  577. {
  578. // A is a transparent decal, B is transparent but not a decal, render A first
  579. return true;
  580. }
  581. }
  582. else
  583. {
  584. if (decal_b)
  585. {
  586. // A is transparent but not a decal, B is a transparent decal, render B first
  587. return false;
  588. }
  589. else
  590. {
  591. // A and B are both transparent, but not decals, render back to front
  592. return (a.depth >= b.depth);
  593. }
  594. }
  595. }
  596. else
  597. {
  598. // A is transparent, B is opaque. Render B first
  599. return false;
  600. }
  601. }
  602. else
  603. {
  604. if (transparent_b)
  605. {
  606. // A is opaque, B is transparent. Render A first
  607. return true;
  608. }
  609. else
  610. {
  611. // A and B are both opaque
  612. if (a.material->get_shader_program() == b.material->get_shader_program())
  613. {
  614. // A and B have the same shader
  615. if (a.vertex_array == b.vertex_array)
  616. {
  617. // A and B have the same VAO, render front to back
  618. return (a.depth < b.depth);
  619. }
  620. else
  621. {
  622. // Sort by VAO
  623. return (a.vertex_array < b.vertex_array);
  624. }
  625. }
  626. else
  627. {
  628. // A and B are both opaque and have different shaders, sort by shader
  629. return (a.material->get_shader_program() < b.material->get_shader_program());
  630. }
  631. }
  632. }
  633. }
  634. }
  635. }