fog_renderer.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "fog_renderer.h"
  2. #include "../scene_renderer.h"
  3. #include <QElapsedTimer>
  4. #include <algorithm>
  5. #include <cstddef>
  6. #include <cstdint>
  7. #include <qelapsedtimer.h>
  8. #include <qmatrix4x4.h>
  9. #include <vector>
  10. namespace Render::GL {
  11. namespace {
  12. const QMatrix4x4 k_identity_matrix;
  13. }
  14. void FogRenderer::update_mask(int width, int height, float tile_size,
  15. const std::vector<std::uint8_t> &cells) {
  16. m_width = std::max(0, width);
  17. m_height = std::max(0, height);
  18. m_tile_size = std::max(0.0001F, tile_size);
  19. m_half_width = m_width * 0.5F - 0.5F;
  20. m_half_height = m_height * 0.5F - 0.5F;
  21. m_cells = cells;
  22. build_chunks();
  23. }
  24. void FogRenderer::submit(Renderer &renderer, ResourceManager *resources) {
  25. if (!m_enabled) {
  26. return;
  27. }
  28. if (m_width <= 0 || m_height <= 0) {
  29. return;
  30. }
  31. if (static_cast<int>(m_cells.size()) != m_width * m_height) {
  32. return;
  33. }
  34. (void)resources;
  35. if (!m_instances.empty()) {
  36. renderer.fog_batch(m_instances.data(), m_instances.size());
  37. }
  38. }
  39. void FogRenderer::build_chunks() {
  40. m_instances.clear();
  41. if (m_width <= 0 || m_height <= 0) {
  42. return;
  43. }
  44. if (static_cast<int>(m_cells.size()) != m_width * m_height) {
  45. return;
  46. }
  47. QElapsedTimer timer;
  48. timer.start();
  49. const float half_tile = m_tile_size * 0.5F;
  50. m_instances.reserve(static_cast<std::size_t>(m_width) * m_height);
  51. std::size_t total_quads = 0;
  52. for (int z = 0; z < m_height; ++z) {
  53. for (int x = 0; x < m_width; ++x) {
  54. const std::uint8_t state = m_cells[z * m_width + x];
  55. if (state >= 2) {
  56. continue;
  57. }
  58. FogInstance instance;
  59. const float world_x = (x - m_half_width) * m_tile_size;
  60. const float world_z = (z - m_half_height) * m_tile_size;
  61. instance.center = QVector3D(world_x, 0.25F, world_z);
  62. instance.color = (state == 0) ? QVector3D(0.02F, 0.02F, 0.05F)
  63. : QVector3D(0.05F, 0.05F, 0.05F);
  64. instance.alpha = (state == 0) ? 0.9F : 0.45F;
  65. instance.size = m_tile_size;
  66. m_instances.push_back(instance);
  67. ++total_quads;
  68. }
  69. }
  70. }
  71. } // namespace Render::GL