summaryrefslogtreecommitdiff
path: root/game/water.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2024-04-06 00:47:37 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2024-04-06 00:47:37 +0100
commit737ce277a6e49d88a04be1e384eef09d486f513b (patch)
tree2eeb29d316567b1157d7a32753658df5c5f36741 /game/water.cpp
parentMerge remote-tracking branch 'origin/deform-terrain' (diff)
downloadilt-737ce277a6e49d88a04be1e384eef09d486f513b.tar.bz2
ilt-737ce277a6e49d88a04be1e384eef09d486f513b.tar.xz
ilt-737ce277a6e49d88a04be1e384eef09d486f513b.zip
Split water from terrain
Shares the geo data instance, and still has the same implementation at this stage.
Diffstat (limited to 'game/water.cpp')
-rw-r--r--game/water.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/game/water.cpp b/game/water.cpp
new file mode 100644
index 0000000..1c0e9ca
--- /dev/null
+++ b/game/water.cpp
@@ -0,0 +1,59 @@
+#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 <iterator>
+#include <location.h>
+#include <maths.h>
+#include <utility>
+#include <vector>
+
+Water::Water(std::shared_ptr<GeoData> tm) : geoData {std::move(tm)}, water {std::make_shared<Texture>("water.png")}
+{
+ generateMeshes();
+}
+
+void
+Water::generateMeshes()
+{
+ std::vector<unsigned int> indices;
+ indices.reserve(geoData->n_faces() * 3);
+ std::vector<Vertex> vertices;
+ vertices.reserve(geoData->n_vertices());
+ std::map<GeoData::VertexHandle, size_t> vertexIndex;
+ std::transform(geoData->vertices_sbegin(), geoData->vertices_end(), std::back_inserter(vertices),
+ [this, &vertexIndex](const GeoData::VertexHandle v) {
+ vertexIndex.emplace(v, vertexIndex.size());
+ const auto p = geoData->point(v);
+ return Vertex {p, RelativePosition2D(p) / 10000.F, geoData->normal(v)};
+ });
+ std::for_each(
+ geoData->faces_sbegin(), geoData->faces_end(), [this, &vertexIndex, &indices](const GeoData::FaceHandle f) {
+ std::transform(geoData->fv_begin(f), geoData->fv_end(f), std::back_inserter(indices),
+ [&vertexIndex](const GeoData::VertexHandle v) {
+ return vertexIndex[v];
+ });
+ });
+ meshes.create<Mesh>(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(&Mesh::Draw);
+}