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

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