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

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