nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
sea.cc
Go to the documentation of this file.
1#include "client/sea.h"
2
3#include <raymath.h>
4
5#include <cmath>
6#include <vector>
7
8#include "client/glsl.h"
9#include "client/triangles.h"
10
11namespace nanommo {
12
13namespace {
14
15// Waves: three of them, each its own way and length, raise and lower each
16// corner of the sea, less where it's shallow.
17constexpr const char* kVertexShader = R"(#version 330
18in vec3 vertexPosition;
19uniform mat4 matModel;
20uniform mat4 matView;
21uniform mat4 matProjection;
22uniform vec3 eye;
23uniform float seconds;
24uniform sampler2D ground;
25uniform float halfSize;
26uniform float cell;
27uniform float height;
28uniform float wavelength;
29uniform float pace;
30out vec3 world;
31out float depth;
32// From the ground up to the water, at each pixel.
33out float gap;
34// How deep the still sea is, at one of the triangle's corners.
35flat out float below;
36// How high up its wave that corner is, from 0 at the bottom to 1 at the top.
37flat out float crest;
38
39const vec2 kWays[3] = vec2[](vec2(1.0, 0.35), vec2(-0.45, 1.0),
40 vec2(0.6, -0.8));
41// Each one's length, as a share of the longest's.
42const float kLengths[3] = float[](1.0, 0.71, 0.53);
43
44void main() {
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));
56 float wave = 0.0;
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;
63 }
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;
69}
70)";
71
72// Facets: each triangle is lit flat, like the ground's, with the normal of its
73// face.
74constexpr const char* kFragmentShader = R"(#version 330
75in vec3 world;
76in float depth;
77in float gap;
78flat in float below;
79flat in float crest;
80uniform vec4 deep;
81uniform vec4 shallow;
82uniform vec4 edge;
83uniform vec3 sun;
84uniform vec3 sunlight;
85uniform vec3 ambient;
86uniform vec3 fogColor;
87uniform float fogNear;
88uniform float fogFar;
89uniform float emission;
90uniform float glint;
91uniform float bands;
92uniform vec3 eye;
93uniform float reach;
94out vec4 pixel;
95
96void main() {
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) {
103 surface = -surface;
104 }
105 float sunlit = max(dot(surface, normalize(sun)), 0.0);
106 if (bands > 0.0) {
107 sunlit = min(floor(sunlit * bands), bands - 1.0) / (bands - 1.0);
108 }
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);
122}
123)";
124
125// In metres from the camera to each side of the sea, a little past the fog.
126constexpr float kReach = 160.0f;
127
128void Set(Shader shader, const char* name, const void* value, int type) {
129 SetShaderValue(shader, GetShaderLocation(shader, name), value, type);
130}
131
132void SetColor(Shader shader, const char* name, Color color) {
133 const Vector4 normalized = ColorNormalize(color);
134 Set(shader, name, &normalized, SHADER_UNIFORM_VEC4);
135}
136
137} // namespace
138
139Sea::Sea(const Terrain& terrain, const Look& look)
140 : shader_(LoadGlsl(kVertexShader, kFragmentShader)) {
141 // A square around the camera, cut into the ground's cells, two triangles
142 // each, the way the ground's are, which the waves move. So the ground under
143 // each triangle is flat, and the gap up to the water is right at each pixel.
144 const int cells = static_cast<int>(2.0f * kReach / Terrain::kCell);
145 const auto corner = [](int i, int j) {
146 return Vector3{i * Terrain::kCell - kReach, 0.0f,
147 j * Terrain::kCell - kReach};
148 };
149 Triangles grid;
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);
154 }
155 }
156 model_ = LoadModelFromMesh(grid.Upload());
157 model_.materials[0].shader = shader_;
158 Set(shader_, "reach", &kReach, SHADER_UNIFORM_FLOAT);
159
160 const Liquid& liquid = look.sea;
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);
169
170 // The ground's heights, one float a corner, as a texture the vertex shader
171 // can read.
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);
178 }
179 }
180 ground_ = LoadTextureFromImage({.data = heights.data(),
181 .width = corners,
182 .height = corners,
183 .mipmaps = 1,
184 .format = PIXELFORMAT_UNCOMPRESSED_R32});
185 // raylib hands the material's textures to the shader as it draws.
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);
190 Set(shader_, "cell", &Terrain::kCell, SHADER_UNIFORM_FLOAT);
191}
192
193// UnloadModel leaves the model's shader and textures alone.
195 UnloadModel(model_);
196 UnloadTexture(ground_);
197 UnloadShader(shader_);
198}
199
200void Sea::Draw(const Camera3D& camera, const Atmosphere& atmosphere,
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);
211 // It follows the camera a whole cell at a time, so that each corner of it
212 // stays where a corner of the ground is, and the waves, which go by where
213 // they are in the world, don't move with it.
214 const float x =
215 std::round(camera.position.x / Terrain::kCell) * Terrain::kCell;
216 const float z =
217 std::round(camera.position.z / Terrain::kCell) * Terrain::kCell;
218 DrawMesh(model_.meshes[0], model_.materials[0],
219 MatrixTranslate(x, Terrain::kSeaLevel, z));
220}
221
222} // namespace nanommo
void Draw(const Camera3D &camera, const Atmosphere &atmosphere, float bands, float seconds) const
Definition sea.cc:200
Sea(const Terrain &terrain, const Look &look)
Definition sea.cc:139
float half_size() const
From the middle to each side of the grid.
Definition terrain.h:88
static constexpr float kCell
In metres. Each cell of the grid is two triangles.
Definition terrain.h:48
static constexpr float kSeaLevel
Definition terrain.h:50
float Corner(int i, int j) const
The height at a corner of the grid, from 0 to cells() along each side.
Definition terrain.h:78
int cells() const
Along each side.
Definition terrain.h:85
void Add(Vector3 a, Vector3 b, Vector3 c, Color color)
Definition triangles.cc:27
Mesh Upload() const
Hands them to the GPU, as a mesh for LoadModelFromMesh.
Definition triangles.cc:36
Definition chat.cc:9
Shader LoadGlsl(const char *vertex, const char *fragment)
Definition glsl.cc:32
The sky and the light, which the shaders read each frame.
Definition atmosphere.h:13
std::array< float, 3 > ambient
What a face gets even when it's turned away from the sun.
Definition atmosphere.h:25
std::array< float, 3 > sunlight
What the sun adds to a face that's turned to it.
Definition atmosphere.h:23
std::array< float, 3 > sky
The background, which the fog fades into.
Definition atmosphere.h:15
std::array< float, 3 > sun
Towards the sun.
Definition atmosphere.h:21
What a land's sea is made of: its colours, its waves and its light.
Definition atmosphere.h:46
Color edge
Along the line where it meets the ground: foam on water, a crust on lava.
Definition atmosphere.h:52
float height
In metres, from the still surface to the top of a wave.
Definition atmosphere.h:54
Color deep
Out where it's deep. Its alpha is how much of the ground it hides.
Definition atmosphere.h:48
Color shallow
Where it's shallow, near the shore.
Definition atmosphere.h:50
float glint
How much of the sun it reflects to the eye.
Definition atmosphere.h:61
float pace
How fast its waves go, where 1 is as fast as water's.
Definition atmosphere.h:59
A land's colours: its atmosphere, and the colours that go into its meshes.
Definition atmosphere.h:69