From 54f0ffccb2bd6032e978f94250f157b87e7f812f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 17 Feb 2021 01:22:57 +0000 Subject: Split landmass and water meshes --- game/terrain.cpp | 27 +++++++++++++++++++++------ game/terrain.h | 3 ++- 2 files changed, 23 insertions(+), 7 deletions(-) (limited to 'game') diff --git a/game/terrain.cpp b/game/terrain.cpp index 3d39a2a..9a7d243 100644 --- a/game/terrain.cpp +++ b/game/terrain.cpp @@ -12,6 +12,10 @@ #include #include +template class TerrainComp : public Mesh { + using Mesh::Mesh; +}; + Terrain::Terrain() : texture {Texture::cachedTexture.get("terrain.png")} { constexpr auto size {241}; // Vertices @@ -61,7 +65,8 @@ Terrain::Terrain() : texture {Texture::cachedTexture.get("terrain.png")} } } } - finish(size, size, resolution, vertices); + finish(size, size, vertices); + addWater(size, size, resolution); } Terrain::Terrain(const std::string & fileName) : texture {Texture::cachedTexture.get("terrain.png")} @@ -85,11 +90,12 @@ Terrain::Terrain(const std::string & fileName) : texture {Texture::cachedTexture } } - finish(map.width, map.height, resolution, vertices); + finish(map.width, map.height, vertices); + addWater(map.width, map.height, resolution); } void -Terrain::finish(unsigned int width, unsigned int height, unsigned int resolution, std::vector & vertices) +Terrain::finish(unsigned int width, unsigned int height, std::vector & vertices) { const auto tilesCount = (width - 1) * (height - 1); const auto trianglesCount = tilesCount * 2; @@ -121,7 +127,15 @@ Terrain::finish(unsigned int width, unsigned int height, unsigned int resolution v(width, x, z).normal = -glm::normalize(glm::cross(c - a, d - b)); } } - const auto verticesCount = vertices.size(); + meshes.create>(vertices, indices); +} + +void +Terrain::addWater(unsigned int width, unsigned int height, unsigned int resolution) +{ + const auto verticesCount {0U}; + std::vector vertices; + std::vector indices; // Add water const auto extentx {(int)((width - 1) * resolution / 2)}; const auto extentz {(int)((height - 1) * resolution / 2)}; @@ -135,7 +149,7 @@ Terrain::finish(unsigned int width, unsigned int height, unsigned int resolution indices.push_back(verticesCount); indices.push_back(verticesCount + 2); indices.push_back(verticesCount + 3); - meshes.create(vertices, indices); + meshes.create>(vertices, indices); } static const Transform identity {}; @@ -146,5 +160,6 @@ Terrain::render(const Shader & shader) const { shader.setModel(identityModel); texture->Bind(); - meshes.apply(&Mesh::Draw); + meshes.apply>(&Mesh::Draw); + meshes.apply>(&Mesh::Draw); } diff --git a/game/terrain.h b/game/terrain.h index 9b5e25e..ff3f92e 100644 --- a/game/terrain.h +++ b/game/terrain.h @@ -25,7 +25,8 @@ public: private: static constexpr unsigned int NUM_BUFFERS {4}; - void finish(unsigned int width, unsigned int height, unsigned int resolution, std::vector &); + void finish(unsigned int width, unsigned int height, std::vector &); + void addWater(unsigned int width, unsigned int height, unsigned int resolution); Collection meshes; std::shared_ptr texture; -- cgit v1.2.3