Rendu
A lightweight rendering engine for experimentations
Loading...
Searching...
No Matches
Terrain.hpp
1
2#include "resources/Image.hpp"
3#include "resources/Texture.hpp"
4#include "resources/Mesh.hpp"
5#include "processing/GaussianBlur.hpp"
6#include "generation/PerlinNoise.hpp"
7#include "generation/Random.hpp"
8#include "Common.hpp"
9
14class Terrain {
15public:
16
18 struct Cell {
20 uint level;
21
27 Cell(uint l, uint x, uint z);
28 };
29
34 Terrain(uint resolution, uint seed);
35
37 void generateMesh();
38
40 void generateMap();
41
45 void generateShadowMap(const glm::vec3 & lightDir);
46
50 bool interface();
51
53 ~Terrain();
54
56 float texelSize() const {
57 return _texelSize;
58 }
59
61 int gridSize() const {
62 return _mshOpts.size;
63 }
64
66 float meshSize() const {
67 return _meshSize * _texelSize;
68 }
69
71 const Texture & map() const {
72 return _map;
73 }
74
76 const Texture & shadowMap() const {
77 return _shadowMap;
78 }
79
81 const std::vector<Cell> & cells() const {
82 return _cells;
83 }
84
85private:
86
90 void erode(Image & img);
91
95 void transferAndUpdateMap(Image & heightMap);
96
99 float lacunarity = 2.0f;
100 float gain = 0.5f;
101 float scale = 0.01f;
102 float maxHeight = 2.0f;
103 float falloff = 4.0f;
104 float rescale = 1.25f;
105 int octaves = 8;
106 };
107
110 int size = 96;
111 int levels = 4;
112 };
113
116 float inertia = 0.6f;
117 float gravity = 10.0f;
118 float minSlope = 0.01f;
119 float capacityBase = 12.0f;
120 float erosion = 0.75f;
121 float evaporation = 0.02f;
122 float deposition = 0.2f;
123 int gatherRadius = 3;
124 int dropsCount = 50000;
125 int stepsMax = 256;
126 bool apply = true;
127 };
128
130 std::vector<Cell> _cells;
131 Texture _map = Texture("Terrain");
132 Texture _mapLowRes = Texture("Terrain low-res");
135
139
141 uint _seed;
142 float _texelSize = 0.05f;
143 float _meshSize = 0.0f;
144
145};
Applies an approximate gaussian blur using a dual filtering approach.
Definition: GaussianBlur.hpp:17
Represents an image composed of pixels with values in [0,1]. Provide image loading/saving utilities,...
Definition: Image.hpp:8
Represents a geometric mesh composed of vertices, other attributes and triangles.
Definition: Mesh.hpp:15
Generate 3D perlin noise and multi-layered noise.
Definition: PerlinNoise.hpp:10
Generate a terrain with Perlin noise and erosion. Represent the terrain, regrouping elevation and sha...
Definition: Terrain.hpp:14
int _resolution
Height map resolution.
Definition: Terrain.hpp:140
std::vector< Cell > _cells
Grid mesh cells.
Definition: Terrain.hpp:130
MeshSettings _mshOpts
Grid mesh options.
Definition: Terrain.hpp:137
void erode(Image &img)
Definition: Terrain.cpp:207
float _texelSize
Size of a map texel in world space.
Definition: Terrain.hpp:142
void generateMap()
Definition: Terrain.cpp:152
~Terrain()
Definition: Terrain.cpp:482
bool interface()
Definition: Terrain.cpp:434
const std::vector< Cell > & cells() const
Definition: Terrain.hpp:81
PerlinNoise _perlin
Perlin noise generator.
Definition: Terrain.hpp:129
int gridSize() const
Definition: Terrain.hpp:61
Texture _shadowMap
Shadow map generation texture.
Definition: Terrain.hpp:133
GaussianBlur _gaussBlur
Gaussian blur.
Definition: Terrain.hpp:134
Texture _mapLowRes
Low resolution min-height terrain map.
Definition: Terrain.hpp:132
const Texture & shadowMap() const
Definition: Terrain.hpp:76
void generateMesh()
Definition: Terrain.cpp:15
void transferAndUpdateMap(Image &heightMap)
Definition: Terrain.cpp:306
GenerationSettings _genOpts
Terrain generations settings.
Definition: Terrain.hpp:136
ErosionSettings _erOpts
Erosion options.
Definition: Terrain.hpp:138
const Texture & map() const
Definition: Terrain.hpp:71
float meshSize() const
Definition: Terrain.hpp:66
Texture _map
Terrain map, height in R channel, normals in GBA channels.
Definition: Terrain.hpp:131
uint _seed
Current generation seed.
Definition: Terrain.hpp:141
void generateShadowMap(const glm::vec3 &lightDir)
Definition: Terrain.cpp:405
float texelSize() const
Definition: Terrain.hpp:56
float _meshSize
Size of the mesh.
Definition: Terrain.hpp:143
Represents a texture containing one or more images, stored on the CPU and/or GPU.
Definition: Texture.hpp:12
Definition: Terrain.hpp:18
uint level
The density level used to generate the mesh of this cell.
Definition: Terrain.hpp:20
Mesh mesh
The geometry covering this cell.
Definition: Terrain.hpp:19
Definition: Terrain.hpp:115
int stepsMax
Number of steps for each droplet simulation.
Definition: Terrain.hpp:125
float erosion
Erosion strenght.
Definition: Terrain.hpp:120
bool apply
Should erosion be applied.
Definition: Terrain.hpp:126
float inertia
Inertia of water droplet.
Definition: Terrain.hpp:116
float minSlope
Minimal slope, to avoid strong erosion spots.
Definition: Terrain.hpp:118
int dropsCount
Number of droplets to sequentially simulate.
Definition: Terrain.hpp:124
float capacityBase
Base water capacity.
Definition: Terrain.hpp:119
float deposition
Deposition speed.
Definition: Terrain.hpp:122
float gravity
Gravity.
Definition: Terrain.hpp:117
int gatherRadius
Gathering radius for contributions.
Definition: Terrain.hpp:123
float evaporation
Evaporation speed.
Definition: Terrain.hpp:121
Definition: Terrain.hpp:98
float scale
The base frequency, in pixels.
Definition: Terrain.hpp:101
int octaves
Number of noise layers.
Definition: Terrain.hpp:105
float lacunarity
The frequency ratio between successive noise layers.
Definition: Terrain.hpp:99
float rescale
General height rescaling.
Definition: Terrain.hpp:104
float falloff
Strength of the circular falloff.
Definition: Terrain.hpp:103
float maxHeight
Maximum height.
Definition: Terrain.hpp:102
float gain
The amplitude ratio between successive noise layers.
Definition: Terrain.hpp:100
Definition: Terrain.hpp:109
int levels
Number of levels of detail.
Definition: Terrain.hpp:111
int size
Grid dimensions.
Definition: Terrain.hpp:110