17constexpr const char* kVertexShader = R
"(#version 330
18in vec3 vertexPosition;
21uniform mat4 matProjection;
24uniform sampler2D ground;
25uniform float halfSize;
28uniform float wavelength;
32// From the ground up to the water, at each pixel.
34// How deep the still sea is, at one of the triangle's corners.
36// How high up its wave that corner is, from 0 at the bottom to 1 at the top.
39const vec2 kWays[3] = vec2[](vec2(1.0, 0.35), vec2(-0.45, 1.0),
41// Each one's length, as a share of the longest's.
42const float kLengths[3] = float[](1.0, 0.71, 0.53);
45 world = (matModel * vec4(vertexPosition, 1.0)).xyz;
46 // The corner of the ground's cells under this corner of the sea, which is
47 // the edge's past the edge of the terrain.
48 ivec2 corner = clamp(ivec2(round((world.xz + halfSize) / cell)), 0,
49 textureSize(ground, 0).x - 1);
50 float bottom = texelFetch(ground, corner, 0).r;
51 below = world.y - bottom;
52 // Far away, a cell is a few pixels, and waves that small only flicker.
53 float calm = 1.0 - smoothstep(40.0, 100.0, distance(world.xz, eye.xz));
54 // In the shallows, waves lap at the shore.
55 calm *= mix(0.25, 1.0, smoothstep(0.0, 2.5, below));
57 for (int i = 0; i < 3; ++i) {
58 vec2 way = normalize(kWays[i]);
59 float k = 6.2831853 / (wavelength * kLengths[i]);
60 // Each moves as fast as a wave of its length does at sea, at its pace.
61 float phase = dot(way, world.xz) * k + seconds * pace * sqrt(9.8 * k);
62 wave += sin(phase) / 3.0;
64 crest = 0.5 + 0.5 * wave;
65 world.y += height * calm * wave;
66 gap = world.y - bottom;
67 gl_Position = matProjection * matView * vec4(world, 1.0);
68 depth = gl_Position.w;
74constexpr const char* kFragmentShader = R
"(#version 330
89uniform float emission;
97 // How far the world moves from this pixel to the next one across and the
98 // next one up. Both lie in the triangle's face, so across them is its
99 // normal, the same for each of its pixels.
100 vec3 surface = normalize(cross(dFdx(world), dFdy(world)));
101 // Turned up, whichever way round the two were.
102 if (surface.y < 0.0) {
105 float sunlit = max(dot(surface, normalize(sun)), 0.0);
107 sunlit = min(floor(sunlit * bands), bands - 1.0) / (bands - 1.0);
109 vec4 color = mix(shallow, deep, smoothstep(0.2, 2.0, below));
110 // Along the line where it meets the ground.
111 color = mix(color, edge, 1.0 - smoothstep(0.08, 0.14, gap));
112 // The tops of the waves give off the most light, and the bottoms none.
113 vec3 light = ambient + sunlight * sunlit + emission * 4.0 * crest * crest;
114 // The sun, reflected off the surface, where it comes back to the eye.
115 vec3 reflected = reflect(-normalize(sun), surface);
116 float shine = pow(max(dot(reflected, normalize(eye - world)), 0.0), 24.0);
117 vec3 lit = color.rgb * light + sunlight * shine * glint;
118 float fog = clamp((depth - fogNear) / (fogFar - fogNear), 0.0, 1.0);
119 // Into the sky towards the edge of the sea, wherever the fog ends.
120 fog = max(fog, smoothstep(reach - 30.0, reach, distance(world.xz, eye.xz)));
121 pixel = vec4(mix(lit, fogColor, fog), color.a);
126constexpr float kReach = 160.0f;
128void Set(Shader shader,
const char* name,
const void* value,
int type) {
129 SetShaderValue(shader, GetShaderLocation(shader, name), value, type);
132void SetColor(Shader shader,
const char* name, Color color) {
133 const Vector4 normalized = ColorNormalize(color);
134 Set(shader, name, &normalized, SHADER_UNIFORM_VEC4);
140 : shader_(
LoadGlsl(kVertexShader, kFragmentShader)) {
144 const int cells =
static_cast<int>(2.0f * kReach /
Terrain::kCell);
145 const auto corner = [](
int i,
int j) {
150 for (
int j = 0; j < cells; ++j) {
151 for (
int i = 0; i < cells; ++i) {
152 grid.
Add(corner(i, j), corner(i + 1, j + 1), corner(i + 1, j), WHITE);
153 grid.
Add(corner(i, j), corner(i, j + 1), corner(i + 1, j + 1), WHITE);
156 model_ = LoadModelFromMesh(grid.
Upload());
157 model_.materials[0].shader = shader_;
158 Set(shader_,
"reach", &kReach, SHADER_UNIFORM_FLOAT);
161 SetColor(shader_,
"deep", liquid.
deep);
162 SetColor(shader_,
"shallow", liquid.
shallow);
163 SetColor(shader_,
"edge", liquid.
edge);
164 Set(shader_,
"height", &liquid.
height, SHADER_UNIFORM_FLOAT);
165 Set(shader_,
"wavelength", &liquid.
wavelength, SHADER_UNIFORM_FLOAT);
166 Set(shader_,
"pace", &liquid.
pace, SHADER_UNIFORM_FLOAT);
167 Set(shader_,
"glint", &liquid.
glint, SHADER_UNIFORM_FLOAT);
168 Set(shader_,
"emission", &liquid.
emission, SHADER_UNIFORM_FLOAT);
172 const int corners = terrain.
cells() + 1;
173 std::vector<float> heights(corners * corners);
174 const Grid<float> ground(heights.data(), corners, corners);
175 for (
int j = 0; j < corners; ++j) {
176 for (
int i = 0; i < corners; ++i) {
177 ground[j, i] = terrain.
Corner(i, j);
180 ground_ = LoadTextureFromImage({.data = heights.data(),
184 .format = PIXELFORMAT_UNCOMPRESSED_R32});
186 model_.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = ground_;
187 shader_.locs[SHADER_LOC_MAP_DIFFUSE] = GetShaderLocation(shader_,
"ground");
188 const float half_size = terrain.
half_size();
189 Set(shader_,
"halfSize", &half_size, SHADER_UNIFORM_FLOAT);
196 UnloadTexture(ground_);
197 UnloadShader(shader_);
201 float bands,
float seconds)
const {
202 Set(shader_,
"sun", atmosphere.
sun.data(), SHADER_UNIFORM_VEC3);
203 Set(shader_,
"sunlight", atmosphere.
sunlight.data(), SHADER_UNIFORM_VEC3);
204 Set(shader_,
"ambient", atmosphere.
ambient.data(), SHADER_UNIFORM_VEC3);
205 Set(shader_,
"fogColor", atmosphere.
sky.data(), SHADER_UNIFORM_VEC3);
206 Set(shader_,
"fogNear", &atmosphere.
fog_near, SHADER_UNIFORM_FLOAT);
207 Set(shader_,
"fogFar", &atmosphere.
fog_far, SHADER_UNIFORM_FLOAT);
208 Set(shader_,
"bands", &bands, SHADER_UNIFORM_FLOAT);
209 Set(shader_,
"eye", &camera.position, SHADER_UNIFORM_VEC3);
210 Set(shader_,
"seconds", &seconds, SHADER_UNIFORM_FLOAT);
218 DrawMesh(model_.meshes[0], model_.materials[0],
void Draw(const Camera3D &camera, const Atmosphere &atmosphere, float bands, float seconds) const
Sea(const Terrain &terrain, const Look &look)
float half_size() const
From the middle to each side of the grid.
static constexpr float kCell
In metres. Each cell of the grid is two triangles.
static constexpr float kSeaLevel
float Corner(int i, int j) const
The height at a corner of the grid, from 0 to cells() along each side.
int cells() const
Along each side.
void Add(Vector3 a, Vector3 b, Vector3 c, Color color)
Mesh Upload() const
Hands them to the GPU, as a mesh for LoadModelFromMesh.
Shader LoadGlsl(const char *vertex, const char *fragment)
The sky and the light, which the shaders read each frame.
std::array< float, 3 > ambient
What a face gets even when it's turned away from the sun.
std::array< float, 3 > sunlight
What the sun adds to a face that's turned to it.
std::array< float, 3 > sky
The background, which the fog fades into.
std::array< float, 3 > sun
Towards the sun.
What a land's sea is made of: its colours, its waves and its light.
Color edge
Along the line where it meets the ground: foam on water, a crust on lava.
float height
In metres, from the still surface to the top of a wave.
Color deep
Out where it's deep. Its alpha is how much of the ground it hides.
Color shallow
Where it's shallow, near the shore.
float glint
How much of the sun it reflects to the eye.
float pace
How fast its waves go, where 1 is as fast as water's.
A land's colours: its atmosphere, and the colours that go into its meshes.