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

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