road_renderer.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "road_renderer.h"
  2. #include "../../game/map/terrain.h"
  3. #include "../../game/map/visibility_service.h"
  4. #include "../gl/mesh.h"
  5. #include "../gl/resources.h"
  6. #include "../scene_renderer.h"
  7. #include "ground_utils.h"
  8. #include <QVector2D>
  9. #include <QVector3D>
  10. #include <algorithm>
  11. #include <cmath>
  12. #include <cstddef>
  13. #include <memory>
  14. #include <qglobal.h>
  15. #include <qmatrix4x4.h>
  16. #include <qvectornd.h>
  17. #include <vector>
  18. namespace Render::GL {
  19. RoadRenderer::RoadRenderer() = default;
  20. RoadRenderer::~RoadRenderer() = default;
  21. void RoadRenderer::configure(
  22. const std::vector<Game::Map::RoadSegment> &road_segments, float tile_size) {
  23. m_road_segments = road_segments;
  24. m_tile_size = tile_size;
  25. build_meshes();
  26. }
  27. void RoadRenderer::build_meshes() {
  28. m_meshes.clear();
  29. if (m_road_segments.empty()) {
  30. return;
  31. }
  32. auto noise = [](float x, float y) -> float {
  33. float const ix = std::floor(x);
  34. float const iy = std::floor(y);
  35. float fx = x - ix;
  36. float fy = y - iy;
  37. fx = fx * fx * (3.0F - 2.0F * fx);
  38. fy = fy * fy * (3.0F - 2.0F * fy);
  39. float const a = Ground::noise_hash(ix, iy);
  40. float const b = Ground::noise_hash(ix + 1.0F, iy);
  41. float const c = Ground::noise_hash(ix, iy + 1.0F);
  42. float const d = Ground::noise_hash(ix + 1.0F, iy + 1.0F);
  43. return a * (1.0F - fx) * (1.0F - fy) + b * fx * (1.0F - fy) +
  44. c * (1.0F - fx) * fy + d * fx * fy;
  45. };
  46. for (const auto &segment : m_road_segments) {
  47. QVector3D dir = segment.end - segment.start;
  48. float const length = dir.length();
  49. if (length < 0.01F) {
  50. m_meshes.push_back(nullptr);
  51. continue;
  52. }
  53. dir.normalize();
  54. QVector3D const perpendicular(-dir.z(), 0.0F, dir.x());
  55. float const half_width = segment.width * 0.5F;
  56. int length_steps =
  57. static_cast<int>(std::ceil(length / (m_tile_size * 0.5F))) + 1;
  58. length_steps = std::max(length_steps, 8);
  59. std::vector<Vertex> vertices;
  60. std::vector<unsigned int> indices;
  61. for (int i = 0; i < length_steps; ++i) {
  62. float const t =
  63. static_cast<float>(i) / static_cast<float>(length_steps - 1);
  64. QVector3D center_pos = segment.start + dir * (length * t);
  65. constexpr float k_edge_noise_freq_1 = 1.5F;
  66. constexpr float k_edge_noise_freq_2 = 4.0F;
  67. float const edge_noise1 = noise(center_pos.x() * k_edge_noise_freq_1,
  68. center_pos.z() * k_edge_noise_freq_1);
  69. float const edge_noise2 = noise(center_pos.x() * k_edge_noise_freq_2,
  70. center_pos.z() * k_edge_noise_freq_2);
  71. float combined_noise = edge_noise1 * 0.6F + edge_noise2 * 0.4F;
  72. combined_noise = (combined_noise - 0.5F) * 2.0F;
  73. float const width_variation = combined_noise * half_width * 0.15F;
  74. constexpr float road_y_offset = 0.02F;
  75. QVector3D const left =
  76. center_pos - perpendicular * (half_width + width_variation);
  77. QVector3D const right =
  78. center_pos + perpendicular * (half_width + width_variation);
  79. float const normal[3] = {0.0F, 1.0F, 0.0F};
  80. Vertex left_vertex;
  81. left_vertex.position[0] = left.x();
  82. left_vertex.position[1] = left.y() + road_y_offset;
  83. left_vertex.position[2] = left.z();
  84. left_vertex.normal[0] = normal[0];
  85. left_vertex.normal[1] = normal[1];
  86. left_vertex.normal[2] = normal[2];
  87. left_vertex.tex_coord[0] = 0.0F;
  88. left_vertex.tex_coord[1] = t;
  89. vertices.push_back(left_vertex);
  90. Vertex right_vertex;
  91. right_vertex.position[0] = right.x();
  92. right_vertex.position[1] = right.y() + road_y_offset;
  93. right_vertex.position[2] = right.z();
  94. right_vertex.normal[0] = normal[0];
  95. right_vertex.normal[1] = normal[1];
  96. right_vertex.normal[2] = normal[2];
  97. right_vertex.tex_coord[0] = 1.0F;
  98. right_vertex.tex_coord[1] = t;
  99. vertices.push_back(right_vertex);
  100. if (i < length_steps - 1) {
  101. unsigned int const idx0 = i * 2;
  102. unsigned int const idx1 = idx0 + 1;
  103. unsigned int const idx2 = idx0 + 2;
  104. unsigned int const idx3 = idx0 + 3;
  105. indices.push_back(idx0);
  106. indices.push_back(idx2);
  107. indices.push_back(idx1);
  108. indices.push_back(idx1);
  109. indices.push_back(idx2);
  110. indices.push_back(idx3);
  111. }
  112. }
  113. if (!vertices.empty() && !indices.empty()) {
  114. m_meshes.push_back(std::make_unique<Mesh>(vertices, indices));
  115. } else {
  116. m_meshes.push_back(nullptr);
  117. }
  118. }
  119. }
  120. void RoadRenderer::submit(Renderer &renderer, ResourceManager *resources) {
  121. if (m_meshes.empty() || m_road_segments.empty()) {
  122. return;
  123. }
  124. Q_UNUSED(resources);
  125. auto &visibility = Game::Map::VisibilityService::instance();
  126. const bool use_visibility = visibility.is_initialized();
  127. auto *shader = renderer.get_shader("road");
  128. if (shader == nullptr) {
  129. shader = renderer.get_shader("terrain");
  130. if (shader == nullptr) {
  131. return;
  132. }
  133. }
  134. renderer.set_current_shader(shader);
  135. QMatrix4x4 model;
  136. model.setToIdentity();
  137. QVector3D const road_base_color(0.45F, 0.42F, 0.38F);
  138. size_t mesh_index = 0;
  139. for (const auto &segment : m_road_segments) {
  140. if (mesh_index >= m_meshes.size()) {
  141. break;
  142. }
  143. auto *mesh = m_meshes[mesh_index].get();
  144. ++mesh_index;
  145. if (mesh == nullptr) {
  146. continue;
  147. }
  148. QVector3D dir = segment.end - segment.start;
  149. float const length = dir.length();
  150. float alpha = 1.0F;
  151. QVector3D color_multiplier(1.0F, 1.0F, 1.0F);
  152. if (use_visibility) {
  153. int max_visibility_state = 0;
  154. dir.normalize();
  155. int const samples_per_segment = 5;
  156. for (int i = 0; i < samples_per_segment; ++i) {
  157. float const t =
  158. static_cast<float>(i) / static_cast<float>(samples_per_segment - 1);
  159. QVector3D const pos = segment.start + dir * (length * t);
  160. if (visibility.isVisibleWorld(pos.x(), pos.z())) {
  161. max_visibility_state = 2;
  162. break;
  163. }
  164. if (visibility.isExploredWorld(pos.x(), pos.z())) {
  165. max_visibility_state = std::max(max_visibility_state, 1);
  166. }
  167. }
  168. if (max_visibility_state == 0) {
  169. continue;
  170. }
  171. if (max_visibility_state == 1) {
  172. alpha = 0.5F;
  173. color_multiplier = QVector3D(0.4F, 0.4F, 0.45F);
  174. }
  175. }
  176. QVector3D const final_color(road_base_color.x() * color_multiplier.x(),
  177. road_base_color.y() * color_multiplier.y(),
  178. road_base_color.z() * color_multiplier.z());
  179. renderer.mesh(mesh, model, final_color, nullptr, alpha);
  180. }
  181. renderer.set_current_shader(nullptr);
  182. }
  183. } // namespace Render::GL