diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-02-20 00:17:58 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-02-20 00:17:58 +0000 |
commit | 2bc4bcebc93e7211dfb84303888635f888ba8018 (patch) | |
tree | 3a349beadc079c544001f3a65ac0e26fe494f8e5 /game | |
parent | Create smoother terrain (diff) | |
download | ilt-2bc4bcebc93e7211dfb84303888635f888ba8018.tar.bz2 ilt-2bc4bcebc93e7211dfb84303888635f888ba8018.tar.xz ilt-2bc4bcebc93e7211dfb84303888635f888ba8018.zip |
Custom land and water shaders
Create sandy beaches, snow topped mountains and grassy hills with a single texture, coloured
according to land height by a custom shader.
Also use the land mass mesh with a new water texture and a custom shader to create rather nice
looking water effect with depth, waves and motion.
Diffstat (limited to 'game')
-rw-r--r-- | game/terrain.cpp | 44 | ||||
-rw-r--r-- | game/terrain.h | 4 |
2 files changed, 15 insertions, 33 deletions
diff --git a/game/terrain.cpp b/game/terrain.cpp index 83116fc..20c06e8 100644 --- a/game/terrain.cpp +++ b/game/terrain.cpp @@ -9,13 +9,10 @@ #include <gfx/models/mesh.h> #include <gfx/models/vertex.hpp> #include <glm/glm.hpp> +#include <maths.h> #include <random> #include <stb_image.h> -template<unsigned int Comp> class TerrainComp : public Mesh { - using Mesh::Mesh; -}; - Terrain::Terrain() : grass {Texture::cachedTexture.get("grass.png")}, water {Texture::cachedTexture.get("water.png")} { constexpr auto size {241}; // Vertices @@ -66,7 +63,6 @@ Terrain::Terrain() : grass {Texture::cachedTexture.get("grass.png")}, water {Tex } } finish(size, size, vertices); - addWater(size, size, resolution); } Terrain::Terrain(const std::string & fileName) : @@ -89,7 +85,6 @@ Terrain::Terrain(const std::string & fileName) : } finish(map.width, map.height, vertices); - addWater(map.width, map.height, resolution); } void @@ -125,40 +120,27 @@ Terrain::finish(unsigned int width, unsigned int height, std::vector<Vertex> & v v(width, x, z).normal = -glm::normalize(glm::cross(c - a, d - b)); } } - meshes.create<TerrainComp<0>>(vertices, indices); + meshes.create<Mesh>(vertices, indices); } +static const Transform identity {}; +static const auto identityModel {identity.GetModel()}; + void -Terrain::addWater(unsigned int width, unsigned int height, unsigned int resolution) +Terrain::tick(TickDuration dur) { - 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)}; - vertices.emplace_back(glm::vec3 {-extentx, 0, -extentz}, glm::vec2 {0, 0}, glm::vec3 {0, 1, 0}); - vertices.emplace_back(glm::vec3 {-extentx, 0, extentz}, glm::vec2 {0, height}, glm::vec3 {0, 1, 0}); - vertices.emplace_back(glm::vec3 {extentx, 0, extentz}, glm::vec2 {width, height}, glm::vec3 {0, 1, 0}); - vertices.emplace_back(glm::vec3 {extentx, 0, -extentz}, glm::vec2 {width, 0}, glm::vec3 {0, 1, 0}); - indices.push_back(verticesCount); - indices.push_back(verticesCount + 1); - indices.push_back(verticesCount + 2); - indices.push_back(verticesCount); - indices.push_back(verticesCount + 2); - indices.push_back(verticesCount + 3); - meshes.create<TerrainComp<1>>(vertices, indices); + waveCycle += dur.count(); } -static const Transform identity {}; -static const auto identityModel {identity.GetModel()}; - void Terrain::render(const Shader & shader) const { - shader.setModel(identityModel); + shader.setModel(identityModel, Shader::Program::LandMass); grass->Bind(); - meshes.apply<TerrainComp<0>>(&Mesh::Draw); + meshes.apply(&Mesh::Draw); + + shader.setModel(identityModel, Shader::Program::Water); + shader.setUniform("waves", {waveCycle, 0, 0}); water->Bind(); - meshes.apply<TerrainComp<1>>(&Mesh::Draw); + meshes.apply(&Mesh::Draw); } diff --git a/game/terrain.h b/game/terrain.h index 1aed1c5..2cec963 100644 --- a/game/terrain.h +++ b/game/terrain.h @@ -20,13 +20,13 @@ public: void render(const Shader & shader) const override; - void tick(TickDuration) override { } + void tick(TickDuration) override; + float waveCycle {0.F}; private: static constexpr unsigned int NUM_BUFFERS {4}; 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> grass, water; |