summaryrefslogtreecommitdiff
path: root/game
diff options
context:
space:
mode:
Diffstat (limited to 'game')
-rw-r--r--game/terrain.cpp27
-rw-r--r--game/terrain.h3
2 files changed, 23 insertions, 7 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);
}
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<Vertex> &);
+ void finish(unsigned int width, unsigned int height, std::vector<Vertex> &);
+ void addWater(unsigned int width, unsigned int height, unsigned int resolution);
Collection<Mesh, false> meshes;
std::shared_ptr<Texture> texture;