diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-02-17 01:22:57 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-02-17 01:22:57 +0000 |
commit | 54f0ffccb2bd6032e978f94250f157b87e7f812f (patch) | |
tree | 71d9861513ffd96eed7b381fd796be421654a7d8 /game/terrain.cpp | |
parent | Don't keep the terrain vertex and index vectors (diff) | |
download | ilt-54f0ffccb2bd6032e978f94250f157b87e7f812f.tar.bz2 ilt-54f0ffccb2bd6032e978f94250f157b87e7f812f.tar.xz ilt-54f0ffccb2bd6032e978f94250f157b87e7f812f.zip |
Split landmass and water meshes
Diffstat (limited to 'game/terrain.cpp')
-rw-r--r-- | game/terrain.cpp | 27 |
1 files changed, 21 insertions, 6 deletions
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 <random> #include <stb_image.h> +template<unsigned int Comp> 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<Vertex> & vertices) +Terrain::finish(unsigned int width, unsigned int height, std::vector<Vertex> & 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<TerrainComp<0>>(vertices, indices); +} + +void +Terrain::addWater(unsigned int width, unsigned int height, unsigned int resolution) +{ + const auto verticesCount {0U}; + std::vector<Vertex> vertices; + std::vector<unsigned int> 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<Mesh>(vertices, indices); + meshes.create<TerrainComp<1>>(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<TerrainComp<0>>(&Mesh::Draw); + meshes.apply<TerrainComp<1>>(&Mesh::Draw); } |