nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
ground.cc
Go to the documentation of this file.
1#include "client/ground.h"
2
3#include <algorithm>
4#include <cstdint>
5
6#include "terrain/noise.h"
7
8namespace nanommo {
9
10namespace {
11
12constexpr float kBeach = 0.6f;
13constexpr float kTreeLine = 12.0f;
14constexpr float kSnowLine = 19.0f;
15
16// The y of a face's normal: below this, it's too steep for grass.
17constexpr float kSteep = 0.8f;
18
19} // namespace
20
21Surface SurfaceAt(float height, float up) {
22 if (height < kBeach) {
23 return Surface::kSand;
24 }
25 if (height > kSnowLine) {
26 return Surface::kSnow;
27 }
28 if (height > kTreeLine || up < kSteep) {
29 return Surface::kRock;
30 }
31 return Surface::kGrass;
32}
33
34Color GroundColor(const Look& look, float height, float up) {
35 switch (SurfaceAt(height, up)) {
36 case Surface::kSand:
37 return look.sand;
38 case Surface::kSnow:
39 return look.snow;
40 case Surface::kRock:
41 return look.rock;
42 case Surface::kGrass:
43 return look.grass;
44 }
45 return look.grass;
46}
47
48namespace {
49
50// Up to four percent lighter or darker, so that faces of one colour still
51// stand apart.
52Color Vary(Color color, uint32_t hash) {
53 const float shade = 0.96f + 0.08f * static_cast<float>(hash & 0xff) / 255.0f;
54 const auto scaled = [shade](unsigned char value) {
55 return static_cast<unsigned char>(std::min(value * shade, 255.0f));
56 };
57 return {scaled(color.r), scaled(color.g), scaled(color.b), color.a};
58}
59
60void AddFace(Triangles& triangles, const Look& look, Vector3 a, Vector3 b,
61 Vector3 c, uint32_t hash) {
62 const float height = (a.y + b.y + c.y) / 3.0f;
63 const Color color = GroundColor(look, height, FaceNormal(a, b, c).y);
64 triangles.Add(a, b, c, Vary(color, hash));
65}
66
67} // namespace
68
69void AddGround(Triangles& triangles, const Terrain& terrain, const Look& look,
70 int left, int top, int cells) {
71 const float half_size = terrain.half_size();
72 const auto corner = [&](int i, int j) {
73 return Vector3{i * Terrain::kCell - half_size, terrain.Corner(i, j),
74 j * Terrain::kCell - half_size};
75 };
76 for (int j = top; j < std::min(top + cells, terrain.cells()); ++j) {
77 for (int i = left; i < std::min(left + cells, terrain.cells()); ++i) {
78 const Vector3 top_left = corner(i, j);
79 const Vector3 top_right = corner(i + 1, j);
80 const Vector3 bottom_left = corner(i, j + 1);
81 const Vector3 bottom_right = corner(i + 1, j + 1);
82 AddFace(triangles, look, top_left, bottom_right, top_right,
83 Hash(terrain.seed(), 2 * i, j));
84 AddFace(triangles, look, top_left, bottom_left, bottom_right,
85 Hash(terrain.seed(), 2 * i + 1, j));
86 }
87 }
88}
89
90} // namespace nanommo
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
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
uint32_t seed() const
Definition terrain.h:82
int cells() const
Along each side.
Definition terrain.h:85
Definition chat.cc:9
uint32_t Hash(uint32_t seed, int32_t x, int32_t z)
Definition noise.cc:22
Surface
Definition ground.h:15
void AddGround(Triangles &triangles, const Terrain &terrain, const Look &look, int left, int top, int cells)
Definition ground.cc:69
Color GroundColor(const Look &look, float height, float up)
The surface's colour in the look.
Definition ground.cc:34
Surface SurfaceAt(float height, float up)
Definition ground.cc:21
Vector3 FaceNormal(Vector3 a, Vector3 b, Vector3 c)
Definition triangles.cc:22
A land's colours: its atmosphere, and the colours that go into its meshes.
Definition atmosphere.h:69