From ac05fbbc71282b059164b51efd68ee6e372870cb Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 20 Apr 2023 20:27:43 +0100 Subject: Switch to render trees in bulk through foliage asset rendering --- game/scenary/foliage.cpp | 22 ++++++++++++++-------- game/scenary/foliage.h | 10 +++++++--- game/scenary/plant.cpp | 12 +++--------- game/scenary/plant.h | 12 +++++------- 4 files changed, 29 insertions(+), 27 deletions(-) (limited to 'game/scenary') diff --git a/game/scenary/foliage.cpp b/game/scenary/foliage.cpp index d39d500..35be051 100644 --- a/game/scenary/foliage.cpp +++ b/game/scenary/foliage.cpp @@ -1,7 +1,9 @@ #include "foliage.h" #include "gfx/gl/sceneShader.h" #include "gfx/gl/shadowMapper.h" +#include "gfx/gl/vertexArrayObject.hpp" #include "gfx/models/texture.h" +#include "location.hpp" bool Foliage::persist(Persistence::PersistenceStore & store) @@ -13,21 +15,25 @@ void Foliage::postLoad() { texture = getTexture(); + bodyMesh->configureVAO(instanceVAO).addAttribs(instances.bufferName(), 1); } void -Foliage::render(const SceneShader & shader, const Location & loc) const +Foliage::render(const SceneShader & shader) const { - shader.basic.use(loc); - if (texture) { - texture->bind(); + if (const auto count = instances.count()) { + shader.basicInst.use(); + if (texture) { + texture->bind(); + } + glBindVertexArray(instanceVAO); + glDrawElementsInstanced( + bodyMesh->type(), bodyMesh->count(), GL_UNSIGNED_INT, nullptr, static_cast(count)); + glBindVertexArray(0); } - bodyMesh->Draw(); } void -Foliage::shadows(const ShadowMapper & mapper, const Location & loc) const +Foliage::shadows(const ShadowMapper &) const { - mapper.dynamicPoint.use(loc); - bodyMesh->Draw(); } diff --git a/game/scenary/foliage.h b/game/scenary/foliage.h index b85aab2..b72a9c2 100644 --- a/game/scenary/foliage.h +++ b/game/scenary/foliage.h @@ -1,19 +1,23 @@ #pragma once #include "assetFactory/asset.h" +#include "gfx/gl/instanceVertices.h" +#include "gfx/renderable.h" class SceneShader; class ShadowMapper; class Location; class Texture; -class Foliage : public Asset, public StdTypeDefs { +class Foliage : public Asset, public Renderable, public StdTypeDefs { Mesh::Ptr bodyMesh; std::shared_ptr texture; + glVertexArray instanceVAO; public: - void render(const SceneShader &, const Location &) const; - void shadows(const ShadowMapper &, const Location &) const; + mutable InstanceVertices instances; + void render(const SceneShader &) const override; + void shadows(const ShadowMapper &) const override; protected: friend Persistence::SelectionPtrBase>; diff --git a/game/scenary/plant.cpp b/game/scenary/plant.cpp index 2b01bee..678d4a7 100644 --- a/game/scenary/plant.cpp +++ b/game/scenary/plant.cpp @@ -1,13 +1,7 @@ #include "plant.h" -void -Plant::render(const SceneShader & shader) const +Plant::Plant(std::shared_ptr type, Location position) : + type {std::move(type)}, + location {this->type->instances.acquire(glm::translate(position.pos) * rotate_ypr(position.rot))} { - type->render(shader, position); -} - -void -Plant::shadows(const ShadowMapper & mapper) const -{ - type->shadows(mapper, position); } diff --git a/game/scenary/plant.h b/game/scenary/plant.h index 55acca1..77c5979 100644 --- a/game/scenary/plant.h +++ b/game/scenary/plant.h @@ -2,15 +2,13 @@ #include "foliage.h" #include "game/worldobject.h" -#include "gfx/renderable.h" #include "location.hpp" +#include "maths.h" +#include -class Plant : public Renderable, public WorldObject { +class Plant : public WorldObject { std::shared_ptr type; - Location position; - - void render(const SceneShader & shader) const override; - void shadows(const ShadowMapper & shadowMapper) const override; + InstanceVertices::InstanceProxy location; void tick(TickDuration) override @@ -18,5 +16,5 @@ class Plant : public Renderable, public WorldObject { } public: - Plant(std::shared_ptr type, Location position) : type(std::move(type)), position(position) { } + Plant(std::shared_ptr type, Location position); }; -- cgit v1.2.3