bridge_renderer.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include "bridge_renderer.h"
  2. #include "../../game/map/visibility_service.h"
  3. #include "../gl/mesh.h"
  4. #include "../gl/render_constants.h"
  5. #include "../gl/resources.h"
  6. #include "../scene_renderer.h"
  7. #include "map/terrain.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. using namespace Render::GL::Geometry;
  20. BridgeRenderer::BridgeRenderer() = default;
  21. BridgeRenderer::~BridgeRenderer() = default;
  22. void BridgeRenderer::configure(const std::vector<Game::Map::Bridge> &bridges,
  23. float tile_size) {
  24. m_bridges = bridges;
  25. m_tile_size = tile_size;
  26. buildMeshes();
  27. }
  28. void BridgeRenderer::buildMeshes() {
  29. m_meshes.clear();
  30. if (m_bridges.empty()) {
  31. return;
  32. }
  33. for (const auto &bridge : m_bridges) {
  34. QVector3D dir = bridge.end - bridge.start;
  35. float const length = dir.length();
  36. if (length < 0.01F) {
  37. m_meshes.push_back(nullptr);
  38. continue;
  39. }
  40. dir.normalize();
  41. QVector3D const perpendicular(-dir.z(), 0.0F, dir.x());
  42. float const half_width = bridge.width * 0.5F;
  43. int length_segments =
  44. static_cast<int>(std::ceil(length / (m_tile_size * 0.3F)));
  45. length_segments = std::max(length_segments, MinLengthSegments);
  46. std::vector<Vertex> vertices;
  47. std::vector<unsigned int> indices;
  48. constexpr int k_vertices_per_bridge_segment = 12;
  49. const float deck_thickness = std::clamp(bridge.width * 0.25F, 0.35F, 0.8F);
  50. const float parapet_height = std::clamp(bridge.width * 0.25F, 0.25F, 0.55F);
  51. const float parapet_offset = half_width * 1.05F;
  52. auto add_vertex = [&](const QVector3D &position, const QVector3D &normal,
  53. float u, float v) {
  54. Vertex vtx{};
  55. vtx.position[0] = position.x();
  56. vtx.position[1] = position.y();
  57. vtx.position[2] = position.z();
  58. QVector3D const n = normal.normalized();
  59. vtx.normal[0] = n.x();
  60. vtx.normal[1] = n.y();
  61. vtx.normal[2] = n.z();
  62. vtx.tex_coord[0] = u;
  63. vtx.tex_coord[1] = v;
  64. vertices.push_back(vtx);
  65. };
  66. auto push_quad = [&](unsigned int a, unsigned int b, unsigned int c,
  67. unsigned int d) {
  68. indices.push_back(a);
  69. indices.push_back(b);
  70. indices.push_back(c);
  71. indices.push_back(a);
  72. indices.push_back(c);
  73. indices.push_back(d);
  74. };
  75. for (int i = 0; i <= length_segments; ++i) {
  76. float const t =
  77. static_cast<float>(i) / static_cast<float>(length_segments);
  78. QVector3D const center_pos = bridge.start + dir * (length * t);
  79. float const arch_curve = 4.0F * t * (1.0F - t);
  80. float const arch_height = bridge.height * arch_curve * 0.8F;
  81. float const deck_height =
  82. bridge.start.y() + bridge.height + arch_height * 0.3F;
  83. float const stone_noise = std::sin(center_pos.x() * 3.0F) *
  84. std::cos(center_pos.z() * 2.5F) * 0.02F;
  85. float const deck_y = deck_height + stone_noise;
  86. float const underside_y =
  87. deck_height - deck_thickness - arch_curve * bridge.height * 0.55F;
  88. float const rail_top_y = deck_y + parapet_height;
  89. QVector3D const left_normal = (-perpendicular).normalized();
  90. QVector3D const right_normal = perpendicular.normalized();
  91. QVector3D top_left = center_pos + perpendicular * (-half_width);
  92. top_left.setY(deck_y);
  93. QVector3D top_right = center_pos + perpendicular * (half_width);
  94. top_right.setY(deck_y);
  95. QVector3D bottom_left = top_left;
  96. bottom_left.setY(underside_y);
  97. QVector3D bottom_right = top_right;
  98. bottom_right.setY(underside_y);
  99. QVector3D const side_left_top = top_left;
  100. QVector3D const side_left_bottom = bottom_left;
  101. QVector3D const side_right_top = top_right;
  102. QVector3D const side_right_bottom = bottom_right;
  103. QVector3D parapet_left_bottom =
  104. center_pos + perpendicular * (-parapet_offset);
  105. parapet_left_bottom.setY(deck_y);
  106. QVector3D parapet_left_top = parapet_left_bottom;
  107. parapet_left_top.setY(rail_top_y);
  108. QVector3D parapet_right_bottom =
  109. center_pos + perpendicular * (parapet_offset);
  110. parapet_right_bottom.setY(deck_y);
  111. QVector3D parapet_right_top = parapet_right_bottom;
  112. parapet_right_top.setY(rail_top_y);
  113. float const tex_u0 = 0.0F;
  114. float const tex_u1 = 1.0F;
  115. float const tex_v = t * length * 0.4F;
  116. add_vertex(top_left, QVector3D(0.0F, 1.0F, 0.0F), tex_u0, tex_v);
  117. add_vertex(top_right, QVector3D(0.0F, 1.0F, 0.0F), tex_u1, tex_v);
  118. add_vertex(bottom_left, QVector3D(0.0F, -1.0F, 0.0F), tex_u0, tex_v);
  119. add_vertex(bottom_right, QVector3D(0.0F, -1.0F, 0.0F), tex_u1, tex_v);
  120. add_vertex(side_left_top, left_normal, tex_u0, tex_v);
  121. add_vertex(side_left_bottom, left_normal, tex_u0, tex_v);
  122. add_vertex(side_right_top, right_normal, tex_u1, tex_v);
  123. add_vertex(side_right_bottom, right_normal, tex_u1, tex_v);
  124. add_vertex(parapet_left_top, left_normal, tex_u0, tex_v);
  125. add_vertex(parapet_left_bottom, left_normal, tex_u0, tex_v);
  126. add_vertex(parapet_right_top, right_normal, tex_u1, tex_v);
  127. add_vertex(parapet_right_bottom, right_normal, tex_u1, tex_v);
  128. if (i < length_segments) {
  129. auto const base_idx =
  130. static_cast<unsigned int>(i * k_vertices_per_bridge_segment);
  131. unsigned int const next_idx = base_idx + k_vertices_per_bridge_segment;
  132. push_quad(base_idx + 0, base_idx + 1, next_idx + 1, next_idx + 0);
  133. push_quad(next_idx + 3, next_idx + 2, base_idx + 2, base_idx + 3);
  134. push_quad(base_idx + 4, base_idx + 5, next_idx + 5, next_idx + 4);
  135. push_quad(base_idx + 6, base_idx + 7, next_idx + 7, next_idx + 6);
  136. push_quad(base_idx + 9, base_idx + 8, next_idx + 8, next_idx + 9);
  137. push_quad(base_idx + 11, base_idx + 10, next_idx + 10, next_idx + 11);
  138. }
  139. }
  140. if (!vertices.empty()) {
  141. unsigned int const start_idx = 0;
  142. auto const end_idx = static_cast<unsigned int>(
  143. length_segments * k_vertices_per_bridge_segment);
  144. QVector3D const forward_normal = dir;
  145. auto add_cap = [&](unsigned int topL, unsigned int topR,
  146. unsigned int bottomR, unsigned int bottomL,
  147. const QVector3D &normal) {
  148. auto const cap_start = static_cast<unsigned int>(vertices.size());
  149. auto copy_vertex = [&](unsigned int source, const QVector3D &norm) {
  150. const Vertex &src = vertices[source];
  151. Vertex vtx = src;
  152. QVector3D const n = norm.normalized();
  153. vtx.normal[0] = n.x();
  154. vtx.normal[1] = n.y();
  155. vtx.normal[2] = n.z();
  156. vertices.push_back(vtx);
  157. };
  158. copy_vertex(topL, normal);
  159. copy_vertex(topR, normal);
  160. copy_vertex(bottomR, normal);
  161. copy_vertex(bottomL, normal);
  162. push_quad(cap_start + 0, cap_start + 1, cap_start + 2, cap_start + 3);
  163. };
  164. add_cap(start_idx + 0, start_idx + 1, start_idx + 3, start_idx + 2,
  165. -forward_normal);
  166. add_cap(end_idx + 0, end_idx + 1, end_idx + 3, end_idx + 2,
  167. forward_normal);
  168. }
  169. if (!vertices.empty() && !indices.empty()) {
  170. m_meshes.push_back(std::make_unique<Mesh>(vertices, indices));
  171. } else {
  172. m_meshes.push_back(nullptr);
  173. }
  174. }
  175. }
  176. void BridgeRenderer::submit(Renderer &renderer, ResourceManager *resources) {
  177. if (m_meshes.empty() || m_bridges.empty()) {
  178. return;
  179. }
  180. Q_UNUSED(resources);
  181. auto &visibility = Game::Map::VisibilityService::instance();
  182. const bool use_visibility = visibility.isInitialized();
  183. auto *shader = renderer.getShader("bridge");
  184. if (shader == nullptr) {
  185. shader = renderer.getShader("basic");
  186. if (shader == nullptr) {
  187. return;
  188. }
  189. }
  190. renderer.setCurrentShader(shader);
  191. QMatrix4x4 model;
  192. model.setToIdentity();
  193. QVector3D const stone_color(0.55F, 0.52F, 0.48F);
  194. size_t mesh_index = 0;
  195. for (const auto &bridge : m_bridges) {
  196. if (mesh_index >= m_meshes.size()) {
  197. break;
  198. }
  199. auto *mesh = m_meshes[mesh_index].get();
  200. ++mesh_index;
  201. if (mesh == nullptr) {
  202. continue;
  203. }
  204. QVector3D dir = bridge.end - bridge.start;
  205. float const length = dir.length();
  206. float alpha = 1.0F;
  207. QVector3D color_multiplier(1.0F, 1.0F, 1.0F);
  208. if (use_visibility) {
  209. int max_visibility_state = 0;
  210. dir.normalize();
  211. int const samples_per_bridge = 5;
  212. for (int i = 0; i < samples_per_bridge; ++i) {
  213. float const t =
  214. static_cast<float>(i) / static_cast<float>(samples_per_bridge - 1);
  215. QVector3D const pos = bridge.start + dir * (length * t);
  216. if (visibility.isVisibleWorld(pos.x(), pos.z())) {
  217. max_visibility_state = 2;
  218. break;
  219. }
  220. if (visibility.isExploredWorld(pos.x(), pos.z())) {
  221. max_visibility_state = std::max(max_visibility_state, 1);
  222. }
  223. }
  224. if (max_visibility_state == 0) {
  225. continue;
  226. }
  227. if (max_visibility_state == 1) {
  228. alpha = 0.5F;
  229. color_multiplier = QVector3D(0.4F, 0.4F, 0.45F);
  230. }
  231. }
  232. QVector3D const final_color(stone_color.x() * color_multiplier.x(),
  233. stone_color.y() * color_multiplier.y(),
  234. stone_color.z() * color_multiplier.z());
  235. renderer.mesh(mesh, model, final_color, nullptr, alpha);
  236. }
  237. renderer.setCurrentShader(nullptr);
  238. }
  239. } // namespace Render::GL