summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-02-20 00:17:58 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-02-20 00:17:58 +0000
commit2bc4bcebc93e7211dfb84303888635f888ba8018 (patch)
tree3a349beadc079c544001f3a65ac0e26fe494f8e5
parentCreate smoother terrain (diff)
downloadilt-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.
-rw-r--r--game/terrain.cpp44
-rw-r--r--game/terrain.h4
-rw-r--r--gfx/gl/shader.cpp18
-rw-r--r--gfx/gl/shader.h4
-rw-r--r--gfx/gl/shaders/landmassShader.fs41
-rw-r--r--gfx/gl/shaders/landmassShader.vs21
-rw-r--r--gfx/gl/shaders/waterShader.fs13
-rw-r--r--gfx/gl/shaders/waterShader.vs23
-rw-r--r--res/grass.pngbin26864 -> 35625 bytes
-rw-r--r--res/water.pngbin16779 -> 103700 bytes
10 files changed, 130 insertions, 38 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;
diff --git a/gfx/gl/shader.cpp b/gfx/gl/shader.cpp
index 8c58c39..454ad4b 100644
--- a/gfx/gl/shader.cpp
+++ b/gfx/gl/shader.cpp
@@ -1,7 +1,11 @@
#include "shader.h"
#include <array>
#include <gfx/gl/shaders/fs-basicShader.h>
+#include <gfx/gl/shaders/fs-landmassShader.h>
+#include <gfx/gl/shaders/fs-waterShader.h>
#include <gfx/gl/shaders/vs-basicShader.h>
+#include <gfx/gl/shaders/vs-landmassShader.h>
+#include <gfx/gl/shaders/vs-waterShader.h>
#include <glm/glm.hpp>
#include <stdexcept>
#include <string>
@@ -28,9 +32,17 @@ Shader::ProgramHandle::ProgramHandle(std::initializer_list<GLuint> srcs) : viewP
Shader::Shader() :
programs {{{
- Source {{basicShader_vs, basicShader_vs_len}, GL_VERTEX_SHADER}.id,
- Source {{basicShader_fs, basicShader_fs_len}, GL_FRAGMENT_SHADER}.id,
- }}}
+ Source {{basicShader_vs, basicShader_vs_len}, GL_VERTEX_SHADER}.id,
+ Source {{basicShader_fs, basicShader_fs_len}, GL_FRAGMENT_SHADER}.id,
+ },
+ {
+ Source {{waterShader_vs, waterShader_vs_len}, GL_VERTEX_SHADER}.id,
+ Source {{waterShader_fs, waterShader_fs_len}, GL_FRAGMENT_SHADER}.id,
+ },
+ {
+ Source {{landmassShader_vs, landmassShader_vs_len}, GL_VERTEX_SHADER}.id,
+ Source {{landmassShader_fs, landmassShader_fs_len}, GL_FRAGMENT_SHADER}.id,
+ }}}
{
}
diff --git a/gfx/gl/shader.h b/gfx/gl/shader.h
index 0282f21..5d26781 100644
--- a/gfx/gl/shader.h
+++ b/gfx/gl/shader.h
@@ -9,7 +9,7 @@
class Shader {
public:
- enum class Program { Basic = 0 };
+ enum class Program { Basic = 0, Water = 1, LandMass = 2 };
Shader();
@@ -39,7 +39,7 @@ private:
GLint viewProjection_uniform, model_uniform;
};
- std::array<ProgramHandle, 1> programs;
+ std::array<ProgramHandle, 3> programs;
};
#endif
diff --git a/gfx/gl/shaders/landmassShader.fs b/gfx/gl/shaders/landmassShader.fs
new file mode 100644
index 0000000..36cc971
--- /dev/null
+++ b/gfx/gl/shaders/landmassShader.fs
@@ -0,0 +1,41 @@
+#version 130
+
+in vec2 texCoord0;
+in vec3 normal0;
+in float height;
+
+uniform sampler2D sampler;
+uniform vec3 lightDirection;
+uniform vec3 lightColor;
+uniform vec3 ambientColor;
+
+const vec3 grass = vec3(.1, .4, .05);
+const vec3 sand = vec3(.76, .7, .5);
+const vec3 snow = vec3(.97, .97, .99);
+
+vec2
+grad_between(float x, float lower, float upper)
+{
+ float off = (x - lower) / (upper - lower);
+ return vec2(off, 1 - off);
+}
+
+void
+main()
+{
+ gl_FragColor = texture(sampler, texCoord0);
+ gl_FragColor.xyz *= clamp(ambientColor + (dot(-lightDirection, normal0) * lightColor), 0.0, 1.0);
+ if (height < 0.5) {
+ gl_FragColor.rgb *= sand;
+ }
+ else if (height > 30) {
+ gl_FragColor.rgb *= snow;
+ }
+ else if (height > 28) {
+ vec2 grad = grad_between(height, 28, 30);
+ gl_FragColor.rgb *= grass + (snow - grass) * grad.x;
+ }
+ else {
+ gl_FragColor.rgb *= grass;
+ }
+}
diff --git a/gfx/gl/shaders/landmassShader.vs b/gfx/gl/shaders/landmassShader.vs
new file mode 100644
index 0000000..72c9060
--- /dev/null
+++ b/gfx/gl/shaders/landmassShader.vs
@@ -0,0 +1,21 @@
+#version 130
+
+in vec3 position;
+in vec2 texCoord;
+in vec3 normal;
+
+out vec2 texCoord0;
+out vec3 normal0;
+out float height;
+
+uniform mat4 viewProjection;
+uniform mat4 model;
+uniform vec3 waves;
+
+void main()
+{
+ gl_Position = viewProjection * model * vec4(position, 1.0);
+ texCoord0 = texCoord;
+ normal0 = normal;
+ height = position.y;
+}
diff --git a/gfx/gl/shaders/waterShader.fs b/gfx/gl/shaders/waterShader.fs
new file mode 100644
index 0000000..bdfbc33
--- /dev/null
+++ b/gfx/gl/shaders/waterShader.fs
@@ -0,0 +1,13 @@
+#version 130
+
+in vec2 texCoord0;
+in float depth;
+
+uniform sampler2D sampler;
+uniform vec3 waves;
+
+void main()
+{
+ gl_FragColor = texture(sampler, texCoord0);
+ gl_FragColor.a *= clamp(-depth * .7, .1, 1.0);
+}
diff --git a/gfx/gl/shaders/waterShader.vs b/gfx/gl/shaders/waterShader.vs
new file mode 100644
index 0000000..55a0fb8
--- /dev/null
+++ b/gfx/gl/shaders/waterShader.vs
@@ -0,0 +1,23 @@
+#version 130
+
+in vec3 position;
+in vec2 texCoord;
+in vec3 normal;
+
+out vec2 texCoord0;
+out float depth;
+
+uniform mat4 viewProjection;
+uniform mat4 model;
+uniform vec3 waves;
+
+void main()
+{
+ vec3 wpos = vec3(
+ position.x + cos(waves.x),
+ cos(waves.x + position.x + (position.z / 7)) * .3,
+ position.z + cos(waves.x * waves.z));
+ gl_Position = viewProjection * model * vec4(wpos, 1.0);
+ texCoord0 = texCoord;
+ depth = position.y;
+}
diff --git a/res/grass.png b/res/grass.png
index 9f410e1..2f76277 100644
--- a/res/grass.png
+++ b/res/grass.png
Binary files differ
diff --git a/res/water.png b/res/water.png
index ed7a284..4493852 100644
--- a/res/water.png
+++ b/res/water.png
Binary files differ