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

664 lines
22 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "renderer/passes/material-pass.hpp"
  20. #include "configuration.hpp"
  21. #include "resources/resource-manager.hpp"
  22. #include "rasterizer/rasterizer.hpp"
  23. #include "rasterizer/framebuffer.hpp"
  24. #include "rasterizer/shader.hpp"
  25. #include "rasterizer/shader-type.hpp"
  26. #include "rasterizer/shader-program.hpp"
  27. #include "rasterizer/shader-input.hpp"
  28. #include "rasterizer/vertex-buffer.hpp"
  29. #include "rasterizer/vertex-array.hpp"
  30. #include "rasterizer/vertex-attribute-type.hpp"
  31. #include "rasterizer/drawing-mode.hpp"
  32. #include "rasterizer/texture-2d.hpp"
  33. #include "rasterizer/texture-wrapping.hpp"
  34. #include "rasterizer/texture-filter.hpp"
  35. #include "renderer/vertex-attributes.hpp"
  36. #include "renderer/material-flags.hpp"
  37. #include "renderer/model.hpp"
  38. #include "renderer/render-context.hpp"
  39. #include "scene/camera.hpp"
  40. #include "scene/scene.hpp"
  41. #include "scene/ambient-light.hpp"
  42. #include "scene/directional-light.hpp"
  43. #include "scene/point-light.hpp"
  44. #include "scene/spotlight.hpp"
  45. #include "scene/scene.hpp"
  46. #include "configuration.hpp"
  47. #include "math/math.hpp"
  48. #include <cmath>
  49. #include <glad/glad.h>
  50. #include "shadow-map-pass.hpp"
  51. static bool operation_compare(const render_operation& a, const render_operation& b);
  52. material_pass::material_pass(::rasterizer* rasterizer, const ::framebuffer* framebuffer, resource_manager* resource_manager):
  53. render_pass(rasterizer, framebuffer),
  54. fallback_material(nullptr),
  55. time_tween(nullptr),
  56. focal_point_tween(nullptr),
  57. shadow_map_pass(nullptr),
  58. shadow_map(nullptr)
  59. {
  60. soft_shadows_texture = resource_manager->load<texture_2d>("tree-shadow.png");
  61. soft_shadows_texture->set_wrapping(texture_wrapping::clamp, texture_wrapping::clamp);
  62. soft_shadows_texture->set_filters(texture_min_filter::linear_mipmap_linear, texture_mag_filter::linear);
  63. max_ambient_light_count = MATERIAL_PASS_MAX_AMBIENT_LIGHT_COUNT;
  64. max_point_light_count = MATERIAL_PASS_MAX_POINT_LIGHT_COUNT;
  65. max_directional_light_count = MATERIAL_PASS_MAX_DIRECTIONAL_LIGHT_COUNT;
  66. max_spotlight_count = MATERIAL_PASS_MAX_SPOTLIGHT_COUNT;
  67. ambient_light_colors = new float3[max_ambient_light_count];
  68. point_light_colors = new float3[max_point_light_count];
  69. point_light_positions = new float3[max_point_light_count];
  70. point_light_attenuations = new float3[max_point_light_count];
  71. directional_light_colors = new float3[max_directional_light_count];
  72. directional_light_directions = new float3[max_directional_light_count];
  73. spotlight_colors = new float3[max_spotlight_count];
  74. spotlight_positions = new float3[max_spotlight_count];
  75. spotlight_directions = new float3[max_spotlight_count];
  76. spotlight_attenuations = new float3[max_spotlight_count];
  77. spotlight_cutoffs = new float2[max_spotlight_count];
  78. }
  79. material_pass::~material_pass()
  80. {
  81. delete[] ambient_light_colors;
  82. delete[] point_light_colors;
  83. delete[] point_light_positions;
  84. delete[] point_light_attenuations;
  85. delete[] directional_light_colors;
  86. delete[] directional_light_directions;
  87. delete[] spotlight_colors;
  88. delete[] spotlight_positions;
  89. delete[] spotlight_directions;
  90. delete[] spotlight_attenuations;
  91. delete[] spotlight_cutoffs;
  92. }
  93. void material_pass::render(render_context* context) const
  94. {
  95. rasterizer->use_framebuffer(*framebuffer);
  96. glDisable(GL_BLEND);
  97. glEnable(GL_DEPTH_TEST);
  98. glDepthFunc(GL_LESS);
  99. glDepthMask(GL_TRUE);
  100. glDepthFunc(GL_LESS);
  101. glEnable(GL_CULL_FACE);
  102. glCullFace(GL_BACK);
  103. glDisable(GL_STENCIL_TEST);
  104. glStencilMask(0x00);
  105. auto viewport = framebuffer->get_dimensions();
  106. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  107. float2 resolution = {static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))};
  108. float time = (time_tween) ? time_tween->interpolate(context->alpha) : 0.0f;
  109. float3 focal_point = (focal_point_tween) ? focal_point_tween->interpolate(context->alpha) : float3{0, 0, 0};
  110. float4x4 view = context->camera->get_view_tween().interpolate(context->alpha);
  111. float4x4 projection = context->camera->get_projection_tween().interpolate(context->alpha);
  112. float4x4 view_projection = projection * view;
  113. float4x4 model_view_projection;
  114. float4x4 model;
  115. float4x4 model_view;
  116. float3x3 normal_model_view;
  117. int active_material_flags = 0;
  118. const ::shader_program* active_shader_program = nullptr;
  119. const ::material* active_material = nullptr;
  120. const parameter_set* parameters = nullptr;
  121. // Reset light counts
  122. ambient_light_count = 0;
  123. point_light_count = 0;
  124. directional_light_count = 0;
  125. spotlight_count = 0;
  126. // Collect lights
  127. const std::list<scene_object_base*>* lights = context->scene->get_objects(light::object_type_id);
  128. for (const scene_object_base* object: *lights)
  129. {
  130. // Skip inactive lights
  131. if (!object->is_active())
  132. continue;
  133. const ::light* light = static_cast<const ::light*>(object);
  134. switch (light->get_light_type())
  135. {
  136. // Add ambient light
  137. case light_type::ambient:
  138. {
  139. if (ambient_light_count < max_ambient_light_count)
  140. {
  141. ambient_light_colors[ambient_light_count] = light->get_scaled_color_tween().interpolate(context->alpha);
  142. ++ambient_light_count;
  143. }
  144. break;
  145. }
  146. // Add point light
  147. case light_type::point:
  148. {
  149. if (point_light_count < max_point_light_count)
  150. {
  151. point_light_colors[point_light_count] = light->get_scaled_color_tween().interpolate(context->alpha);
  152. // Transform position into view-space
  153. float3 position = light->get_transform_tween().interpolate(context->alpha).translation;
  154. float3 view_space_position = math::resize<3>(view * float4{position.x, position.y, position.z, 1.0f});
  155. point_light_positions[point_light_count] = view_space_position;
  156. point_light_attenuations[point_light_count] = static_cast<const point_light*>(light)->get_attenuation_tween().interpolate(context->alpha);
  157. ++point_light_count;
  158. }
  159. break;
  160. }
  161. // Add directional light
  162. case light_type::directional:
  163. {
  164. if (directional_light_count < max_directional_light_count)
  165. {
  166. directional_light_colors[directional_light_count] = light->get_scaled_color_tween().interpolate(context->alpha);
  167. // Transform direction into view-space
  168. float3 direction = static_cast<const directional_light*>(light)->get_direction_tween().interpolate(context->alpha);
  169. float3 view_space_direction = math::normalize(math::resize<3>(view * math::resize<4>(-direction)));
  170. directional_light_directions[directional_light_count] = view_space_direction;
  171. ++directional_light_count;
  172. }
  173. break;
  174. }
  175. // Add spotlight
  176. case light_type::spot:
  177. {
  178. if (spotlight_count < max_spotlight_count)
  179. {
  180. spotlight_colors[spotlight_count] = light->get_scaled_color_tween().interpolate(context->alpha);
  181. // Transform position into view-space
  182. float3 position = light->get_transform_tween().interpolate(context->alpha).translation;
  183. float3 view_space_position = math::resize<3>(view * float4{position.x, position.y, position.z, 1.0f});
  184. spotlight_positions[spotlight_count] = view_space_position;
  185. const ::spotlight* spotlight = static_cast<const ::spotlight*>(light);
  186. // Transform direction into view-space
  187. float3 direction = spotlight->get_direction_tween().interpolate(context->alpha);
  188. float3 view_space_direction = math::normalize(math::resize<3>(view * float4{-direction.x, -direction.y, -direction.z, 0.0f}));
  189. spotlight_directions[spotlight_count] = view_space_direction;
  190. spotlight_attenuations[spotlight_count] = spotlight->get_attenuation_tween().interpolate(context->alpha);
  191. spotlight_cutoffs[spotlight_count] = spotlight->get_cosine_cutoff_tween().interpolate(context->alpha);
  192. ++spotlight_count;
  193. }
  194. break;
  195. }
  196. default:
  197. break;
  198. }
  199. }
  200. float4x4 shadow_map_matrices[4];
  201. float4 shadow_map_split_distances;
  202. if (shadow_map_pass)
  203. {
  204. for (int i = 0; i < 4; ++i)
  205. shadow_map_matrices[i] = shadow_map_pass->get_shadow_matrices()[i];
  206. // Calculate shadow map split distances
  207. for (int i = 0; i < 4; ++i)
  208. shadow_map_split_distances[i] = shadow_map_pass->get_split_distances()[i + 1];
  209. }
  210. // Sort render operations
  211. context->operations.sort(operation_compare);
  212. for (const render_operation& operation: context->operations)
  213. {
  214. // Get operation material
  215. const ::material* material = operation.material;
  216. if (!material)
  217. {
  218. if (fallback_material)
  219. {
  220. // No material specified, use fallback material
  221. material = fallback_material;
  222. }
  223. else
  224. {
  225. // No material specified and no fallback material, skip operation
  226. continue;
  227. }
  228. }
  229. // Switch materials if necessary
  230. if (active_material != material)
  231. {
  232. active_material = material;
  233. // Change rasterizer state according to material flags
  234. std::uint32_t material_flags = active_material->get_flags();
  235. if (active_material_flags != material_flags)
  236. {
  237. if ((material_flags & MATERIAL_FLAG_TRANSLUCENT) != (active_material_flags & MATERIAL_FLAG_TRANSLUCENT))
  238. {
  239. if (material_flags & MATERIAL_FLAG_TRANSLUCENT)
  240. {
  241. glEnable(GL_BLEND);
  242. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  243. }
  244. else
  245. {
  246. glDisable(GL_BLEND);
  247. }
  248. }
  249. if ((material_flags & MATERIAL_FLAG_BACK_FACES) != (active_material_flags & MATERIAL_FLAG_BACK_FACES))
  250. {
  251. if (material_flags & MATERIAL_FLAG_BACK_FACES)
  252. {
  253. glEnable(GL_CULL_FACE);
  254. glCullFace(GL_FRONT);
  255. }
  256. else
  257. {
  258. glEnable(GL_CULL_FACE);
  259. glCullFace(GL_BACK);
  260. }
  261. }
  262. else if ((material_flags & MATERIAL_FLAG_FRONT_AND_BACK_FACES) != (active_material_flags & MATERIAL_FLAG_FRONT_AND_BACK_FACES))
  263. {
  264. if (material_flags & MATERIAL_FLAG_FRONT_AND_BACK_FACES)
  265. {
  266. glDisable(GL_CULL_FACE);
  267. }
  268. else
  269. {
  270. glEnable(GL_CULL_FACE);
  271. glCullFace(GL_BACK);
  272. }
  273. }
  274. if ((material_flags & MATERIAL_FLAG_X_RAY) != (active_material_flags & MATERIAL_FLAG_X_RAY))
  275. {
  276. if (material_flags & MATERIAL_FLAG_X_RAY)
  277. {
  278. glDisable(GL_DEPTH_TEST);
  279. }
  280. else
  281. {
  282. glEnable(GL_DEPTH_TEST);
  283. }
  284. }
  285. if ((material_flags & MATERIAL_FLAG_DECAL_SURFACE) != (active_material_flags & MATERIAL_FLAG_DECAL_SURFACE))
  286. {
  287. if (material_flags & MATERIAL_FLAG_DECAL_SURFACE)
  288. {
  289. glEnable(GL_STENCIL_TEST);
  290. glStencilFunc(GL_ALWAYS, 1, ~0);
  291. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  292. glStencilMask(~0);
  293. }
  294. else
  295. {
  296. glDisable(GL_STENCIL_TEST);
  297. glStencilMask(0);
  298. }
  299. }
  300. if ((material_flags & MATERIAL_FLAG_DECAL) != (active_material_flags & MATERIAL_FLAG_DECAL))
  301. {
  302. if (material_flags & MATERIAL_FLAG_DECAL)
  303. {
  304. glEnable(GL_DEPTH_TEST);
  305. glDepthFunc(GL_LEQUAL);
  306. glDepthMask(GL_FALSE);
  307. glEnable(GL_STENCIL_TEST);
  308. glStencilFunc(GL_EQUAL, 1, ~0);
  309. //glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO);
  310. //glStencilMask(~0);
  311. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  312. glStencilMask(0);
  313. }
  314. else
  315. {
  316. glEnable(GL_DEPTH_TEST);
  317. glDepthFunc(GL_LESS);
  318. glDepthMask(GL_TRUE);
  319. glDisable(GL_STENCIL_TEST);
  320. glStencilMask(0);
  321. }
  322. }
  323. /*
  324. if ((material_flags & MATERIAL_FLAG_OUTLINE) != (active_material_flags & MATERIAL_FLAG_OUTLINE))
  325. {
  326. if (material_flags & MATERIAL_FLAG_OUTLINE)
  327. {
  328. glEnable(GL_STENCIL_TEST);
  329. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  330. glStencilFunc(GL_ALWAYS, 2, 0xFF);
  331. glStencilMask(0xFF);
  332. }
  333. else
  334. {
  335. glDisable(GL_STENCIL_TEST);
  336. glStencilMask(0x00);
  337. }
  338. }
  339. */
  340. active_material_flags = material_flags;
  341. }
  342. // Switch shaders if necessary
  343. const ::shader_program* shader_program = active_material->get_shader_program();
  344. if (active_shader_program != shader_program)
  345. {
  346. active_shader_program = shader_program;
  347. if (!active_shader_program)
  348. {
  349. continue;
  350. }
  351. // Change shader program
  352. rasterizer->use_program(*active_shader_program);
  353. // Get set of known shader input parameters
  354. if (auto it = parameter_sets.find(active_shader_program); it != parameter_sets.end())
  355. {
  356. parameters = it->second;
  357. }
  358. else
  359. {
  360. parameters = load_parameter_set(active_shader_program);
  361. }
  362. // Upload context-dependent shader parameters
  363. if (parameters->time)
  364. parameters->time->upload(time);
  365. if (parameters->resolution)
  366. parameters->resolution->upload(resolution);
  367. if (parameters->view)
  368. parameters->view->upload(view);
  369. if (parameters->view_projection)
  370. parameters->view_projection->upload(view_projection);
  371. if (parameters->ambient_light_count)
  372. parameters->ambient_light_count->upload(ambient_light_count);
  373. if (parameters->ambient_light_colors)
  374. parameters->ambient_light_colors->upload(0, ambient_light_colors, ambient_light_count);
  375. if (parameters->point_light_count)
  376. parameters->point_light_count->upload(point_light_count);
  377. if (parameters->point_light_colors)
  378. parameters->point_light_colors->upload(0, point_light_colors, point_light_count);
  379. if (parameters->point_light_positions)
  380. parameters->point_light_positions->upload(0, point_light_positions, point_light_count);
  381. if (parameters->point_light_attenuations)
  382. parameters->point_light_attenuations->upload(0, point_light_attenuations, point_light_count);
  383. if (parameters->directional_light_count)
  384. parameters->directional_light_count->upload(directional_light_count);
  385. if (parameters->directional_light_colors)
  386. parameters->directional_light_colors->upload(0, directional_light_colors, directional_light_count);
  387. if (parameters->directional_light_directions)
  388. parameters->directional_light_directions->upload(0, directional_light_directions, directional_light_count);
  389. if (parameters->spotlight_count)
  390. parameters->spotlight_count->upload(spotlight_count);
  391. if (parameters->spotlight_colors)
  392. parameters->spotlight_colors->upload(0, spotlight_colors, spotlight_count);
  393. if (parameters->spotlight_positions)
  394. parameters->spotlight_positions->upload(0, spotlight_positions, spotlight_count);
  395. if (parameters->spotlight_directions)
  396. parameters->spotlight_directions->upload(0, spotlight_directions, spotlight_count);
  397. if (parameters->spotlight_attenuations)
  398. parameters->spotlight_attenuations->upload(0, spotlight_attenuations, spotlight_count);
  399. if (parameters->spotlight_cutoffs)
  400. parameters->spotlight_cutoffs->upload(0, spotlight_cutoffs, spotlight_count);
  401. if (parameters->soft_shadows)
  402. parameters->soft_shadows->upload(soft_shadows_texture);
  403. if (parameters->focal_point)
  404. parameters->focal_point->upload(focal_point);
  405. if (parameters->shadow_map_matrices)
  406. parameters->shadow_map_matrices->upload(0, shadow_map_matrices, 4);
  407. if (parameters->shadow_map_split_distances)
  408. parameters->shadow_map_split_distances->upload(shadow_map_split_distances);
  409. if (parameters->shadow_map && shadow_map)
  410. parameters->shadow_map->upload(shadow_map);
  411. }
  412. // Upload material properties to shader
  413. active_material->upload(context->alpha);
  414. }
  415. // Calculate operation-dependent parameters
  416. model = operation.transform;
  417. model_view_projection = view_projection * model;
  418. model_view = view * model;
  419. normal_model_view = math::transpose(math::inverse(math::resize<3, 3>(model_view)));
  420. // Upload operation-dependent parameters
  421. if (parameters->model)
  422. parameters->model->upload(model);
  423. if (parameters->model_view)
  424. parameters->model_view->upload(model_view);
  425. if (parameters->model_view_projection)
  426. parameters->model_view_projection->upload(model_view_projection);
  427. if (parameters->normal_model_view)
  428. parameters->normal_model_view->upload(normal_model_view);
  429. // Draw geometry
  430. if (operation.instance_count)
  431. rasterizer->draw_arrays_instanced(*operation.vertex_array, operation.drawing_mode, operation.start_index, operation.index_count, operation.instance_count);
  432. else
  433. rasterizer->draw_arrays(*operation.vertex_array, operation.drawing_mode, operation.start_index, operation.index_count);
  434. }
  435. }
  436. void material_pass::set_fallback_material(const material* fallback)
  437. {
  438. this->fallback_material = fallback;
  439. }
  440. void material_pass::set_time_tween(const tween<double>* time)
  441. {
  442. this->time_tween = time;
  443. }
  444. void material_pass::set_focal_point_tween(const tween<float3>* focal_point)
  445. {
  446. this->focal_point_tween = focal_point;
  447. }
  448. const material_pass::parameter_set* material_pass::load_parameter_set(const shader_program* program) const
  449. {
  450. // Allocate a new parameter set
  451. parameter_set* parameters = new parameter_set();
  452. // Connect inputs
  453. parameters->time = program->get_input("time");
  454. parameters->resolution = program->get_input("resolution");
  455. parameters->model = program->get_input("model");
  456. parameters->view = program->get_input("view");
  457. parameters->projection = program->get_input("projection");
  458. parameters->model_view = program->get_input("model_view");
  459. parameters->view_projection = program->get_input("view_projection");
  460. parameters->model_view_projection = program->get_input("model_view_projection");
  461. parameters->normal_model_view = program->get_input("normal_model_view");
  462. parameters->ambient_light_count = program->get_input("ambient_light_count");
  463. parameters->ambient_light_colors = program->get_input("ambient_light_colors");
  464. parameters->point_light_count = program->get_input("point_light_count");
  465. parameters->point_light_colors = program->get_input("point_light_colors");
  466. parameters->point_light_positions = program->get_input("point_light_positions");
  467. parameters->point_light_attenuations = program->get_input("point_light_attenuations");
  468. parameters->directional_light_count = program->get_input("directional_light_count");
  469. parameters->directional_light_colors = program->get_input("directional_light_colors");
  470. parameters->directional_light_directions = program->get_input("directional_light_directions");
  471. parameters->spotlight_count = program->get_input("spotlight_count");
  472. parameters->spotlight_colors = program->get_input("spotlight_colors");
  473. parameters->spotlight_positions = program->get_input("spotlight_positions");
  474. parameters->spotlight_directions = program->get_input("spotlight_directions");
  475. parameters->spotlight_attenuations = program->get_input("spotlight_attenuations");
  476. parameters->spotlight_cutoffs = program->get_input("spotlight_cutoffs");
  477. parameters->soft_shadows = program->get_input("soft_shadows");
  478. parameters->focal_point = program->get_input("focal_point");
  479. parameters->shadow_map_matrices = program->get_input("shadow_map_matrices");
  480. parameters->shadow_map_split_distances = program->get_input("shadow_map_split_distances");
  481. parameters->shadow_map = program->get_input("shadow_map");
  482. // Add parameter set to map of parameter sets
  483. parameter_sets[program] = parameters;
  484. return parameters;
  485. }
  486. bool operation_compare(const render_operation& a, const render_operation& b)
  487. {
  488. if (!a.material)
  489. return false;
  490. else if (!b.material)
  491. return true;
  492. bool xray_a = a.material->get_flags() & MATERIAL_FLAG_X_RAY;
  493. bool xray_b = b.material->get_flags() & MATERIAL_FLAG_X_RAY;
  494. if (xray_a)
  495. {
  496. if (xray_b)
  497. {
  498. // A and B are both xray, render back to front
  499. return (a.depth >= b.depth);
  500. }
  501. else
  502. {
  503. // A is xray, B is not. Render B first
  504. return false;
  505. }
  506. }
  507. else
  508. {
  509. if (xray_b)
  510. {
  511. // A is opaque, B is xray. Render A first
  512. return true;
  513. }
  514. else
  515. {
  516. // Determine transparency
  517. bool transparent_a = a.material->get_flags() & MATERIAL_FLAG_TRANSLUCENT;
  518. bool transparent_b = b.material->get_flags() & MATERIAL_FLAG_TRANSLUCENT;
  519. if (transparent_a)
  520. {
  521. if (transparent_b)
  522. {
  523. // Determine decal status
  524. bool decal_a = a.material->get_flags() & MATERIAL_FLAG_DECAL;
  525. bool decal_b = b.material->get_flags() & MATERIAL_FLAG_DECAL;
  526. if (decal_a)
  527. {
  528. if (decal_b)
  529. {
  530. // A and B are both transparent decals, render back to front
  531. return (a.depth >= b.depth);
  532. }
  533. else
  534. {
  535. // A is a transparent decal, B is transparent but not a decal, render A first
  536. return true;
  537. }
  538. }
  539. else
  540. {
  541. if (decal_b)
  542. {
  543. // A is transparent but not a decal, B is a transparent decal, render B first
  544. return false;
  545. }
  546. else
  547. {
  548. // A and B are both transparent, but not decals, render back to front
  549. return (a.depth >= b.depth);
  550. }
  551. }
  552. }
  553. else
  554. {
  555. // A is transparent, B is opaque. Render B first
  556. return false;
  557. }
  558. }
  559. else
  560. {
  561. if (transparent_b)
  562. {
  563. // A is opaque, B is transparent. Render A first
  564. return true;
  565. }
  566. else
  567. {
  568. // A and B are both opaque
  569. if (a.material->get_shader_program() == b.material->get_shader_program())
  570. {
  571. // A and B have the same shader
  572. if (a.vertex_array == b.vertex_array)
  573. {
  574. // A and B have the same VAO, render front to back
  575. return (a.depth < b.depth);
  576. }
  577. else
  578. {
  579. // Sort by VAO
  580. return (a.vertex_array < b.vertex_array);
  581. }
  582. }
  583. else
  584. {
  585. // A and B are both opaque and have different shaders, sort by shader
  586. return (a.material->get_shader_program() < b.material->get_shader_program());
  587. }
  588. }
  589. }
  590. }
  591. }
  592. }