summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-04-20 20:27:43 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2023-04-20 20:27:43 +0100
commitac05fbbc71282b059164b51efd68ee6e372870cb (patch)
treea505ed3e38a8e2419c1d706f45dd39873b627962
parentExpose bufferName and count from InstanceVertices (diff)
downloadilt-ac05fbbc71282b059164b51efd68ee6e372870cb.tar.bz2
ilt-ac05fbbc71282b059164b51efd68ee6e372870cb.tar.xz
ilt-ac05fbbc71282b059164b51efd68ee6e372870cb.zip
Switch to render trees in bulk through foliage asset rendering
-rw-r--r--game/scenary/foliage.cpp22
-rw-r--r--game/scenary/foliage.h10
-rw-r--r--game/scenary/plant.cpp12
-rw-r--r--game/scenary/plant.h12
-rw-r--r--test/test-assetFactory.cpp2
-rw-r--r--ui/gameMainWindow.cpp10
6 files changed, 40 insertions, 28 deletions
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<glm::mat4>(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<GLsizei>(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<Foliage> {
+class Foliage : public Asset, public Renderable, public StdTypeDefs<Foliage> {
Mesh::Ptr bodyMesh;
std::shared_ptr<Texture> texture;
+ glVertexArray instanceVAO;
public:
- void render(const SceneShader &, const Location &) const;
- void shadows(const ShadowMapper &, const Location &) const;
+ mutable InstanceVertices<glm::mat4> instances;
+ void render(const SceneShader &) const override;
+ void shadows(const ShadowMapper &) const override;
protected:
friend Persistence::SelectionPtrBase<std::shared_ptr<Foliage>>;
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<const Foliage> 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 <glm/gtx/transform.hpp>
-class Plant : public Renderable, public WorldObject {
+class Plant : public WorldObject {
std::shared_ptr<const Foliage> type;
- Location position;
-
- void render(const SceneShader & shader) const override;
- void shadows(const ShadowMapper & shadowMapper) const override;
+ InstanceVertices<glm::mat4>::InstanceProxy location;
void
tick(TickDuration) override
@@ -18,5 +16,5 @@ class Plant : public Renderable, public WorldObject {
}
public:
- Plant(std::shared_ptr<const Foliage> type, Location position) : type(std::move(type)), position(position) { }
+ Plant(std::shared_ptr<const Foliage> type, Location position);
};
diff --git a/test/test-assetFactory.cpp b/test/test-assetFactory.cpp
index 82543f3..817654b 100644
--- a/test/test-assetFactory.cpp
+++ b/test/test-assetFactory.cpp
@@ -108,7 +108,7 @@ BOOST_AUTO_TEST_CASE(foliage, *boost::unit_test::timeout(5))
BOOST_REQUIRE(tree_01_1_f);
auto plant = std::make_shared<Plant>(tree_01_1_f, Location {{-2, 2, 0}, {}});
- objects.objects.push_back(plant);
+ objects.objects.push_back(tree_01_1_f);
render(5);
}
diff --git a/ui/gameMainWindow.cpp b/ui/gameMainWindow.cpp
index 0b30cad..3f85a4f 100644
--- a/ui/gameMainWindow.cpp
+++ b/ui/gameMainWindow.cpp
@@ -51,6 +51,11 @@ GameMainWindow::render() const
void
GameMainWindow::content(const SceneShader & shader) const
{
+ for (const auto & [id, asset] : gameState->assets) {
+ if (const auto r = std::dynamic_pointer_cast<const Renderable>(asset)) {
+ r->render(shader);
+ }
+ }
gameState->world.apply<Renderable>(&Renderable::render, shader);
uiComponents.apply<WorldOverlay>(&WorldOverlay::render, shader);
}
@@ -68,5 +73,10 @@ GameMainWindow::lights(const SceneShader & shader) const
void
GameMainWindow::shadows(const ShadowMapper & shadowMapper) const
{
+ for (const auto & [id, asset] : gameState->assets) {
+ if (const auto r = std::dynamic_pointer_cast<const Renderable>(asset)) {
+ r->shadows(shadowMapper);
+ }
+ }
gameState->world.apply<Renderable>(&Renderable::shadows, shadowMapper);
}