vegetation_pipeline.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. #include "vegetation_pipeline.h"
  2. #include "../render_constants.h"
  3. #include "gl/shader_cache.h"
  4. #include <GL/gl.h>
  5. #include <QDebug>
  6. #include <QOpenGLContext>
  7. #include <cmath>
  8. #include <cstddef>
  9. #include <cstdint>
  10. #include <qglobal.h>
  11. #include <qopenglext.h>
  12. #include <qstringliteral.h>
  13. #include <qvectornd.h>
  14. #include <vector>
  15. namespace Render::GL::BackendPipelines {
  16. using namespace Render::GL::VertexAttrib;
  17. using namespace Render::GL::ComponentCount;
  18. using namespace Render::GL::Geometry;
  19. VegetationPipeline::VegetationPipeline(ShaderCache *shaderCache)
  20. : m_shaderCache(shaderCache) {}
  21. VegetationPipeline::~VegetationPipeline() { shutdown(); }
  22. auto VegetationPipeline::initialize() -> bool {
  23. initializeOpenGLFunctions();
  24. if (m_shaderCache == nullptr) {
  25. return false;
  26. }
  27. m_stoneShader = m_shaderCache->get(QStringLiteral("stone_instanced"));
  28. m_plantShader = m_shaderCache->get(QStringLiteral("plant_instanced"));
  29. m_pineShader = m_shaderCache->get(QStringLiteral("pine_instanced"));
  30. m_firecampShader = m_shaderCache->get(QStringLiteral("firecamp"));
  31. if (m_stoneShader == nullptr) {
  32. qWarning() << "VegetationPipeline: stone shader missing";
  33. }
  34. if (m_plantShader == nullptr) {
  35. qWarning() << "VegetationPipeline: plant shader missing";
  36. }
  37. if (m_pineShader == nullptr) {
  38. qWarning() << "VegetationPipeline: pine shader missing";
  39. }
  40. if (m_firecampShader == nullptr) {
  41. qWarning() << "VegetationPipeline: firecamp shader missing";
  42. }
  43. initializeStonePipeline();
  44. initializePlantPipeline();
  45. initializePinePipeline();
  46. initializeFireCampPipeline();
  47. cacheUniforms();
  48. m_initialized = true;
  49. return true;
  50. }
  51. void VegetationPipeline::shutdown() {
  52. shutdownStonePipeline();
  53. shutdownPlantPipeline();
  54. shutdownPinePipeline();
  55. shutdownFireCampPipeline();
  56. m_initialized = false;
  57. }
  58. void VegetationPipeline::cacheUniforms() {
  59. if (m_stoneShader != nullptr) {
  60. m_stoneUniforms.view_proj = m_stoneShader->uniformHandle("uViewProj");
  61. m_stoneUniforms.light_direction =
  62. m_stoneShader->uniformHandle("uLightDirection");
  63. }
  64. if (m_plantShader != nullptr) {
  65. m_plantUniforms.view_proj = m_plantShader->uniformHandle("uViewProj");
  66. m_plantUniforms.time = m_plantShader->uniformHandle("uTime");
  67. m_plantUniforms.windStrength =
  68. m_plantShader->uniformHandle("uWindStrength");
  69. m_plantUniforms.windSpeed = m_plantShader->uniformHandle("uWindSpeed");
  70. m_plantUniforms.light_direction =
  71. m_plantShader->uniformHandle("uLightDirection");
  72. }
  73. if (m_pineShader != nullptr) {
  74. m_pineUniforms.view_proj = m_pineShader->uniformHandle("uViewProj");
  75. m_pineUniforms.time = m_pineShader->uniformHandle("uTime");
  76. m_pineUniforms.windStrength = m_pineShader->uniformHandle("uWindStrength");
  77. m_pineUniforms.windSpeed = m_pineShader->uniformHandle("uWindSpeed");
  78. m_pineUniforms.light_direction =
  79. m_pineShader->uniformHandle("uLightDirection");
  80. }
  81. if (m_firecampShader != nullptr) {
  82. m_firecampUniforms.view_proj =
  83. m_firecampShader->uniformHandle("u_viewProj");
  84. m_firecampUniforms.time = m_firecampShader->uniformHandle("u_time");
  85. m_firecampUniforms.flickerSpeed =
  86. m_firecampShader->uniformHandle("u_flickerSpeed");
  87. m_firecampUniforms.flickerAmount =
  88. m_firecampShader->uniformHandle("u_flickerAmount");
  89. m_firecampUniforms.glowStrength =
  90. m_firecampShader->uniformHandle("u_glowStrength");
  91. m_firecampUniforms.fireTexture =
  92. m_firecampShader->uniformHandle("fireTexture");
  93. m_firecampUniforms.camera_right =
  94. m_firecampShader->uniformHandle("u_cameraRight");
  95. m_firecampUniforms.camera_forward =
  96. m_firecampShader->uniformHandle("u_cameraForward");
  97. }
  98. }
  99. void VegetationPipeline::initializeStonePipeline() {
  100. initializeOpenGLFunctions();
  101. shutdownStonePipeline();
  102. struct StoneVertex {
  103. QVector3D position;
  104. QVector3D normal;
  105. };
  106. const StoneVertex stone_vertices[] = {
  107. {{-0.5F, -0.5F, 0.5F}, {0.0F, 0.0F, 1.0F}},
  108. {{0.5F, -0.5F, 0.5F}, {0.0F, 0.0F, 1.0F}},
  109. {{0.5F, 0.5F, 0.5F}, {0.0F, 0.0F, 1.0F}},
  110. {{-0.5F, 0.5F, 0.5F}, {0.0F, 0.0F, 1.0F}},
  111. {{-0.5F, -0.5F, -0.5F}, {0.0F, 0.0F, -1.0F}},
  112. {{-0.5F, 0.5F, -0.5F}, {0.0F, 0.0F, -1.0F}},
  113. {{0.5F, 0.5F, -0.5F}, {0.0F, 0.0F, -1.0F}},
  114. {{0.5F, -0.5F, -0.5F}, {0.0F, 0.0F, -1.0F}},
  115. {{-0.5F, 0.5F, -0.5F}, {0.0F, 1.0F, 0.0F}},
  116. {{-0.5F, 0.5F, 0.5F}, {0.0F, 1.0F, 0.0F}},
  117. {{0.5F, 0.5F, 0.5F}, {0.0F, 1.0F, 0.0F}},
  118. {{0.5F, 0.5F, -0.5F}, {0.0F, 1.0F, 0.0F}},
  119. {{-0.5F, -0.5F, -0.5F}, {0.0F, -1.0F, 0.0F}},
  120. {{0.5F, -0.5F, -0.5F}, {0.0F, -1.0F, 0.0F}},
  121. {{0.5F, -0.5F, 0.5F}, {0.0F, -1.0F, 0.0F}},
  122. {{-0.5F, -0.5F, 0.5F}, {0.0F, -1.0F, 0.0F}},
  123. {{0.5F, -0.5F, -0.5F}, {1.0F, 0.0F, 0.0F}},
  124. {{0.5F, 0.5F, -0.5F}, {1.0F, 0.0F, 0.0F}},
  125. {{0.5F, 0.5F, 0.5F}, {1.0F, 0.0F, 0.0F}},
  126. {{0.5F, -0.5F, 0.5F}, {1.0F, 0.0F, 0.0F}},
  127. {{-0.5F, -0.5F, -0.5F}, {-1.0F, 0.0F, 0.0F}},
  128. {{-0.5F, -0.5F, 0.5F}, {-1.0F, 0.0F, 0.0F}},
  129. {{-0.5F, 0.5F, 0.5F}, {-1.0F, 0.0F, 0.0F}},
  130. {{-0.5F, 0.5F, -0.5F}, {-1.0F, 0.0F, 0.0F}},
  131. };
  132. const uint16_t stone_indices[] = {
  133. 0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4, 8, 9, 10, 10, 11, 8,
  134. 12, 13, 14, 14, 15, 12, 16, 17, 18, 18, 19, 16, 20, 21, 22, 22, 23, 20};
  135. glGenVertexArrays(1, &m_stoneVao);
  136. glBindVertexArray(m_stoneVao);
  137. glGenBuffers(1, &m_stoneVertexBuffer);
  138. glBindBuffer(GL_ARRAY_BUFFER, m_stoneVertexBuffer);
  139. glBufferData(GL_ARRAY_BUFFER, sizeof(stone_vertices), stone_vertices,
  140. GL_STATIC_DRAW);
  141. m_stoneVertexCount = CubeVertexCount;
  142. glGenBuffers(1, &m_stoneIndexBuffer);
  143. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_stoneIndexBuffer);
  144. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(stone_indices), stone_indices,
  145. GL_STATIC_DRAW);
  146. constexpr int k_stone_index_count = 36;
  147. m_stoneIndexCount = k_stone_index_count;
  148. glEnableVertexAttribArray(Position);
  149. glVertexAttribPointer(
  150. Position, Vec3, GL_FLOAT, GL_FALSE, sizeof(StoneVertex),
  151. reinterpret_cast<void *>(offsetof(StoneVertex, position)));
  152. glEnableVertexAttribArray(Normal);
  153. glVertexAttribPointer(
  154. Normal, Vec3, GL_FLOAT, GL_FALSE, sizeof(StoneVertex),
  155. reinterpret_cast<void *>(offsetof(StoneVertex, normal)));
  156. glEnableVertexAttribArray(TexCoord);
  157. glVertexAttribDivisor(TexCoord, 1);
  158. glEnableVertexAttribArray(InstancePosition);
  159. glVertexAttribDivisor(InstancePosition, 1);
  160. glBindVertexArray(0);
  161. glBindBuffer(GL_ARRAY_BUFFER, 0);
  162. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  163. }
  164. void VegetationPipeline::shutdownStonePipeline() {
  165. if (QOpenGLContext::currentContext() == nullptr) {
  166. m_stoneVao = 0;
  167. m_stoneVertexBuffer = 0;
  168. m_stoneIndexBuffer = 0;
  169. m_stoneVertexCount = 0;
  170. m_stoneIndexCount = 0;
  171. return;
  172. }
  173. initializeOpenGLFunctions();
  174. if (m_stoneIndexBuffer != 0U) {
  175. glDeleteBuffers(1, &m_stoneIndexBuffer);
  176. m_stoneIndexBuffer = 0;
  177. }
  178. if (m_stoneVertexBuffer != 0U) {
  179. glDeleteBuffers(1, &m_stoneVertexBuffer);
  180. m_stoneVertexBuffer = 0;
  181. }
  182. if (m_stoneVao != 0U) {
  183. glDeleteVertexArrays(1, &m_stoneVao);
  184. m_stoneVao = 0;
  185. }
  186. m_stoneVertexCount = 0;
  187. m_stoneIndexCount = 0;
  188. }
  189. void VegetationPipeline::initializePlantPipeline() {
  190. initializeOpenGLFunctions();
  191. shutdownPlantPipeline();
  192. struct PlantVertex {
  193. QVector3D position;
  194. QVector2D tex_coord;
  195. QVector3D normal;
  196. };
  197. const PlantVertex plant_vertices[] = {
  198. {{-0.5F, 0.0F, 0.0F}, {0.0F, 0.0F}, {0.0F, 0.0F, 1.0F}},
  199. {{0.5F, 0.0F, 0.0F}, {1.0F, 0.0F}, {0.0F, 0.0F, 1.0F}},
  200. {{0.5F, 1.0F, 0.0F}, {1.0F, 1.0F}, {0.0F, 0.0F, 1.0F}},
  201. {{-0.5F, 1.0F, 0.0F}, {0.0F, 1.0F}, {0.0F, 0.0F, 1.0F}},
  202. {{0.5F, 0.0F, 0.0F}, {0.0F, 0.0F}, {0.0F, 0.0F, -1.0F}},
  203. {{-0.5F, 0.0F, 0.0F}, {1.0F, 0.0F}, {0.0F, 0.0F, -1.0F}},
  204. {{-0.5F, 1.0F, 0.0F}, {1.0F, 1.0F}, {0.0F, 0.0F, -1.0F}},
  205. {{0.5F, 1.0F, 0.0F}, {0.0F, 1.0F}, {0.0F, 0.0F, -1.0F}},
  206. {{0.0F, 0.0F, -0.5F}, {0.0F, 0.0F}, {1.0F, 0.0F, 0.0F}},
  207. {{0.0F, 0.0F, 0.5F}, {1.0F, 0.0F}, {1.0F, 0.0F, 0.0F}},
  208. {{0.0F, 1.0F, 0.5F}, {1.0F, 1.0F}, {1.0F, 0.0F, 0.0F}},
  209. {{0.0F, 1.0F, -0.5F}, {0.0F, 1.0F}, {1.0F, 0.0F, 0.0F}},
  210. {{0.0F, 0.0F, 0.5F}, {0.0F, 0.0F}, {-1.0F, 0.0F, 0.0F}},
  211. {{0.0F, 0.0F, -0.5F}, {1.0F, 0.0F}, {-1.0F, 0.0F, 0.0F}},
  212. {{0.0F, 1.0F, -0.5F}, {1.0F, 1.0F}, {-1.0F, 0.0F, 0.0F}},
  213. {{0.0F, 1.0F, 0.5F}, {0.0F, 1.0F}, {-1.0F, 0.0F, 0.0F}},
  214. };
  215. const unsigned short plant_indices[] = {
  216. 0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7,
  217. 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15,
  218. };
  219. glGenVertexArrays(1, &m_plantVao);
  220. glBindVertexArray(m_plantVao);
  221. glGenBuffers(1, &m_plantVertexBuffer);
  222. glBindBuffer(GL_ARRAY_BUFFER, m_plantVertexBuffer);
  223. glBufferData(GL_ARRAY_BUFFER, sizeof(plant_vertices), plant_vertices,
  224. GL_STATIC_DRAW);
  225. m_plantVertexCount = PlantCrossQuadVertexCount;
  226. glEnableVertexAttribArray(Position);
  227. glVertexAttribPointer(
  228. Position, Vec3, GL_FLOAT, GL_FALSE, sizeof(PlantVertex),
  229. reinterpret_cast<void *>(offsetof(PlantVertex, position)));
  230. glEnableVertexAttribArray(Normal);
  231. glVertexAttribPointer(
  232. Normal, Vec2, GL_FLOAT, GL_FALSE, sizeof(PlantVertex),
  233. reinterpret_cast<void *>(offsetof(PlantVertex, tex_coord)));
  234. glEnableVertexAttribArray(TexCoord);
  235. glVertexAttribPointer(
  236. TexCoord, Vec3, GL_FLOAT, GL_FALSE, sizeof(PlantVertex),
  237. reinterpret_cast<void *>(offsetof(PlantVertex, normal)));
  238. glGenBuffers(1, &m_plantIndexBuffer);
  239. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_plantIndexBuffer);
  240. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(plant_indices), plant_indices,
  241. GL_STATIC_DRAW);
  242. m_plantIndexCount = PlantCrossQuadIndexCount;
  243. glEnableVertexAttribArray(InstancePosition);
  244. glVertexAttribDivisor(InstancePosition, 1);
  245. glEnableVertexAttribArray(InstanceScale);
  246. glVertexAttribDivisor(InstanceScale, 1);
  247. glEnableVertexAttribArray(InstanceColor);
  248. glVertexAttribDivisor(InstanceColor, 1);
  249. glBindVertexArray(0);
  250. glBindBuffer(GL_ARRAY_BUFFER, 0);
  251. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  252. }
  253. void VegetationPipeline::shutdownPlantPipeline() {
  254. if (QOpenGLContext::currentContext() == nullptr) {
  255. m_plantVao = 0;
  256. m_plantVertexBuffer = 0;
  257. m_plantIndexBuffer = 0;
  258. m_plantVertexCount = 0;
  259. m_plantIndexCount = 0;
  260. return;
  261. }
  262. initializeOpenGLFunctions();
  263. if (m_plantIndexBuffer != 0U) {
  264. glDeleteBuffers(1, &m_plantIndexBuffer);
  265. m_plantIndexBuffer = 0;
  266. }
  267. if (m_plantVertexBuffer != 0U) {
  268. glDeleteBuffers(1, &m_plantVertexBuffer);
  269. m_plantVertexBuffer = 0;
  270. }
  271. if (m_plantVao != 0U) {
  272. glDeleteVertexArrays(1, &m_plantVao);
  273. m_plantVao = 0;
  274. }
  275. m_plantVertexCount = 0;
  276. m_plantIndexCount = 0;
  277. }
  278. void VegetationPipeline::initializePinePipeline() {
  279. initializeOpenGLFunctions();
  280. shutdownPinePipeline();
  281. struct PineVertex {
  282. QVector3D position;
  283. QVector2D tex_coord;
  284. QVector3D normal;
  285. };
  286. constexpr int k_segments = PineTreeSegments;
  287. constexpr float k_two_pi = 6.28318530718F;
  288. std::vector<PineVertex> vertices;
  289. vertices.reserve(k_segments * 5 + 1);
  290. std::vector<unsigned short> indices;
  291. indices.reserve(k_segments * 6 * 4 + k_segments * 3);
  292. auto add_ring = [&](float radius, float y, float normalUp,
  293. float v_coord) -> int {
  294. const int start = static_cast<int>(vertices.size());
  295. for (int i = 0; i < k_segments; ++i) {
  296. const float t = static_cast<float>(i) / static_cast<float>(k_segments);
  297. const float angle = t * k_two_pi;
  298. const float nx = std::cos(angle);
  299. const float nz = std::sin(angle);
  300. QVector3D normal(nx, normalUp, nz);
  301. normal.normalize();
  302. QVector3D const position(radius * nx, y, radius * nz);
  303. QVector2D const tex_coord(t, v_coord);
  304. vertices.push_back({position, tex_coord, normal});
  305. }
  306. return start;
  307. };
  308. auto connect_rings = [&](int lowerStart, int upperStart) {
  309. for (int i = 0; i < k_segments; ++i) {
  310. const int next = (i + 1) % k_segments;
  311. const auto lower0 = static_cast<unsigned short>(lowerStart + i);
  312. const auto lower1 = static_cast<unsigned short>(lowerStart + next);
  313. const auto upper0 = static_cast<unsigned short>(upperStart + i);
  314. const auto upper1 = static_cast<unsigned short>(upperStart + next);
  315. indices.push_back(lower0);
  316. indices.push_back(lower1);
  317. indices.push_back(upper1);
  318. indices.push_back(lower0);
  319. indices.push_back(upper1);
  320. indices.push_back(upper0);
  321. }
  322. };
  323. const int trunk_bottom = add_ring(0.12F, 0.0F, 0.0F, 0.0F);
  324. const int trunk_mid = add_ring(0.11F, 0.35F, 0.0F, 0.12F);
  325. const int trunk_top = add_ring(0.10F, 0.58F, 0.05F, 0.30F);
  326. const int branch_base = add_ring(0.60F, 0.64F, 0.35F, 0.46F);
  327. const int branch_mid = add_ring(0.42F, 0.82F, 0.6F, 0.68F);
  328. const int branch_upper = add_ring(0.24F, 1.00F, 0.7F, 0.88F);
  329. const int branch_tip = add_ring(0.12F, 1.10F, 0.85F, 0.96F);
  330. connect_rings(trunk_bottom, trunk_mid);
  331. connect_rings(trunk_mid, trunk_top);
  332. connect_rings(trunk_top, branch_base);
  333. connect_rings(branch_base, branch_mid);
  334. connect_rings(branch_mid, branch_upper);
  335. connect_rings(branch_upper, branch_tip);
  336. const auto trunk_cap_index = static_cast<unsigned short>(vertices.size());
  337. vertices.push_back({QVector3D(0.0F, 0.0F, 0.0F), QVector2D(0.5F, 0.0F),
  338. QVector3D(0.0F, -1.0F, 0.0F)});
  339. for (int i = 0; i < k_segments; ++i) {
  340. const int next = (i + 1) % k_segments;
  341. indices.push_back(static_cast<unsigned short>(trunk_bottom + next));
  342. indices.push_back(static_cast<unsigned short>(trunk_bottom + i));
  343. indices.push_back(trunk_cap_index);
  344. }
  345. const auto apex_index = static_cast<unsigned short>(vertices.size());
  346. vertices.push_back({QVector3D(0.0F, 1.18F, 0.0F), QVector2D(0.5F, 1.0F),
  347. QVector3D(0.0F, 1.0F, 0.0F)});
  348. for (int i = 0; i < k_segments; ++i) {
  349. const int next = (i + 1) % k_segments;
  350. indices.push_back(static_cast<unsigned short>(branch_tip + i));
  351. indices.push_back(static_cast<unsigned short>(branch_tip + next));
  352. indices.push_back(apex_index);
  353. }
  354. glGenVertexArrays(1, &m_pineVao);
  355. glBindVertexArray(m_pineVao);
  356. glGenBuffers(1, &m_pineVertexBuffer);
  357. glBindBuffer(GL_ARRAY_BUFFER, m_pineVertexBuffer);
  358. glBufferData(GL_ARRAY_BUFFER,
  359. static_cast<GLsizeiptr>(vertices.size() * sizeof(PineVertex)),
  360. vertices.data(), GL_STATIC_DRAW);
  361. m_pineVertexCount = static_cast<GLsizei>(vertices.size());
  362. glEnableVertexAttribArray(Position);
  363. glVertexAttribPointer(
  364. Position, Vec3, GL_FLOAT, GL_FALSE, sizeof(PineVertex),
  365. reinterpret_cast<void *>(offsetof(PineVertex, position)));
  366. glEnableVertexAttribArray(Normal);
  367. glVertexAttribPointer(
  368. Normal, Vec2, GL_FLOAT, GL_FALSE, sizeof(PineVertex),
  369. reinterpret_cast<void *>(offsetof(PineVertex, tex_coord)));
  370. glEnableVertexAttribArray(TexCoord);
  371. glVertexAttribPointer(TexCoord, Vec3, GL_FLOAT, GL_FALSE, sizeof(PineVertex),
  372. reinterpret_cast<void *>(offsetof(PineVertex, normal)));
  373. glGenBuffers(1, &m_pineIndexBuffer);
  374. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_pineIndexBuffer);
  375. glBufferData(GL_ELEMENT_ARRAY_BUFFER,
  376. static_cast<GLsizeiptr>(indices.size() * sizeof(unsigned short)),
  377. indices.data(), GL_STATIC_DRAW);
  378. m_pineIndexCount = static_cast<GLsizei>(indices.size());
  379. glEnableVertexAttribArray(InstancePosition);
  380. glVertexAttribDivisor(InstancePosition, 1);
  381. glEnableVertexAttribArray(InstanceScale);
  382. glVertexAttribDivisor(InstanceScale, 1);
  383. glEnableVertexAttribArray(InstanceColor);
  384. glVertexAttribDivisor(InstanceColor, 1);
  385. glBindVertexArray(0);
  386. glBindBuffer(GL_ARRAY_BUFFER, 0);
  387. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  388. }
  389. void VegetationPipeline::shutdownPinePipeline() {
  390. if (QOpenGLContext::currentContext() == nullptr) {
  391. m_pineVao = 0;
  392. m_pineVertexBuffer = 0;
  393. m_pineIndexBuffer = 0;
  394. m_pineVertexCount = 0;
  395. m_pineIndexCount = 0;
  396. return;
  397. }
  398. initializeOpenGLFunctions();
  399. if (m_pineIndexBuffer != 0U) {
  400. glDeleteBuffers(1, &m_pineIndexBuffer);
  401. m_pineIndexBuffer = 0;
  402. }
  403. if (m_pineVertexBuffer != 0U) {
  404. glDeleteBuffers(1, &m_pineVertexBuffer);
  405. m_pineVertexBuffer = 0;
  406. }
  407. if (m_pineVao != 0U) {
  408. glDeleteVertexArrays(1, &m_pineVao);
  409. m_pineVao = 0;
  410. }
  411. m_pineVertexCount = 0;
  412. m_pineIndexCount = 0;
  413. }
  414. void VegetationPipeline::initializeFireCampPipeline() {
  415. initializeOpenGLFunctions();
  416. shutdownFireCampPipeline();
  417. struct FireCampVertex {
  418. QVector3D position;
  419. QVector2D tex_coord;
  420. };
  421. constexpr std::size_t k_firecamp_vertex_reserve = 12;
  422. constexpr std::size_t k_firecamp_index_reserve = 18;
  423. std::vector<FireCampVertex> vertices;
  424. vertices.reserve(k_firecamp_vertex_reserve);
  425. std::vector<unsigned short> indices;
  426. indices.reserve(k_firecamp_index_reserve);
  427. auto append_plane = [&](float planeIndex) {
  428. auto const base = static_cast<unsigned short>(vertices.size());
  429. vertices.push_back(
  430. {QVector3D(-1.0F, 0.0F, planeIndex), QVector2D(0.0F, 0.0F)});
  431. vertices.push_back(
  432. {QVector3D(1.0F, 0.0F, planeIndex), QVector2D(1.0F, 0.0F)});
  433. vertices.push_back(
  434. {QVector3D(1.0F, 2.0F, planeIndex), QVector2D(1.0F, 1.0F)});
  435. vertices.push_back(
  436. {QVector3D(-1.0F, 2.0F, planeIndex), QVector2D(0.0F, 1.0F)});
  437. indices.push_back(base + 0);
  438. indices.push_back(base + 1);
  439. indices.push_back(base + 2);
  440. indices.push_back(base + 0);
  441. indices.push_back(base + 2);
  442. indices.push_back(base + 3);
  443. };
  444. append_plane(0.0F);
  445. append_plane(1.0F);
  446. append_plane(2.0F);
  447. glGenVertexArrays(1, &m_firecampVao);
  448. glBindVertexArray(m_firecampVao);
  449. glGenBuffers(1, &m_firecampVertexBuffer);
  450. glBindBuffer(GL_ARRAY_BUFFER, m_firecampVertexBuffer);
  451. glBufferData(
  452. GL_ARRAY_BUFFER,
  453. static_cast<GLsizeiptr>(vertices.size() * sizeof(FireCampVertex)),
  454. vertices.data(), GL_STATIC_DRAW);
  455. m_firecampVertexCount = static_cast<GLsizei>(vertices.size());
  456. glEnableVertexAttribArray(Position);
  457. glVertexAttribPointer(Position, Vec3, GL_FLOAT, GL_FALSE,
  458. sizeof(FireCampVertex), reinterpret_cast<void *>(0));
  459. glEnableVertexAttribArray(Normal);
  460. glVertexAttribPointer(
  461. Normal, Vec2, GL_FLOAT, GL_FALSE, sizeof(FireCampVertex),
  462. reinterpret_cast<void *>(offsetof(FireCampVertex, tex_coord)));
  463. glGenBuffers(1, &m_firecampIndexBuffer);
  464. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_firecampIndexBuffer);
  465. glBufferData(GL_ELEMENT_ARRAY_BUFFER,
  466. static_cast<GLsizeiptr>(indices.size() * sizeof(unsigned short)),
  467. indices.data(), GL_STATIC_DRAW);
  468. m_firecampIndexCount = static_cast<GLsizei>(indices.size());
  469. glEnableVertexAttribArray(InstancePosition);
  470. glVertexAttribDivisor(InstancePosition, 1);
  471. glEnableVertexAttribArray(InstanceScale);
  472. glVertexAttribDivisor(InstanceScale, 1);
  473. glBindVertexArray(0);
  474. glBindBuffer(GL_ARRAY_BUFFER, 0);
  475. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  476. }
  477. void VegetationPipeline::shutdownFireCampPipeline() {
  478. if (QOpenGLContext::currentContext() == nullptr) {
  479. m_firecampVao = 0;
  480. m_firecampVertexBuffer = 0;
  481. m_firecampIndexBuffer = 0;
  482. m_firecampVertexCount = 0;
  483. m_firecampIndexCount = 0;
  484. return;
  485. }
  486. initializeOpenGLFunctions();
  487. if (m_firecampIndexBuffer != 0U) {
  488. glDeleteBuffers(1, &m_firecampIndexBuffer);
  489. m_firecampIndexBuffer = 0;
  490. }
  491. if (m_firecampVertexBuffer != 0U) {
  492. glDeleteBuffers(1, &m_firecampVertexBuffer);
  493. m_firecampVertexBuffer = 0;
  494. }
  495. if (m_firecampVao != 0U) {
  496. glDeleteVertexArrays(1, &m_firecampVao);
  497. m_firecampVao = 0;
  498. }
  499. m_firecampVertexCount = 0;
  500. m_firecampIndexCount = 0;
  501. }
  502. } // namespace Render::GL::BackendPipelines