summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--application/main.cpp2
-rw-r--r--game/terrain.cpp11
-rw-r--r--game/terrain.h3
-rw-r--r--game/water.cpp59
-rw-r--r--game/water.h29
-rw-r--r--test/test-render.cpp15
6 files changed, 103 insertions, 16 deletions
diff --git a/application/main.cpp b/application/main.cpp
index adaec9b..f0ba8bb 100644
--- a/application/main.cpp
+++ b/application/main.cpp
@@ -18,6 +18,7 @@
#include <game/vehicles/railVehicle.h>
#include <game/vehicles/railVehicleClass.h>
#include <game/vehicles/train.h>
+#include <game/water.h>
#include <game/worldobject.h>
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp> // IWYU pragma: keep
@@ -43,6 +44,7 @@ public:
windows.create<GameMainWindow>(DISPLAY_WIDTH, DISPLAY_HEIGHT);
world.create<Terrain>(geoData);
+ world.create<Water>(geoData);
assets = AssetFactory::loadAll("res");
{
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..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);
+}
diff --git a/game/water.h b/game/water.h
new file mode 100644
index 0000000..20796d7
--- /dev/null
+++ b/game/water.h
@@ -0,0 +1,29 @@
+#pragma once
+
+#include "chronology.h"
+#include "collection.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};
+
+private:
+ void generateMeshes();
+
+ std::shared_ptr<GeoData> geoData;
+ Collection<Mesh, false> meshes;
+ std::shared_ptr<Texture> water;
+};
diff --git a/test/test-render.cpp b/test/test-render.cpp
index 6c20a23..79424f5 100644
--- a/test/test-render.cpp
+++ b/test/test-render.cpp
@@ -12,6 +12,7 @@
#include <game/terrain.h>
#include <game/vehicles/railVehicle.h>
#include <game/vehicles/railVehicleClass.h>
+#include <game/water.h>
#include <gfx/gl/sceneRenderer.h>
#include <gfx/models/texture.h>
#include <lib/glArrays.h>
@@ -26,11 +27,10 @@ class TestScene : public SceneProvider {
AssetFactory::loadXML(RESDIR "/brush47.xml")->assets.at("brush-47"));
std::shared_ptr<RailVehicle> train1, train2;
RailLinks rail;
+ std::shared_ptr<GeoData> gd = std::make_shared<GeoData>(GeoData::createFlat({0, 0}, {1000000, 1000000}, 1));
- Terrain terrain {[]() {
- auto gd = std::make_shared<GeoData>(GeoData::createFlat({0, 0}, {1000000, 1000000}, 1));
- return gd;
- }()};
+ Terrain terrain {gd};
+ Water water {gd};
public:
TestScene()
@@ -51,6 +51,7 @@ public:
content(const SceneShader & shader) const override
{
terrain.render(shader);
+ water.render(shader);
brush47rvc->render(shader);
rail.render(shader);
}
@@ -132,12 +133,16 @@ BOOST_AUTO_TEST_CASE(terrain)
ss.camera.setView({310000000, 490000000, 600000}, glm::normalize(glm::vec3 {1, 1, -0.5F}));
class TestTerrain : public SceneProvider {
- Terrain terrain {std::make_shared<GeoData>(GeoData::loadFromAsciiGrid(FIXTURESDIR "height/SD19.asc"))};
+ std::shared_ptr<GeoData> gd
+ = std::make_shared<GeoData>(GeoData::loadFromAsciiGrid(FIXTURESDIR "height/SD19.asc"));
+ Terrain terrain {gd};
+ Water water {gd};
void
content(const SceneShader & shader) const override
{
terrain.render(shader);
+ water.render(shader);
}
void