From 737ce277a6e49d88a04be1e384eef09d486f513b Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 6 Apr 2024 00:47:37 +0100 Subject: Split water from terrain Shares the geo data instance, and still has the same implementation at this stage. --- application/main.cpp | 2 ++ game/terrain.cpp | 11 ++-------- game/terrain.h | 3 +-- game/water.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ game/water.h | 29 ++++++++++++++++++++++++++ test/test-render.cpp | 15 ++++++++----- 6 files changed, 103 insertions(+), 16 deletions(-) create mode 100644 game/water.cpp create mode 100644 game/water.h 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 #include #include +#include #include #include #include // IWYU pragma: keep @@ -43,6 +44,7 @@ public: windows.create(DISPLAY_WIDTH, DISPLAY_HEIGHT); world.create(geoData); + world.create(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 #include -Terrain::Terrain(std::shared_ptr tm) : - geoData {std::move(tm)}, grass {std::make_shared("grass.png")}, - water {std::make_shared("water.png")} +Terrain::Terrain(std::shared_ptr tm) : geoData {std::move(tm)}, grass {std::make_shared("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; Collection meshes; - std::shared_ptr grass, water; + std::shared_ptr 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +Water::Water(std::shared_ptr tm) : geoData {std::move(tm)}, water {std::make_shared("water.png")} +{ + generateMeshes(); +} + +void +Water::generateMeshes() +{ + std::vector indices; + indices.reserve(geoData->n_faces() * 3); + std::vector vertices; + vertices.reserve(geoData->n_vertices()); + std::map 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(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 +#include +#include + +class SceneShader; +class Texture; +class GeoData; + +class Water : public WorldObject, public Renderable { +public: + explicit Water(std::shared_ptr); + + void render(const SceneShader & shader) const override; + + void tick(TickDuration) override; + float waveCycle {0.F}; + +private: + void generateMeshes(); + + std::shared_ptr geoData; + Collection meshes; + std::shared_ptr 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 #include #include +#include #include #include #include @@ -26,11 +27,10 @@ class TestScene : public SceneProvider { AssetFactory::loadXML(RESDIR "/brush47.xml")->assets.at("brush-47")); std::shared_ptr train1, train2; RailLinks rail; + std::shared_ptr gd = std::make_shared(GeoData::createFlat({0, 0}, {1000000, 1000000}, 1)); - Terrain terrain {[]() { - auto gd = std::make_shared(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::loadFromAsciiGrid(FIXTURESDIR "height/SD19.asc"))}; + std::shared_ptr gd + = std::make_shared(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 -- cgit v1.2.3 From 7d1351c2a25d71d312bf341ccab6fe6ca213023f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 7 Apr 2024 16:13:42 +0100 Subject: Fix creating a flat terrain of exactly the requested size Which must be a size of multiples of GRID_SIZE, 10m --- game/geoData.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 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 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 vertices; - for (GlobalDistance row = lower.x; row < upper.x; row += GRID_SIZE) { - for (GlobalDistance col = lower.y; col < upper.y; col += GRID_SIZE) { + 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(std::ceil(float(upper.x - lower.x) / GRID_SIZE)); - const auto ncols = static_cast(std::ceil(float(upper.y - lower.y) / GRID_SIZE)); - 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)], }); } } -- cgit v1.2.3 From 8231a803cbb561715ed0df5a1e393f002874b5db Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 7 Apr 2024 18:24:32 +0100 Subject: Create water squares/polygons only where required --- game/water.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 15 deletions(-) diff --git a/game/water.cpp b/game/water.cpp index 1c0e9ca..3dc1916 100644 --- a/game/water.cpp +++ b/game/water.cpp @@ -12,34 +12,80 @@ #include #include #include +#include #include #include +namespace glm { + bool + operator<(const GlobalPosition2D a, const GlobalPosition2D b) + { + return std::tie(a.x, a.y) < std::tie(b.x, b.y); + } +} + Water::Water(std::shared_ptr tm) : geoData {std::move(tm)}, water {std::make_shared("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 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(), 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 indices; - indices.reserve(geoData->n_faces() * 3); std::vector vertices; - vertices.reserve(geoData->n_vertices()); - std::map 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]; - }); + std::map vertexIndex; + std::for_each(waterPositions.begin(), waterPositions.end(), + [&indices, &vertices, &vertexIndex, extents = geoData->getExtents(), this](const GlobalPosition2D p) { + std::array 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(RelativePosition3D {geoData->positionAt(cpos)}, + TextureRelCoord(pos / TILE_SIZE), up); + } + *out++ = static_cast(v.first->second); + } + } + for (const auto i : {0U, 3U, 1U, 0U, 2U, 3U}) { + indices.push_back(currentIndices[i]); + } }); meshes.create(vertices, indices); } -- cgit v1.2.3 From 97d4db588bfc4254e09e4305fb0525a04748f84e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 7 Apr 2024 19:38:18 +0100 Subject: Stripped down water vertex and simplified shaders --- game/water.cpp | 15 ++++++++++----- game/water.h | 7 ++++++- gfx/gl/shaders/water.fs | 5 +++-- gfx/gl/shaders/water.vs | 12 ++++++------ ui/gameMainWindow.cpp | 3 +-- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/game/water.cpp b/game/water.cpp index 3dc1916..f720e3e 100644 --- a/game/water.cpp +++ b/game/water.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include @@ -24,6 +23,13 @@ namespace glm { } } +template<> +VertexArrayObject & +VertexArrayObject::addAttribsFor(const GLuint arrayBuffer, const GLuint divisor) +{ + return addAttribs(arrayBuffer, divisor); +} + Water::Water(std::shared_ptr tm) : geoData {std::move(tm)}, water {std::make_shared("water.png")} { generateMeshes(); @@ -77,8 +83,7 @@ Water::generateMeshes() 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(RelativePosition3D {geoData->positionAt(cpos)}, - TextureRelCoord(pos / TILE_SIZE), up); + vertices.emplace_back(geoData->positionAt(cpos)); } *out++ = static_cast(v.first->second); } @@ -87,7 +92,7 @@ Water::generateMeshes() indices.push_back(currentIndices[i]); } }); - meshes.create(vertices, indices); + meshes.create>(vertices, indices); } void @@ -101,5 +106,5 @@ Water::render(const SceneShader & shader) const { shader.water.use(waveCycle); water->bind(); - meshes.apply(&Mesh::Draw); + meshes.apply(&MeshT::Draw); } diff --git a/game/water.h b/game/water.h index 20796d7..ceb7bd2 100644 --- a/game/water.h +++ b/game/water.h @@ -2,6 +2,7 @@ #include "chronology.h" #include "collection.h" +#include "config/types.h" #include "game/worldobject.h" #include #include @@ -20,10 +21,14 @@ public: void tick(TickDuration) override; float waveCycle {0.F}; + struct Vertex { + GlobalPosition3D pos; + }; + private: void generateMeshes(); std::shared_ptr geoData; - Collection meshes; + Collection, false> meshes; std::shared_ptr water; }; diff --git a/gfx/gl/shaders/water.fs b/gfx/gl/shaders/water.fs index 8891733..0918d9f 100644 --- a/gfx/gl/shaders/water.fs +++ b/gfx/gl/shaders/water.fs @@ -1,7 +1,8 @@ #version 330 core #extension GL_ARB_shading_language_420pack : enable -include(`materialInterface.glsl') +in vec3 FragPos; +in vec2 TexCoords; include(`materialOut.glsl') uniform sampler2D texture0; @@ -10,7 +11,7 @@ void main() { gPosition = ivec4(FragPos, 1); - gNormal = vec4(Normal, 1); + gNormal = vec4(0, 0, 1, 1); gAlbedoSpec = texture(texture0, TexCoords); gAlbedoSpec.a *= clamp(-FragPos.z * .0007, .1, 1.0); } diff --git a/gfx/gl/shaders/water.vs b/gfx/gl/shaders/water.vs index f6c7c8f..34c4700 100644 --- a/gfx/gl/shaders/water.vs +++ b/gfx/gl/shaders/water.vs @@ -1,7 +1,8 @@ #version 330 core -include(`meshIn.glsl') -include(`materialInterface.glsl') +layout(location = 0) in ivec3 position; +out vec3 FragPos; +out vec2 TexCoords; uniform mat4 viewProjection; uniform ivec3 viewPoint; @@ -10,12 +11,11 @@ uniform float waves; void main() { - vec3 wpos = vec3(position.x + (cos(waves) * 1000.0), position.y + (cos(waves * 0 / 2) * 1000.0), - cos(waves + (position.x / 1000000.0) + (position.y / 8000.0)) * 300.0); + vec3 wpos = vec3(position.x + (cos(waves) * 1000.0), position.y + (cos(waves * 1.4) * 1000.0), + cos(waves + (position.x / 1000000) + (position.y / 8000)) * 300.0); FragPos = vec3(wpos.xy, position.z); - TexCoords = texCoord; - Normal = normal; + TexCoords = position.xy / 8192; gl_Position = viewProjection * vec4(wpos - viewPoint, 1.0); } diff --git a/ui/gameMainWindow.cpp b/ui/gameMainWindow.cpp index d71c74a..ccbcdba 100644 --- a/ui/gameMainWindow.cpp +++ b/ui/gameMainWindow.cpp @@ -15,7 +15,6 @@ #include #include #include -#include class GameMainToolbar : Mode, public Toolbar { public: @@ -31,7 +30,7 @@ public: GameMainWindow::GameMainWindow(size_t w, size_t h) : Window {w, h, "I Like Trains", SDL_WINDOW_OPENGL}, SceneRenderer {Window::size, 0} { - uiComponents.create(glm::vec2 {315'000'000, 495'000'000}); + uiComponents.create(glm::vec2 {310'727'624, 494'018'810}); auto gms = uiComponents.create(&camera, ScreenAbsCoord {w, h}); uiComponents.create(gms.get()); } -- cgit v1.2.3 From c9af4856e1849cb7cd97e6ac85fc2aa048c8fd1d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 7 Apr 2024 23:43:50 +0100 Subject: Fix water texture wrapper in light of large position values --- gfx/gl/shaders/water.vs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gfx/gl/shaders/water.vs b/gfx/gl/shaders/water.vs index 34c4700..58bf7b6 100644 --- a/gfx/gl/shaders/water.vs +++ b/gfx/gl/shaders/water.vs @@ -15,7 +15,7 @@ main() cos(waves + (position.x / 1000000) + (position.y / 8000)) * 300.0); FragPos = vec3(wpos.xy, position.z); - TexCoords = position.xy / 8192; + TexCoords = (position.xy / 8192) - (viewPoint.xy / 8192); gl_Position = viewProjection * vec4(wpos - viewPoint, 1.0); } -- cgit v1.2.3