fog_renderer.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "fog_renderer.h"
  2. #include "../scene_renderer.h"
  3. #include <QElapsedTimer>
  4. #include <algorithm>
  5. namespace Render::GL {
  6. namespace {
  7. const QMatrix4x4 kIdentityMatrix;
  8. }
  9. void FogRenderer::updateMask(int width, int height, float tileSize,
  10. const std::vector<std::uint8_t> &cells) {
  11. m_width = std::max(0, width);
  12. m_height = std::max(0, height);
  13. m_tileSize = std::max(0.0001f, tileSize);
  14. m_halfWidth = m_width * 0.5f - 0.5f;
  15. m_halfHeight = m_height * 0.5f - 0.5f;
  16. m_cells = cells;
  17. buildChunks();
  18. }
  19. void FogRenderer::submit(Renderer &renderer, ResourceManager *resources) {
  20. if (!m_enabled)
  21. return;
  22. if (m_width <= 0 || m_height <= 0)
  23. return;
  24. if (static_cast<int>(m_cells.size()) != m_width * m_height)
  25. return;
  26. (void)resources;
  27. if (!m_instances.empty()) {
  28. renderer.fogBatch(m_instances.data(), m_instances.size());
  29. }
  30. }
  31. void FogRenderer::buildChunks() {
  32. m_instances.clear();
  33. if (m_width <= 0 || m_height <= 0)
  34. return;
  35. if (static_cast<int>(m_cells.size()) != m_width * m_height)
  36. return;
  37. QElapsedTimer timer;
  38. timer.start();
  39. const float halfTile = m_tileSize * 0.5f;
  40. m_instances.reserve(static_cast<std::size_t>(m_width) * m_height);
  41. std::size_t totalQuads = 0;
  42. for (int z = 0; z < m_height; ++z) {
  43. for (int x = 0; x < m_width; ++x) {
  44. const std::uint8_t state = m_cells[z * m_width + x];
  45. if (state >= 2)
  46. continue;
  47. FogInstance instance;
  48. const float worldX = (x - m_halfWidth) * m_tileSize;
  49. const float worldZ = (z - m_halfHeight) * m_tileSize;
  50. instance.center = QVector3D(worldX, 0.25f, worldZ);
  51. instance.color = (state == 0) ? QVector3D(0.02f, 0.02f, 0.05f)
  52. : QVector3D(0.05f, 0.05f, 0.05f);
  53. instance.alpha = (state == 0) ? 0.9f : 0.45f;
  54. instance.size = m_tileSize;
  55. m_instances.push_back(instance);
  56. ++totalQuads;
  57. }
  58. }
  59. }
  60. } // namespace Render::GL