bridge_renderer.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "bridge_renderer.h"
  2. #include "../../game/map/visibility_service.h"
  3. #include "../gl/mesh.h"
  4. #include "../gl/resources.h"
  5. #include "../scene_renderer.h"
  6. #include "terrain_gpu.h"
  7. #include <QVector2D>
  8. #include <QVector3D>
  9. #include <cmath>
  10. namespace Render::GL {
  11. BridgeRenderer::BridgeRenderer() = default;
  12. BridgeRenderer::~BridgeRenderer() = default;
  13. void BridgeRenderer::configure(const std::vector<Game::Map::Bridge> &bridges,
  14. float tileSize) {
  15. m_bridges = bridges;
  16. m_tileSize = tileSize;
  17. buildMeshes();
  18. }
  19. void BridgeRenderer::buildMeshes() {
  20. m_meshes.clear();
  21. if (m_bridges.empty()) {
  22. return;
  23. }
  24. for (const auto &bridge : m_bridges) {
  25. QVector3D dir = bridge.end - bridge.start;
  26. float length = dir.length();
  27. if (length < 0.01f) {
  28. m_meshes.push_back(nullptr);
  29. continue;
  30. }
  31. dir.normalize();
  32. QVector3D perpendicular(-dir.z(), 0.0f, dir.x());
  33. float halfWidth = bridge.width * 0.5f;
  34. int lengthSegments =
  35. static_cast<int>(std::ceil(length / (m_tileSize * 0.3f)));
  36. lengthSegments = std::max(lengthSegments, 8);
  37. std::vector<Vertex> vertices;
  38. std::vector<unsigned int> indices;
  39. for (int i = 0; i <= lengthSegments; ++i) {
  40. float t = static_cast<float>(i) / static_cast<float>(lengthSegments);
  41. QVector3D centerPos = bridge.start + dir * (length * t);
  42. float archCurve = 4.0f * t * (1.0f - t);
  43. float archHeight = bridge.height * archCurve * 0.8f;
  44. float deckHeight = bridge.start.y() + bridge.height + archHeight * 0.3f;
  45. float stoneNoise = std::sin(centerPos.x() * 3.0f) *
  46. std::cos(centerPos.z() * 2.5f) * 0.02f;
  47. for (int side = 0; side < 2; ++side) {
  48. float sideSign = (side == 0) ? -1.0f : 1.0f;
  49. QVector3D edgePos = centerPos + perpendicular * (halfWidth * sideSign);
  50. edgePos.setY(deckHeight + stoneNoise);
  51. Vertex deckVertex;
  52. deckVertex.position[0] = edgePos.x();
  53. deckVertex.position[1] = edgePos.y();
  54. deckVertex.position[2] = edgePos.z();
  55. deckVertex.normal[0] = 0.0f;
  56. deckVertex.normal[1] = 1.0f;
  57. deckVertex.normal[2] = 0.0f;
  58. deckVertex.texCoord[0] = side * 1.0f;
  59. deckVertex.texCoord[1] = t * length * 0.5f;
  60. vertices.push_back(deckVertex);
  61. }
  62. float archUnderHeight = bridge.start.y() + archHeight;
  63. for (int side = 0; side < 2; ++side) {
  64. float sideSign = (side == 0) ? -1.0f : 1.0f;
  65. float archWidth = halfWidth * 0.7f;
  66. QVector3D archPos = centerPos + perpendicular * (archWidth * sideSign);
  67. archPos.setY(archUnderHeight);
  68. Vertex archVertex;
  69. archVertex.position[0] = archPos.x();
  70. archVertex.position[1] = archPos.y();
  71. archVertex.position[2] = archPos.z();
  72. archVertex.normal[0] = perpendicular.x() * sideSign;
  73. archVertex.normal[1] = -0.5f;
  74. archVertex.normal[2] = perpendicular.z() * sideSign;
  75. archVertex.texCoord[0] = side * 1.0f;
  76. archVertex.texCoord[1] = t * length * 0.5f;
  77. vertices.push_back(archVertex);
  78. }
  79. float parapetHeight = deckHeight + 0.25f;
  80. for (int side = 0; side < 2; ++side) {
  81. float sideSign = (side == 0) ? -1.0f : 1.0f;
  82. QVector3D parapetPos =
  83. centerPos + perpendicular * (halfWidth * sideSign * 1.05f);
  84. parapetPos.setY(parapetHeight);
  85. Vertex parapetVertex;
  86. parapetVertex.position[0] = parapetPos.x();
  87. parapetVertex.position[1] = parapetPos.y();
  88. parapetVertex.position[2] = parapetPos.z();
  89. parapetVertex.normal[0] = perpendicular.x() * sideSign;
  90. parapetVertex.normal[1] = 0.0f;
  91. parapetVertex.normal[2] = perpendicular.z() * sideSign;
  92. parapetVertex.texCoord[0] = side * 1.0f;
  93. parapetVertex.texCoord[1] = t * length * 0.5f;
  94. vertices.push_back(parapetVertex);
  95. }
  96. if (i < lengthSegments) {
  97. unsigned int idx = i * 6;
  98. indices.push_back(idx + 0);
  99. indices.push_back(idx + 6);
  100. indices.push_back(idx + 1);
  101. indices.push_back(idx + 1);
  102. indices.push_back(idx + 6);
  103. indices.push_back(idx + 7);
  104. indices.push_back(idx + 2);
  105. indices.push_back(idx + 8);
  106. indices.push_back(idx + 0);
  107. indices.push_back(idx + 0);
  108. indices.push_back(idx + 8);
  109. indices.push_back(idx + 6);
  110. indices.push_back(idx + 1);
  111. indices.push_back(idx + 9);
  112. indices.push_back(idx + 3);
  113. indices.push_back(idx + 1);
  114. indices.push_back(idx + 7);
  115. indices.push_back(idx + 9);
  116. indices.push_back(idx + 0);
  117. indices.push_back(idx + 6);
  118. indices.push_back(idx + 4);
  119. indices.push_back(idx + 4);
  120. indices.push_back(idx + 6);
  121. indices.push_back(idx + 10);
  122. indices.push_back(idx + 1);
  123. indices.push_back(idx + 5);
  124. indices.push_back(idx + 7);
  125. indices.push_back(idx + 5);
  126. indices.push_back(idx + 11);
  127. indices.push_back(idx + 7);
  128. }
  129. }
  130. if (!vertices.empty() && !indices.empty()) {
  131. m_meshes.push_back(std::make_unique<Mesh>(vertices, indices));
  132. } else {
  133. m_meshes.push_back(nullptr);
  134. }
  135. }
  136. }
  137. void BridgeRenderer::submit(Renderer &renderer, ResourceManager *resources) {
  138. if (m_meshes.empty() || m_bridges.empty()) {
  139. return;
  140. }
  141. Q_UNUSED(resources);
  142. auto &visibility = Game::Map::VisibilityService::instance();
  143. const bool useVisibility = visibility.isInitialized();
  144. auto shader = renderer.getShader("bridge");
  145. if (!shader) {
  146. shader = renderer.getShader("basic");
  147. if (!shader) {
  148. return;
  149. }
  150. }
  151. renderer.setCurrentShader(shader);
  152. QMatrix4x4 model;
  153. model.setToIdentity();
  154. QVector3D stoneColor(0.55f, 0.52f, 0.48f);
  155. size_t meshIndex = 0;
  156. for (const auto &bridge : m_bridges) {
  157. if (meshIndex >= m_meshes.size())
  158. break;
  159. auto *mesh = m_meshes[meshIndex].get();
  160. ++meshIndex;
  161. if (!mesh) {
  162. continue;
  163. }
  164. QVector3D dir = bridge.end - bridge.start;
  165. float length = dir.length();
  166. float alpha = 1.0f;
  167. QVector3D colorMultiplier(1.0f, 1.0f, 1.0f);
  168. if (useVisibility) {
  169. int maxVisibilityState = 0;
  170. dir.normalize();
  171. int samplesPerBridge = 5;
  172. for (int i = 0; i < samplesPerBridge; ++i) {
  173. float t =
  174. static_cast<float>(i) / static_cast<float>(samplesPerBridge - 1);
  175. QVector3D pos = bridge.start + dir * (length * t);
  176. if (visibility.isVisibleWorld(pos.x(), pos.z())) {
  177. maxVisibilityState = 2;
  178. break;
  179. } else if (visibility.isExploredWorld(pos.x(), pos.z())) {
  180. maxVisibilityState = std::max(maxVisibilityState, 1);
  181. }
  182. }
  183. if (maxVisibilityState == 0) {
  184. continue;
  185. } else if (maxVisibilityState == 1) {
  186. alpha = 0.5f;
  187. colorMultiplier = QVector3D(0.4f, 0.4f, 0.45f);
  188. }
  189. }
  190. QVector3D finalColor(stoneColor.x() * colorMultiplier.x(),
  191. stoneColor.y() * colorMultiplier.y(),
  192. stoneColor.z() * colorMultiplier.z());
  193. renderer.mesh(mesh, model, finalColor, nullptr, alpha);
  194. }
  195. renderer.setCurrentShader(nullptr);
  196. }
  197. } // namespace Render::GL