fog_renderer.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. }
  23. if (m_width <= 0 || m_height <= 0) {
  24. return;
  25. }
  26. if (static_cast<int>(m_cells.size()) != m_width * m_height) {
  27. return;
  28. }
  29. (void)resources;
  30. if (!m_instances.empty()) {
  31. renderer.fogBatch(m_instances.data(), m_instances.size());
  32. }
  33. }
  34. void FogRenderer::buildChunks() {
  35. m_instances.clear();
  36. if (m_width <= 0 || m_height <= 0) {
  37. return;
  38. }
  39. if (static_cast<int>(m_cells.size()) != m_width * m_height) {
  40. return;
  41. }
  42. QElapsedTimer timer;
  43. timer.start();
  44. const float halfTile = m_tileSize * 0.5f;
  45. m_instances.reserve(static_cast<std::size_t>(m_width) * m_height);
  46. std::size_t totalQuads = 0;
  47. for (int z = 0; z < m_height; ++z) {
  48. for (int x = 0; x < m_width; ++x) {
  49. const std::uint8_t state = m_cells[z * m_width + x];
  50. if (state >= 2) {
  51. continue;
  52. }
  53. FogInstance instance;
  54. const float worldX = (x - m_halfWidth) * m_tileSize;
  55. const float worldZ = (z - m_halfHeight) * m_tileSize;
  56. instance.center = QVector3D(worldX, 0.25f, worldZ);
  57. instance.color = (state == 0) ? QVector3D(0.02f, 0.02f, 0.05f)
  58. : QVector3D(0.05f, 0.05f, 0.05f);
  59. instance.alpha = (state == 0) ? 0.9f : 0.45f;
  60. instance.size = m_tileSize;
  61. m_instances.push_back(instance);
  62. ++totalQuads;
  63. }
  64. }
  65. }
  66. } // namespace Render::GL