summaryrefslogtreecommitdiff
path: root/game
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2024-04-08 00:59:52 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2024-04-08 00:59:52 +0100
commit34ea78ea50b015d660fbd9169f61506363959214 (patch)
tree1cb1de6865c0c35e2c35fcd6e2b5610b0eedd8ab /game
parentMerge remote-tracking branch 'origin/deform-terrain' (diff)
parentFix water texture wrapper in light of large position values (diff)
downloadilt-34ea78ea50b015d660fbd9169f61506363959214.tar.bz2
ilt-34ea78ea50b015d660fbd9169f61506363959214.tar.xz
ilt-34ea78ea50b015d660fbd9169f61506363959214.zip
Merge branch 'separate-water'
Diffstat (limited to 'game')
-rw-r--r--game/geoData.cpp26
-rw-r--r--game/terrain.cpp11
-rw-r--r--game/terrain.h3
-rw-r--r--game/water.cpp110
-rw-r--r--game/water.h34
5 files changed, 160 insertions, 24 deletions
diff --git a/game/geoData.cpp b/game/geoData.cpp
index ed4303b..2313342 100644
--- a/game/geoData.cpp
+++ b/game/geoData.cpp
@@ -65,36 +65,36 @@ GeoData::loadFromAsciiGrid(const std::filesystem::path & input)
return mesh;
};
-template<typename T> constexpr static T GRID_SIZE = 10'000;
+constexpr static GlobalDistance GRID_SIZE = 10'000;
GeoData
GeoData::createFlat(GlobalPosition2D lower, GlobalPosition2D upper, GlobalDistance h)
{
+ assert((upper - lower) % GRID_SIZE == GlobalPosition2D {});
GeoData mesh;
mesh.lowerExtent = {lower, h};
mesh.upperExtent = {upper, h};
std::vector<VertexHandle> vertices;
- for (GlobalDistance row = lower.x; row < upper.x; row += GRID_SIZE<GlobalDistance>) {
- for (GlobalDistance col = lower.y; col < upper.y; col += GRID_SIZE<GlobalDistance>) {
+ for (GlobalDistance row = lower.x; row <= upper.x; row += GRID_SIZE) {
+ for (GlobalDistance col = lower.y; col <= upper.y; col += GRID_SIZE) {
vertices.push_back(mesh.add_vertex({col, row, h}));
}
}
- const auto nrows = static_cast<size_t>(std::ceil(float(upper.x - lower.x) / GRID_SIZE<RelativeDistance>));
- const auto ncols = static_cast<size_t>(std::ceil(float(upper.y - lower.y) / GRID_SIZE<RelativeDistance>));
- for (size_t row = 1; row < nrows; ++row) {
- for (size_t col = 1; col < ncols; ++col) {
+ const auto n = glm::vec<2, size_t> {((upper - lower) / GRID_SIZE) + 1};
+ for (auto row = 1U; row < n.x; ++row) {
+ for (auto col = 1U; col < n.y; ++col) {
mesh.add_face({
- vertices[ncols * (row - 1) + (col - 1)],
- vertices[ncols * (row - 0) + (col - 0)],
- vertices[ncols * (row - 0) + (col - 1)],
+ vertices[n.y * (row - 1) + (col - 1)],
+ vertices[n.y * (row - 0) + (col - 0)],
+ vertices[n.y * (row - 0) + (col - 1)],
});
mesh.add_face({
- vertices[ncols * (row - 1) + (col - 1)],
- vertices[ncols * (row - 1) + (col - 0)],
- vertices[ncols * (row - 0) + (col - 0)],
+ vertices[n.y * (row - 1) + (col - 1)],
+ vertices[n.y * (row - 1) + (col - 0)],
+ vertices[n.y * (row - 0) + (col - 0)],
});
}
}
diff --git a/game/terrain.cpp b/game/terrain.cpp
index 91a228f..201c732 100644
--- a/game/terrain.cpp
+++ b/game/terrain.cpp
@@ -15,9 +15,7 @@
#include <utility>
#include <vector>
-Terrain::Terrain(std::shared_ptr<GeoData> tm) :
- geoData {std::move(tm)}, grass {std::make_shared<Texture>("grass.png")},
- water {std::make_shared<Texture>("water.png")}
+Terrain::Terrain(std::shared_ptr<GeoData> tm) : geoData {std::move(tm)}, grass {std::make_shared<Texture>("grass.png")}
{
generateMeshes();
}
@@ -47,9 +45,8 @@ Terrain::generateMeshes()
}
void
-Terrain::tick(TickDuration dur)
+Terrain::tick(TickDuration)
{
- waveCycle += dur.count();
}
void
@@ -58,10 +55,6 @@ Terrain::render(const SceneShader & shader) const
shader.landmass.use();
grass->bind();
meshes.apply(&Mesh::Draw);
-
- shader.water.use(waveCycle);
- water->bind();
- meshes.apply(&Mesh::Draw);
}
void
diff --git a/game/terrain.h b/game/terrain.h
index d62f138..54593fc 100644
--- a/game/terrain.h
+++ b/game/terrain.h
@@ -19,12 +19,11 @@ public:
void shadows(const ShadowMapper &) const override;
void tick(TickDuration) override;
- float waveCycle {0.F};
private:
void generateMeshes();
std::shared_ptr<GeoData> geoData;
Collection<Mesh, false> meshes;
- std::shared_ptr<Texture> grass, water;
+ std::shared_ptr<Texture> grass;
};
diff --git a/game/water.cpp b/game/water.cpp
new file mode 100644
index 0000000..f720e3e
--- /dev/null
+++ b/game/water.cpp
@@ -0,0 +1,110 @@
+#include "water.h"
+#include "game/geoData.h"
+#include "gfx/models/texture.h"
+#include <algorithm>
+#include <cstddef>
+#include <gfx/gl/sceneShader.h>
+#include <gfx/gl/shadowMapper.h>
+#include <gfx/image.h>
+#include <gfx/models/mesh.h>
+#include <gfx/models/vertex.h>
+#include <glm/glm.hpp>
+#include <location.h>
+#include <maths.h>
+#include <set>
+#include <utility>
+#include <vector>
+
+namespace glm {
+ bool
+ operator<(const GlobalPosition2D a, const GlobalPosition2D b)
+ {
+ return std::tie(a.x, a.y) < std::tie(b.x, b.y);
+ }
+}
+
+template<>
+VertexArrayObject &
+VertexArrayObject::addAttribsFor<Water::Vertex>(const GLuint arrayBuffer, const GLuint divisor)
+{
+ return addAttribs<Water::Vertex, &Water::Vertex::pos>(arrayBuffer, divisor);
+}
+
+Water::Water(std::shared_ptr<GeoData> tm) : geoData {std::move(tm)}, water {std::make_shared<Texture>("water.png")}
+{
+ generateMeshes();
+}
+
+static constexpr GlobalDistance MIN_HEIGHT = 1'000;
+static constexpr GlobalDistance TILE_SIZE = 8'192;
+static constexpr GlobalDistance BORDER = TILE_SIZE / 2;
+
+void
+Water::generateMeshes()
+{
+ // Map out where a water square needs to exist to cover all terrain faces with a low vertex
+ std::set<GlobalPosition2D> waterPositions;
+ std::for_each(geoData->vertices_sbegin(), geoData->vertices_end(), [this, &waterPositions](const auto vh) {
+ if (geoData->point(vh).z < MIN_HEIGHT) {
+ std::for_each(geoData->vf_begin(vh), geoData->vf_end(vh),
+ [done = std::set<OpenMesh::FaceHandle>(), this, &waterPositions](const auto fh) mutable {
+ if (done.insert(fh).second) {
+ const auto getrange = [this, fh](glm::length_t axis) {
+ const auto mme = std::minmax_element(geoData->fv_begin(fh), geoData->fv_end(fh),
+ [this, axis](const auto vh1, const auto vh2) {
+ return geoData->point(vh1)[axis] < geoData->point(vh2)[axis];
+ });
+
+ return std::make_pair((geoData->point(*mme.first)[axis] - BORDER) / TILE_SIZE,
+ (geoData->point(*mme.second)[axis] + BORDER) / TILE_SIZE);
+ };
+ const auto xrange = getrange(0);
+ const auto yrange = getrange(1);
+ for (auto x = xrange.first; x < xrange.second; x++) {
+ for (auto y = yrange.first; y < yrange.second; y++) {
+ waterPositions.emplace(x, y);
+ }
+ }
+ }
+ });
+ }
+ });
+
+ std::vector<unsigned int> indices;
+ std::vector<Vertex> vertices;
+ std::map<GlobalPosition2D, size_t> vertexIndex;
+ std::for_each(waterPositions.begin(), waterPositions.end(),
+ [&indices, &vertices, &vertexIndex, extents = geoData->getExtents(), this](const GlobalPosition2D p) {
+ std::array<unsigned int, 4> currentIndices {};
+ auto out = currentIndices.begin();
+ for (auto x : {0, TILE_SIZE}) {
+ for (auto y : {0, TILE_SIZE}) {
+ const auto pos = (p * TILE_SIZE) + GlobalPosition2D {x, y};
+ const auto v = vertexIndex.emplace(pos, vertices.size());
+ if (v.second) {
+ const auto cpos = glm::clamp(pos, std::get<0>(extents).xy(), std::get<1>(extents).xy());
+ vertices.emplace_back(geoData->positionAt(cpos));
+ }
+ *out++ = static_cast<unsigned int>(v.first->second);
+ }
+ }
+ for (const auto i : {0U, 3U, 1U, 0U, 2U, 3U}) {
+ indices.push_back(currentIndices[i]);
+ }
+ });
+ meshes.create<MeshT<Vertex>>(vertices, indices);
+}
+
+void
+Water::tick(TickDuration dur)
+{
+ waveCycle += dur.count();
+}
+
+void
+Water::render(const SceneShader & shader) const
+{
+ shader.water.use(waveCycle);
+ water->bind();
+ meshes.apply(&MeshT<GlobalPosition3D>::Draw);
+}
diff --git a/game/water.h b/game/water.h
new file mode 100644
index 0000000..ceb7bd2
--- /dev/null
+++ b/game/water.h
@@ -0,0 +1,34 @@
+#pragma once
+
+#include "chronology.h"
+#include "collection.h"
+#include "config/types.h"
+#include "game/worldobject.h"
+#include <gfx/models/mesh.h>
+#include <gfx/renderable.h>
+#include <memory>
+
+class SceneShader;
+class Texture;
+class GeoData;
+
+class Water : public WorldObject, public Renderable {
+public:
+ explicit Water(std::shared_ptr<GeoData>);
+
+ void render(const SceneShader & shader) const override;
+
+ void tick(TickDuration) override;
+ float waveCycle {0.F};
+
+ struct Vertex {
+ GlobalPosition3D pos;
+ };
+
+private:
+ void generateMeshes();
+
+ std::shared_ptr<GeoData> geoData;
+ Collection<MeshT<Vertex>, false> meshes;
+ std::shared_ptr<Texture> water;
+};