| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586 |
- #include "vegetation_pipeline.h"
- #include "../render_constants.h"
- #include "gl/shader_cache.h"
- #include <GL/gl.h>
- #include <QDebug>
- #include <QOpenGLContext>
- #include <cmath>
- #include <cstddef>
- #include <cstdint>
- #include <qglobal.h>
- #include <qopenglext.h>
- #include <qstringliteral.h>
- #include <qvectornd.h>
- #include <vector>
- namespace Render::GL::BackendPipelines {
- using namespace Render::GL::VertexAttrib;
- using namespace Render::GL::ComponentCount;
- using namespace Render::GL::Geometry;
- VegetationPipeline::VegetationPipeline(ShaderCache *shaderCache)
- : m_shaderCache(shaderCache) {}
- VegetationPipeline::~VegetationPipeline() { shutdown(); }
- auto VegetationPipeline::initialize() -> bool {
- initializeOpenGLFunctions();
- if (m_shaderCache == nullptr) {
- return false;
- }
- m_stoneShader = m_shaderCache->get(QStringLiteral("stone_instanced"));
- m_plantShader = m_shaderCache->get(QStringLiteral("plant_instanced"));
- m_pineShader = m_shaderCache->get(QStringLiteral("pine_instanced"));
- m_firecampShader = m_shaderCache->get(QStringLiteral("firecamp"));
- if (m_stoneShader == nullptr) {
- qWarning() << "VegetationPipeline: stone shader missing";
- }
- if (m_plantShader == nullptr) {
- qWarning() << "VegetationPipeline: plant shader missing";
- }
- if (m_pineShader == nullptr) {
- qWarning() << "VegetationPipeline: pine shader missing";
- }
- if (m_firecampShader == nullptr) {
- qWarning() << "VegetationPipeline: firecamp shader missing";
- }
- initializeStonePipeline();
- initializePlantPipeline();
- initializePinePipeline();
- initializeFireCampPipeline();
- cacheUniforms();
- m_initialized = true;
- return true;
- }
- void VegetationPipeline::shutdown() {
- shutdownStonePipeline();
- shutdownPlantPipeline();
- shutdownPinePipeline();
- shutdownFireCampPipeline();
- m_initialized = false;
- }
- void VegetationPipeline::cacheUniforms() {
- if (m_stoneShader != nullptr) {
- m_stoneUniforms.view_proj = m_stoneShader->uniformHandle("uViewProj");
- m_stoneUniforms.light_direction =
- m_stoneShader->uniformHandle("uLightDirection");
- }
- if (m_plantShader != nullptr) {
- m_plantUniforms.view_proj = m_plantShader->uniformHandle("uViewProj");
- m_plantUniforms.time = m_plantShader->uniformHandle("uTime");
- m_plantUniforms.windStrength =
- m_plantShader->uniformHandle("uWindStrength");
- m_plantUniforms.windSpeed = m_plantShader->uniformHandle("uWindSpeed");
- m_plantUniforms.light_direction =
- m_plantShader->uniformHandle("uLightDirection");
- }
- if (m_pineShader != nullptr) {
- m_pineUniforms.view_proj = m_pineShader->uniformHandle("uViewProj");
- m_pineUniforms.time = m_pineShader->uniformHandle("uTime");
- m_pineUniforms.windStrength = m_pineShader->uniformHandle("uWindStrength");
- m_pineUniforms.windSpeed = m_pineShader->uniformHandle("uWindSpeed");
- m_pineUniforms.light_direction =
- m_pineShader->uniformHandle("uLightDirection");
- }
- if (m_firecampShader != nullptr) {
- m_firecampUniforms.view_proj =
- m_firecampShader->uniformHandle("u_viewProj");
- m_firecampUniforms.time = m_firecampShader->uniformHandle("u_time");
- m_firecampUniforms.flickerSpeed =
- m_firecampShader->uniformHandle("u_flickerSpeed");
- m_firecampUniforms.flickerAmount =
- m_firecampShader->uniformHandle("u_flickerAmount");
- m_firecampUniforms.glowStrength =
- m_firecampShader->uniformHandle("u_glowStrength");
- m_firecampUniforms.fireTexture =
- m_firecampShader->uniformHandle("fireTexture");
- m_firecampUniforms.camera_right =
- m_firecampShader->uniformHandle("u_cameraRight");
- m_firecampUniforms.camera_forward =
- m_firecampShader->uniformHandle("u_cameraForward");
- }
- }
- void VegetationPipeline::initializeStonePipeline() {
- initializeOpenGLFunctions();
- shutdownStonePipeline();
- struct StoneVertex {
- QVector3D position;
- QVector3D normal;
- };
- const StoneVertex stone_vertices[] = {
- {{-0.5F, -0.5F, 0.5F}, {0.0F, 0.0F, 1.0F}},
- {{0.5F, -0.5F, 0.5F}, {0.0F, 0.0F, 1.0F}},
- {{0.5F, 0.5F, 0.5F}, {0.0F, 0.0F, 1.0F}},
- {{-0.5F, 0.5F, 0.5F}, {0.0F, 0.0F, 1.0F}},
- {{-0.5F, -0.5F, -0.5F}, {0.0F, 0.0F, -1.0F}},
- {{-0.5F, 0.5F, -0.5F}, {0.0F, 0.0F, -1.0F}},
- {{0.5F, 0.5F, -0.5F}, {0.0F, 0.0F, -1.0F}},
- {{0.5F, -0.5F, -0.5F}, {0.0F, 0.0F, -1.0F}},
- {{-0.5F, 0.5F, -0.5F}, {0.0F, 1.0F, 0.0F}},
- {{-0.5F, 0.5F, 0.5F}, {0.0F, 1.0F, 0.0F}},
- {{0.5F, 0.5F, 0.5F}, {0.0F, 1.0F, 0.0F}},
- {{0.5F, 0.5F, -0.5F}, {0.0F, 1.0F, 0.0F}},
- {{-0.5F, -0.5F, -0.5F}, {0.0F, -1.0F, 0.0F}},
- {{0.5F, -0.5F, -0.5F}, {0.0F, -1.0F, 0.0F}},
- {{0.5F, -0.5F, 0.5F}, {0.0F, -1.0F, 0.0F}},
- {{-0.5F, -0.5F, 0.5F}, {0.0F, -1.0F, 0.0F}},
- {{0.5F, -0.5F, -0.5F}, {1.0F, 0.0F, 0.0F}},
- {{0.5F, 0.5F, -0.5F}, {1.0F, 0.0F, 0.0F}},
- {{0.5F, 0.5F, 0.5F}, {1.0F, 0.0F, 0.0F}},
- {{0.5F, -0.5F, 0.5F}, {1.0F, 0.0F, 0.0F}},
- {{-0.5F, -0.5F, -0.5F}, {-1.0F, 0.0F, 0.0F}},
- {{-0.5F, -0.5F, 0.5F}, {-1.0F, 0.0F, 0.0F}},
- {{-0.5F, 0.5F, 0.5F}, {-1.0F, 0.0F, 0.0F}},
- {{-0.5F, 0.5F, -0.5F}, {-1.0F, 0.0F, 0.0F}},
- };
- const uint16_t stone_indices[] = {
- 0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4, 8, 9, 10, 10, 11, 8,
- 12, 13, 14, 14, 15, 12, 16, 17, 18, 18, 19, 16, 20, 21, 22, 22, 23, 20};
- glGenVertexArrays(1, &m_stoneVao);
- glBindVertexArray(m_stoneVao);
- glGenBuffers(1, &m_stoneVertexBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, m_stoneVertexBuffer);
- glBufferData(GL_ARRAY_BUFFER, sizeof(stone_vertices), stone_vertices,
- GL_STATIC_DRAW);
- m_stoneVertexCount = CubeVertexCount;
- glGenBuffers(1, &m_stoneIndexBuffer);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_stoneIndexBuffer);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(stone_indices), stone_indices,
- GL_STATIC_DRAW);
- constexpr int k_stone_index_count = 36;
- m_stoneIndexCount = k_stone_index_count;
- glEnableVertexAttribArray(Position);
- glVertexAttribPointer(
- Position, Vec3, GL_FLOAT, GL_FALSE, sizeof(StoneVertex),
- reinterpret_cast<void *>(offsetof(StoneVertex, position)));
- glEnableVertexAttribArray(Normal);
- glVertexAttribPointer(
- Normal, Vec3, GL_FLOAT, GL_FALSE, sizeof(StoneVertex),
- reinterpret_cast<void *>(offsetof(StoneVertex, normal)));
- glEnableVertexAttribArray(TexCoord);
- glVertexAttribDivisor(TexCoord, 1);
- glEnableVertexAttribArray(InstancePosition);
- glVertexAttribDivisor(InstancePosition, 1);
- glBindVertexArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- }
- void VegetationPipeline::shutdownStonePipeline() {
- if (QOpenGLContext::currentContext() == nullptr) {
- m_stoneVao = 0;
- m_stoneVertexBuffer = 0;
- m_stoneIndexBuffer = 0;
- m_stoneVertexCount = 0;
- m_stoneIndexCount = 0;
- return;
- }
- initializeOpenGLFunctions();
- if (m_stoneIndexBuffer != 0U) {
- glDeleteBuffers(1, &m_stoneIndexBuffer);
- m_stoneIndexBuffer = 0;
- }
- if (m_stoneVertexBuffer != 0U) {
- glDeleteBuffers(1, &m_stoneVertexBuffer);
- m_stoneVertexBuffer = 0;
- }
- if (m_stoneVao != 0U) {
- glDeleteVertexArrays(1, &m_stoneVao);
- m_stoneVao = 0;
- }
- m_stoneVertexCount = 0;
- m_stoneIndexCount = 0;
- }
- void VegetationPipeline::initializePlantPipeline() {
- initializeOpenGLFunctions();
- shutdownPlantPipeline();
- struct PlantVertex {
- QVector3D position;
- QVector2D tex_coord;
- QVector3D normal;
- };
- const PlantVertex plant_vertices[] = {
- {{-0.5F, 0.0F, 0.0F}, {0.0F, 0.0F}, {0.0F, 0.0F, 1.0F}},
- {{0.5F, 0.0F, 0.0F}, {1.0F, 0.0F}, {0.0F, 0.0F, 1.0F}},
- {{0.5F, 1.0F, 0.0F}, {1.0F, 1.0F}, {0.0F, 0.0F, 1.0F}},
- {{-0.5F, 1.0F, 0.0F}, {0.0F, 1.0F}, {0.0F, 0.0F, 1.0F}},
- {{0.5F, 0.0F, 0.0F}, {0.0F, 0.0F}, {0.0F, 0.0F, -1.0F}},
- {{-0.5F, 0.0F, 0.0F}, {1.0F, 0.0F}, {0.0F, 0.0F, -1.0F}},
- {{-0.5F, 1.0F, 0.0F}, {1.0F, 1.0F}, {0.0F, 0.0F, -1.0F}},
- {{0.5F, 1.0F, 0.0F}, {0.0F, 1.0F}, {0.0F, 0.0F, -1.0F}},
- {{0.0F, 0.0F, -0.5F}, {0.0F, 0.0F}, {1.0F, 0.0F, 0.0F}},
- {{0.0F, 0.0F, 0.5F}, {1.0F, 0.0F}, {1.0F, 0.0F, 0.0F}},
- {{0.0F, 1.0F, 0.5F}, {1.0F, 1.0F}, {1.0F, 0.0F, 0.0F}},
- {{0.0F, 1.0F, -0.5F}, {0.0F, 1.0F}, {1.0F, 0.0F, 0.0F}},
- {{0.0F, 0.0F, 0.5F}, {0.0F, 0.0F}, {-1.0F, 0.0F, 0.0F}},
- {{0.0F, 0.0F, -0.5F}, {1.0F, 0.0F}, {-1.0F, 0.0F, 0.0F}},
- {{0.0F, 1.0F, -0.5F}, {1.0F, 1.0F}, {-1.0F, 0.0F, 0.0F}},
- {{0.0F, 1.0F, 0.5F}, {0.0F, 1.0F}, {-1.0F, 0.0F, 0.0F}},
- };
- const unsigned short plant_indices[] = {
- 0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7,
- 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15,
- };
- glGenVertexArrays(1, &m_plantVao);
- glBindVertexArray(m_plantVao);
- glGenBuffers(1, &m_plantVertexBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, m_plantVertexBuffer);
- glBufferData(GL_ARRAY_BUFFER, sizeof(plant_vertices), plant_vertices,
- GL_STATIC_DRAW);
- m_plantVertexCount = PlantCrossQuadVertexCount;
- glEnableVertexAttribArray(Position);
- glVertexAttribPointer(
- Position, Vec3, GL_FLOAT, GL_FALSE, sizeof(PlantVertex),
- reinterpret_cast<void *>(offsetof(PlantVertex, position)));
- glEnableVertexAttribArray(Normal);
- glVertexAttribPointer(
- Normal, Vec2, GL_FLOAT, GL_FALSE, sizeof(PlantVertex),
- reinterpret_cast<void *>(offsetof(PlantVertex, tex_coord)));
- glEnableVertexAttribArray(TexCoord);
- glVertexAttribPointer(
- TexCoord, Vec3, GL_FLOAT, GL_FALSE, sizeof(PlantVertex),
- reinterpret_cast<void *>(offsetof(PlantVertex, normal)));
- glGenBuffers(1, &m_plantIndexBuffer);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_plantIndexBuffer);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(plant_indices), plant_indices,
- GL_STATIC_DRAW);
- m_plantIndexCount = PlantCrossQuadIndexCount;
- glEnableVertexAttribArray(InstancePosition);
- glVertexAttribDivisor(InstancePosition, 1);
- glEnableVertexAttribArray(InstanceScale);
- glVertexAttribDivisor(InstanceScale, 1);
- glEnableVertexAttribArray(InstanceColor);
- glVertexAttribDivisor(InstanceColor, 1);
- glBindVertexArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- }
- void VegetationPipeline::shutdownPlantPipeline() {
- if (QOpenGLContext::currentContext() == nullptr) {
- m_plantVao = 0;
- m_plantVertexBuffer = 0;
- m_plantIndexBuffer = 0;
- m_plantVertexCount = 0;
- m_plantIndexCount = 0;
- return;
- }
- initializeOpenGLFunctions();
- if (m_plantIndexBuffer != 0U) {
- glDeleteBuffers(1, &m_plantIndexBuffer);
- m_plantIndexBuffer = 0;
- }
- if (m_plantVertexBuffer != 0U) {
- glDeleteBuffers(1, &m_plantVertexBuffer);
- m_plantVertexBuffer = 0;
- }
- if (m_plantVao != 0U) {
- glDeleteVertexArrays(1, &m_plantVao);
- m_plantVao = 0;
- }
- m_plantVertexCount = 0;
- m_plantIndexCount = 0;
- }
- void VegetationPipeline::initializePinePipeline() {
- initializeOpenGLFunctions();
- shutdownPinePipeline();
- struct PineVertex {
- QVector3D position;
- QVector2D tex_coord;
- QVector3D normal;
- };
- constexpr int k_segments = PineTreeSegments;
- constexpr float k_two_pi = 6.28318530718F;
- std::vector<PineVertex> vertices;
- vertices.reserve(k_segments * 5 + 1);
- std::vector<unsigned short> indices;
- indices.reserve(k_segments * 6 * 4 + k_segments * 3);
- auto add_ring = [&](float radius, float y, float normalUp,
- float v_coord) -> int {
- const int start = static_cast<int>(vertices.size());
- for (int i = 0; i < k_segments; ++i) {
- const float t = static_cast<float>(i) / static_cast<float>(k_segments);
- const float angle = t * k_two_pi;
- const float nx = std::cos(angle);
- const float nz = std::sin(angle);
- QVector3D normal(nx, normalUp, nz);
- normal.normalize();
- QVector3D const position(radius * nx, y, radius * nz);
- QVector2D const tex_coord(t, v_coord);
- vertices.push_back({position, tex_coord, normal});
- }
- return start;
- };
- auto connect_rings = [&](int lowerStart, int upperStart) {
- for (int i = 0; i < k_segments; ++i) {
- const int next = (i + 1) % k_segments;
- const auto lower0 = static_cast<unsigned short>(lowerStart + i);
- const auto lower1 = static_cast<unsigned short>(lowerStart + next);
- const auto upper0 = static_cast<unsigned short>(upperStart + i);
- const auto upper1 = static_cast<unsigned short>(upperStart + next);
- indices.push_back(lower0);
- indices.push_back(lower1);
- indices.push_back(upper1);
- indices.push_back(lower0);
- indices.push_back(upper1);
- indices.push_back(upper0);
- }
- };
- const int trunk_bottom = add_ring(0.12F, 0.0F, 0.0F, 0.0F);
- const int trunk_mid = add_ring(0.11F, 0.35F, 0.0F, 0.12F);
- const int trunk_top = add_ring(0.10F, 0.58F, 0.05F, 0.30F);
- const int branch_base = add_ring(0.60F, 0.64F, 0.35F, 0.46F);
- const int branch_mid = add_ring(0.42F, 0.82F, 0.6F, 0.68F);
- const int branch_upper = add_ring(0.24F, 1.00F, 0.7F, 0.88F);
- const int branch_tip = add_ring(0.12F, 1.10F, 0.85F, 0.96F);
- connect_rings(trunk_bottom, trunk_mid);
- connect_rings(trunk_mid, trunk_top);
- connect_rings(trunk_top, branch_base);
- connect_rings(branch_base, branch_mid);
- connect_rings(branch_mid, branch_upper);
- connect_rings(branch_upper, branch_tip);
- const auto trunk_cap_index = static_cast<unsigned short>(vertices.size());
- vertices.push_back({QVector3D(0.0F, 0.0F, 0.0F), QVector2D(0.5F, 0.0F),
- QVector3D(0.0F, -1.0F, 0.0F)});
- for (int i = 0; i < k_segments; ++i) {
- const int next = (i + 1) % k_segments;
- indices.push_back(static_cast<unsigned short>(trunk_bottom + next));
- indices.push_back(static_cast<unsigned short>(trunk_bottom + i));
- indices.push_back(trunk_cap_index);
- }
- const auto apex_index = static_cast<unsigned short>(vertices.size());
- vertices.push_back({QVector3D(0.0F, 1.18F, 0.0F), QVector2D(0.5F, 1.0F),
- QVector3D(0.0F, 1.0F, 0.0F)});
- for (int i = 0; i < k_segments; ++i) {
- const int next = (i + 1) % k_segments;
- indices.push_back(static_cast<unsigned short>(branch_tip + i));
- indices.push_back(static_cast<unsigned short>(branch_tip + next));
- indices.push_back(apex_index);
- }
- glGenVertexArrays(1, &m_pineVao);
- glBindVertexArray(m_pineVao);
- glGenBuffers(1, &m_pineVertexBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, m_pineVertexBuffer);
- glBufferData(GL_ARRAY_BUFFER,
- static_cast<GLsizeiptr>(vertices.size() * sizeof(PineVertex)),
- vertices.data(), GL_STATIC_DRAW);
- m_pineVertexCount = static_cast<GLsizei>(vertices.size());
- glEnableVertexAttribArray(Position);
- glVertexAttribPointer(
- Position, Vec3, GL_FLOAT, GL_FALSE, sizeof(PineVertex),
- reinterpret_cast<void *>(offsetof(PineVertex, position)));
- glEnableVertexAttribArray(Normal);
- glVertexAttribPointer(
- Normal, Vec2, GL_FLOAT, GL_FALSE, sizeof(PineVertex),
- reinterpret_cast<void *>(offsetof(PineVertex, tex_coord)));
- glEnableVertexAttribArray(TexCoord);
- glVertexAttribPointer(TexCoord, Vec3, GL_FLOAT, GL_FALSE, sizeof(PineVertex),
- reinterpret_cast<void *>(offsetof(PineVertex, normal)));
- glGenBuffers(1, &m_pineIndexBuffer);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_pineIndexBuffer);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER,
- static_cast<GLsizeiptr>(indices.size() * sizeof(unsigned short)),
- indices.data(), GL_STATIC_DRAW);
- m_pineIndexCount = static_cast<GLsizei>(indices.size());
- glEnableVertexAttribArray(InstancePosition);
- glVertexAttribDivisor(InstancePosition, 1);
- glEnableVertexAttribArray(InstanceScale);
- glVertexAttribDivisor(InstanceScale, 1);
- glEnableVertexAttribArray(InstanceColor);
- glVertexAttribDivisor(InstanceColor, 1);
- glBindVertexArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- }
- void VegetationPipeline::shutdownPinePipeline() {
- if (QOpenGLContext::currentContext() == nullptr) {
- m_pineVao = 0;
- m_pineVertexBuffer = 0;
- m_pineIndexBuffer = 0;
- m_pineVertexCount = 0;
- m_pineIndexCount = 0;
- return;
- }
- initializeOpenGLFunctions();
- if (m_pineIndexBuffer != 0U) {
- glDeleteBuffers(1, &m_pineIndexBuffer);
- m_pineIndexBuffer = 0;
- }
- if (m_pineVertexBuffer != 0U) {
- glDeleteBuffers(1, &m_pineVertexBuffer);
- m_pineVertexBuffer = 0;
- }
- if (m_pineVao != 0U) {
- glDeleteVertexArrays(1, &m_pineVao);
- m_pineVao = 0;
- }
- m_pineVertexCount = 0;
- m_pineIndexCount = 0;
- }
- void VegetationPipeline::initializeFireCampPipeline() {
- initializeOpenGLFunctions();
- shutdownFireCampPipeline();
- struct FireCampVertex {
- QVector3D position;
- QVector2D tex_coord;
- };
- constexpr std::size_t k_firecamp_vertex_reserve = 12;
- constexpr std::size_t k_firecamp_index_reserve = 18;
- std::vector<FireCampVertex> vertices;
- vertices.reserve(k_firecamp_vertex_reserve);
- std::vector<unsigned short> indices;
- indices.reserve(k_firecamp_index_reserve);
- auto append_plane = [&](float planeIndex) {
- auto const base = static_cast<unsigned short>(vertices.size());
- vertices.push_back(
- {QVector3D(-1.0F, 0.0F, planeIndex), QVector2D(0.0F, 0.0F)});
- vertices.push_back(
- {QVector3D(1.0F, 0.0F, planeIndex), QVector2D(1.0F, 0.0F)});
- vertices.push_back(
- {QVector3D(1.0F, 2.0F, planeIndex), QVector2D(1.0F, 1.0F)});
- vertices.push_back(
- {QVector3D(-1.0F, 2.0F, planeIndex), QVector2D(0.0F, 1.0F)});
- indices.push_back(base + 0);
- indices.push_back(base + 1);
- indices.push_back(base + 2);
- indices.push_back(base + 0);
- indices.push_back(base + 2);
- indices.push_back(base + 3);
- };
- append_plane(0.0F);
- append_plane(1.0F);
- append_plane(2.0F);
- glGenVertexArrays(1, &m_firecampVao);
- glBindVertexArray(m_firecampVao);
- glGenBuffers(1, &m_firecampVertexBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, m_firecampVertexBuffer);
- glBufferData(
- GL_ARRAY_BUFFER,
- static_cast<GLsizeiptr>(vertices.size() * sizeof(FireCampVertex)),
- vertices.data(), GL_STATIC_DRAW);
- m_firecampVertexCount = static_cast<GLsizei>(vertices.size());
- glEnableVertexAttribArray(Position);
- glVertexAttribPointer(Position, Vec3, GL_FLOAT, GL_FALSE,
- sizeof(FireCampVertex), reinterpret_cast<void *>(0));
- glEnableVertexAttribArray(Normal);
- glVertexAttribPointer(
- Normal, Vec2, GL_FLOAT, GL_FALSE, sizeof(FireCampVertex),
- reinterpret_cast<void *>(offsetof(FireCampVertex, tex_coord)));
- glGenBuffers(1, &m_firecampIndexBuffer);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_firecampIndexBuffer);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER,
- static_cast<GLsizeiptr>(indices.size() * sizeof(unsigned short)),
- indices.data(), GL_STATIC_DRAW);
- m_firecampIndexCount = static_cast<GLsizei>(indices.size());
- glEnableVertexAttribArray(InstancePosition);
- glVertexAttribDivisor(InstancePosition, 1);
- glEnableVertexAttribArray(InstanceScale);
- glVertexAttribDivisor(InstanceScale, 1);
- glBindVertexArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- }
- void VegetationPipeline::shutdownFireCampPipeline() {
- if (QOpenGLContext::currentContext() == nullptr) {
- m_firecampVao = 0;
- m_firecampVertexBuffer = 0;
- m_firecampIndexBuffer = 0;
- m_firecampVertexCount = 0;
- m_firecampIndexCount = 0;
- return;
- }
- initializeOpenGLFunctions();
- if (m_firecampIndexBuffer != 0U) {
- glDeleteBuffers(1, &m_firecampIndexBuffer);
- m_firecampIndexBuffer = 0;
- }
- if (m_firecampVertexBuffer != 0U) {
- glDeleteBuffers(1, &m_firecampVertexBuffer);
- m_firecampVertexBuffer = 0;
- }
- if (m_firecampVao != 0U) {
- glDeleteVertexArrays(1, &m_firecampVao);
- m_firecampVao = 0;
- }
- m_firecampVertexCount = 0;
- m_firecampIndexCount = 0;
- }
- } // namespace Render::GL::BackendPipelines
|