summaryrefslogtreecommitdiff
path: root/game
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-04-22 19:06:42 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2023-04-22 19:06:42 +0100
commitd1f79b4c438ebf0822741e103b2cb06bdee4514e (patch)
treea4d574d12cd81d5e33886ce7ca961f7dbe872fcf /game
parentRename lots of shader files (diff)
parentKeep the instance unused vector sorted and binary search it (diff)
downloadilt-d1f79b4c438ebf0822741e103b2cb06bdee4514e.tar.bz2
ilt-d1f79b4c438ebf0822741e103b2cb06bdee4514e.tar.xz
ilt-d1f79b4c438ebf0822741e103b2cb06bdee4514e.zip
Merge branch 'instancing'
Diffstat (limited to 'game')
-rw-r--r--game/gamestate.h2
-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
5 files changed, 31 insertions, 27 deletions
diff --git a/game/gamestate.h b/game/gamestate.h
index 605aac4..db223c0 100644
--- a/game/gamestate.h
+++ b/game/gamestate.h
@@ -1,5 +1,6 @@
#pragma once
+#include "assetFactory/assetFactory.h"
#include <collection.hpp>
#include <memory>
#include <special_members.hpp>
@@ -16,5 +17,6 @@ public:
Collection<WorldObject> world;
std::shared_ptr<GeoData> geoData;
+ AssetFactory::Assets assets;
};
extern GameState * gameState;
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);
};