rain_pipeline.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include "rain_pipeline.h"
  2. #include "../../ground/rain_gpu.h"
  3. #include "../backend.h"
  4. #include "../camera.h"
  5. #include "../render_constants.h"
  6. #include "../shader_cache.h"
  7. #include <QDebug>
  8. #include <QOpenGLContext>
  9. #include <cmath>
  10. #include <numbers>
  11. #include <random>
  12. namespace Render::GL::BackendPipelines {
  13. using namespace Render::GL::VertexAttrib;
  14. using namespace Render::GL::ComponentCount;
  15. namespace {
  16. constexpr float kRainColorR = 0.7F;
  17. constexpr float kRainColorG = 0.75F;
  18. constexpr float kRainColorB = 0.85F;
  19. constexpr float kSnowColorR = 1.0F;
  20. constexpr float kSnowColorG = 1.0F;
  21. constexpr float kSnowColorB = 1.0F;
  22. void clear_gl_errors() {
  23. while (glGetError() != GL_NO_ERROR) {
  24. }
  25. }
  26. auto check_gl_error(const char *operation) -> bool {
  27. GLenum err = glGetError();
  28. if (err != GL_NO_ERROR) {
  29. qWarning() << "RainPipeline GL error in" << operation << ":" << err;
  30. return false;
  31. }
  32. return true;
  33. }
  34. } // namespace
  35. auto RainPipeline::initialize() -> bool {
  36. if (m_shader_cache == nullptr) {
  37. qWarning() << "RainPipeline::initialize: null ShaderCache";
  38. return false;
  39. }
  40. initializeOpenGLFunctions();
  41. clear_gl_errors();
  42. m_rain_shader = m_shader_cache->get("rain");
  43. if (m_rain_shader == nullptr) {
  44. m_rain_shader = m_shader_cache->load(
  45. "rain", QStringLiteral(":/assets/shaders/rain.vert"),
  46. QStringLiteral(":/assets/shaders/rain.frag"));
  47. }
  48. if (m_rain_shader == nullptr) {
  49. qWarning() << "RainPipeline: Failed to get rain shader";
  50. return false;
  51. }
  52. cache_uniforms();
  53. generate_rain_drops();
  54. if (!create_rain_geometry()) {
  55. qWarning() << "RainPipeline: Failed to create rain geometry";
  56. return false;
  57. }
  58. qInfo() << "RainPipeline initialized successfully";
  59. return is_initialized();
  60. }
  61. void RainPipeline::shutdown() {
  62. shutdown_geometry();
  63. m_rain_shader = nullptr;
  64. m_rain_drops.clear();
  65. }
  66. void RainPipeline::shutdown_geometry() {
  67. if (QOpenGLContext::currentContext() == nullptr) {
  68. m_vao = 0;
  69. m_vertex_buffer = 0;
  70. m_index_buffer = 0;
  71. m_index_count = 0;
  72. return;
  73. }
  74. initializeOpenGLFunctions();
  75. clear_gl_errors();
  76. if (m_vao != 0) {
  77. glDeleteVertexArrays(1, &m_vao);
  78. m_vao = 0;
  79. }
  80. if (m_vertex_buffer != 0) {
  81. glDeleteBuffers(1, &m_vertex_buffer);
  82. m_vertex_buffer = 0;
  83. }
  84. if (m_index_buffer != 0) {
  85. glDeleteBuffers(1, &m_index_buffer);
  86. m_index_buffer = 0;
  87. }
  88. m_index_count = 0;
  89. }
  90. void RainPipeline::cache_uniforms() {
  91. if (m_rain_shader == nullptr) {
  92. return;
  93. }
  94. m_uniforms.view_proj = m_rain_shader->uniform_handle("u_view_proj");
  95. m_uniforms.time = m_rain_shader->uniform_handle("u_time");
  96. m_uniforms.intensity = m_rain_shader->uniform_handle("u_intensity");
  97. m_uniforms.camera_pos = m_rain_shader->uniform_handle("u_camera_pos");
  98. m_uniforms.rain_color = m_rain_shader->uniform_handle("u_rain_color");
  99. m_uniforms.wind = m_rain_shader->uniform_handle("u_wind");
  100. m_uniforms.weather_type = m_rain_shader->uniform_handle("u_weather_type");
  101. m_uniforms.wind_strength = m_rain_shader->uniform_handle("u_wind_strength");
  102. }
  103. auto RainPipeline::is_initialized() const -> bool {
  104. return m_rain_shader != nullptr && m_vao != 0 && m_index_count > 0;
  105. }
  106. void RainPipeline::generate_rain_drops() {
  107. m_rain_drops.clear();
  108. m_rain_drops.reserve(k_max_drops);
  109. std::mt19937 rng(42);
  110. std::uniform_real_distribution<float> dist_xz(-k_area_radius, k_area_radius);
  111. std::uniform_real_distribution<float> dist_y(0.0F, k_area_height);
  112. std::uniform_real_distribution<float> dist_speed(0.8F, 1.2F);
  113. std::uniform_real_distribution<float> dist_alpha(0.3F, 0.7F);
  114. for (std::size_t i = 0; i < k_max_drops; ++i) {
  115. RainDropData drop;
  116. drop.position = QVector3D(dist_xz(rng), dist_y(rng), dist_xz(rng));
  117. drop.speed = k_drop_speed * dist_speed(rng);
  118. drop.length = k_drop_length;
  119. drop.alpha = dist_alpha(rng);
  120. m_rain_drops.push_back(drop);
  121. }
  122. }
  123. struct RainVertex {
  124. float position[3];
  125. float offset[3];
  126. float alpha;
  127. };
  128. auto RainPipeline::create_rain_geometry() -> bool {
  129. initializeOpenGLFunctions();
  130. shutdown_geometry();
  131. clear_gl_errors();
  132. std::vector<RainVertex> vertices;
  133. std::vector<unsigned int> indices;
  134. vertices.reserve(m_rain_drops.size() * 2);
  135. indices.reserve(m_rain_drops.size() * 2);
  136. for (std::size_t i = 0; i < m_rain_drops.size(); ++i) {
  137. const auto &drop = m_rain_drops[i];
  138. RainVertex top;
  139. top.position[0] = drop.position.x();
  140. top.position[1] = drop.position.y();
  141. top.position[2] = drop.position.z();
  142. top.offset[0] = 0.0F;
  143. top.offset[1] = 0.0F;
  144. top.offset[2] = drop.speed;
  145. top.alpha = drop.alpha;
  146. vertices.push_back(top);
  147. RainVertex bottom;
  148. bottom.position[0] = drop.position.x();
  149. bottom.position[1] = drop.position.y() - drop.length;
  150. bottom.position[2] = drop.position.z();
  151. bottom.offset[0] = 0.0F;
  152. bottom.offset[1] = -drop.length;
  153. bottom.offset[2] = drop.speed;
  154. bottom.alpha = drop.alpha * 0.3F;
  155. vertices.push_back(bottom);
  156. auto base = static_cast<unsigned int>(i * 2);
  157. indices.push_back(base);
  158. indices.push_back(base + 1);
  159. }
  160. glGenVertexArrays(1, &m_vao);
  161. if (!check_gl_error("glGenVertexArrays") || m_vao == 0) {
  162. return false;
  163. }
  164. glBindVertexArray(m_vao);
  165. if (!check_gl_error("glBindVertexArray")) {
  166. glDeleteVertexArrays(1, &m_vao);
  167. m_vao = 0;
  168. return false;
  169. }
  170. glGenBuffers(1, &m_vertex_buffer);
  171. glBindBuffer(GL_ARRAY_BUFFER, m_vertex_buffer);
  172. glBufferData(GL_ARRAY_BUFFER,
  173. static_cast<GLsizeiptr>(vertices.size() * sizeof(RainVertex)),
  174. vertices.data(), GL_STATIC_DRAW);
  175. if (!check_gl_error("vertex buffer")) {
  176. shutdown_geometry();
  177. return false;
  178. }
  179. glGenBuffers(1, &m_index_buffer);
  180. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_index_buffer);
  181. glBufferData(GL_ELEMENT_ARRAY_BUFFER,
  182. static_cast<GLsizeiptr>(indices.size() * sizeof(unsigned int)),
  183. indices.data(), GL_STATIC_DRAW);
  184. if (!check_gl_error("index buffer")) {
  185. shutdown_geometry();
  186. return false;
  187. }
  188. m_index_count = static_cast<GLsizei>(indices.size());
  189. glEnableVertexAttribArray(VertexAttrib::Position);
  190. glVertexAttribPointer(
  191. VertexAttrib::Position, ComponentCount::Vec3, GL_FLOAT, GL_FALSE,
  192. sizeof(RainVertex),
  193. reinterpret_cast<void *>(offsetof(RainVertex, position)));
  194. glEnableVertexAttribArray(VertexAttrib::Normal);
  195. glVertexAttribPointer(VertexAttrib::Normal, ComponentCount::Vec3, GL_FLOAT,
  196. GL_FALSE, sizeof(RainVertex),
  197. reinterpret_cast<void *>(offsetof(RainVertex, offset)));
  198. glEnableVertexAttribArray(VertexAttrib::TexCoord);
  199. glVertexAttribPointer(VertexAttrib::TexCoord, 1, GL_FLOAT, GL_FALSE,
  200. sizeof(RainVertex),
  201. reinterpret_cast<void *>(offsetof(RainVertex, alpha)));
  202. glBindVertexArray(0);
  203. if (!check_gl_error("vertex attributes")) {
  204. shutdown_geometry();
  205. return false;
  206. }
  207. return true;
  208. }
  209. void RainPipeline::render(const Camera &cam, const RainBatchParams &params) {
  210. if (!is_initialized() || params.intensity < 0.01F) {
  211. return;
  212. }
  213. initializeOpenGLFunctions();
  214. clear_gl_errors();
  215. GLboolean depth_mask_enabled = GL_TRUE;
  216. glGetBooleanv(GL_DEPTH_WRITEMASK, &depth_mask_enabled);
  217. GLboolean blend_enabled = glIsEnabled(GL_BLEND);
  218. GLboolean depth_test_enabled = glIsEnabled(GL_DEPTH_TEST);
  219. glEnable(GL_DEPTH_TEST);
  220. glDepthMask(GL_FALSE);
  221. glEnable(GL_BLEND);
  222. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  223. if (params.weather_type == Game::Map::WeatherType::Snow) {
  224. glEnable(GL_PROGRAM_POINT_SIZE);
  225. }
  226. m_rain_shader->use();
  227. glBindVertexArray(m_vao);
  228. QMatrix4x4 view_proj = cam.get_projection_matrix() * cam.get_view_matrix();
  229. QVector3D camera_pos = cam.get_position();
  230. QVector3D particle_color;
  231. if (params.weather_type == Game::Map::WeatherType::Snow) {
  232. particle_color = QVector3D(kSnowColorR, kSnowColorG, kSnowColorB);
  233. } else {
  234. particle_color = QVector3D(kRainColorR, kRainColorG, kRainColorB);
  235. }
  236. m_rain_shader->set_uniform(m_uniforms.view_proj, view_proj);
  237. m_rain_shader->set_uniform(m_uniforms.time, params.time);
  238. m_rain_shader->set_uniform(m_uniforms.intensity, params.intensity);
  239. m_rain_shader->set_uniform(m_uniforms.camera_pos, camera_pos);
  240. m_rain_shader->set_uniform(m_uniforms.rain_color, particle_color);
  241. m_rain_shader->set_uniform(m_uniforms.wind, params.wind_direction);
  242. m_rain_shader->set_uniform(m_uniforms.weather_type,
  243. static_cast<int>(params.weather_type));
  244. m_rain_shader->set_uniform(m_uniforms.wind_strength, params.wind_strength);
  245. if (params.weather_type == Game::Map::WeatherType::Snow) {
  246. glDrawElements(GL_POINTS, m_index_count / 2, GL_UNSIGNED_INT, nullptr);
  247. } else {
  248. glDrawElements(GL_LINES, m_index_count, GL_UNSIGNED_INT, nullptr);
  249. }
  250. glBindVertexArray(0);
  251. if (params.weather_type == Game::Map::WeatherType::Snow) {
  252. glDisable(GL_PROGRAM_POINT_SIZE);
  253. }
  254. glDepthMask(depth_mask_enabled);
  255. if (!blend_enabled) {
  256. glDisable(GL_BLEND);
  257. }
  258. if (!depth_test_enabled) {
  259. glDisable(GL_DEPTH_TEST);
  260. }
  261. }
  262. } // namespace Render::GL::BackendPipelines