Heightfield 1.0
GPUHydraulicErosionParticle Class Reference

Hydraulic erosion simulation on the GPU. More...

#include <gputerrainsimu.h>

Public Member Functions

 GPUHydraulicErosionParticle ()
 Constructor. Initialize everthing to zero.
 
 ~GPUHydraulicErosionParticle ()
 Destructor. Destroys OpenGl buffers and shader program.
 
void Init (const HeightField &hf)
 Initialize the erosion engine from a heightfield. Must be called before any Step().
 
void Step (int n)
 Performs N step of hydraulic erosion on the underlying heightfield.
 
void SetUniforms (float inertia, float sedimentCapacity, float depositSpeed, float evaporateSpeed, float erodeSpeed, int maxLifetime, int erosionBrushRadius)
 Set the uniforms in the shader.
 
void SetUniforms (float depositSpeed, float erosionSpeed, float evaporationSpeed, float randomSedimentation, float randomSedFreq, float randomSedAmp)
 Set the uniforms in the shader.
 
void SetUniforms (float depositSpeed, float erosionSpeed, float evaporationSpeed, int erosionBrushRadius, int maxLifeTime)
 Set the uniforms in the shader.
 
void SetGlobalSpeed (float globalSpeed)
 Set the global erosion speed uniform which affects both erosion and deposition.
 
void SetHardnessAlpha (const ScalarField2 &hardness)
 Set the hardness alpha map in a buffer.
 
void UseHardness (bool use=true)
 Flag for using the hardness alpha map or not.
 
void SetHardnessFunction (int index)
 Set the hardness function to use in the shader. See function GetHardness in the shader for more details.
 
void UsePrecipitation (bool use=true)
 Flag for using the precipitation map or not.
 
void SetPrecipitation (const ScalarField2 &precipitation)
 Set the precipitation map.
 
void GetData (ScalarField2 &hf)
 Reads back newest bedrock and sediment elevation from the GPU, into initialized scalarfields.
 
void GetData (ScalarField2 &bedrock, ScalarField2 &sediments)
 Reads back newest bedrock and sediment elevation from the GPU, into initialized scalarfields. The sediment scalarfield is normalized.
 
void GetDataHardness (ScalarField2 &hardness)
 Get hardness data back from the GPU and store it in a scalar field.
 

Protected Attributes

GLShader simulationShader
 Compute shader.
 
GLBuffer bedrockBuffer
 Bedrock buffer.
 
GLBuffer sedimentBuffer
 Sediment buffer.
 
GLBuffer randomIndicesBuffer
 Random number buffer. Easier than generating random number on the GPU.
 
GLBuffer brushIndicesBuffer
 Compute buffer.
 
GLBuffer brushWeightsBuffer
 Compute buffer.
 
GLOptionalStorageBuffer hardnessBuffer
 Optional Hardness buffer.
 
GLBuffer outHardnessBuffer
 Output hardness buffer (useful if we use the GPU function to generate hardness on the fly)
 
GLOptionalStorageBuffer precipitationBuffer
 Precipitation buffer.
 
int totalBufferSize
 Total buffer size defined as nx * ny.
 
int dispatchSize
 Single dispatch size.
 
int nx
 Heightfield dimension.
 
int ny
 Heightfield dimension.
 
double scaleZa
 Heightfield min elevation.
 
double scaleZb
 Heightfield max elevation.
 
std::vector< int > randomIndices
 Random indices for the algorithm.
 
std::vector< float > tmpData
 Temporary array for retreiving GPU data.
 

Detailed Description

Hydraulic erosion simulation on the GPU.

The algorithm uses a particle-based approach all simulated independently on the terrain.

The implementation is based on https://github.com/SebLague/Hydraulic-Erosion It has a race condition: multiple threads can be writing to the elevation buffer at the same time, Thus making some operations not taken into account. The original shader suffers from the same issue.

AXEL : this doesn't work anymore, have to figure it out. Weird buffer issue ?

Member Function Documentation

◆ GetDataHardness()

void GPUHydraulicErosionParticle::GetDataHardness ( ScalarField2 & hardness)

Get hardness data back from the GPU and store it in a scalar field.

Parameters
hardnessthe returned hardness data

◆ Init()

void GPUHydraulicErosionParticle::Init ( const HeightField & hf)

Initialize the erosion engine from a heightfield. Must be called before any Step().

Parameters
hfheightfield

◆ SetGlobalSpeed()

void GPUHydraulicErosionParticle::SetGlobalSpeed ( float globalSpeed)

Set the global erosion speed uniform which affects both erosion and deposition.

Parameters
globalSpeedspeed, should be between [0, 1]

◆ SetHardnessAlpha()

void GPUHydraulicErosionParticle::SetHardnessAlpha ( const ScalarField2 & hardness)

Set the hardness alpha map in a buffer.

Parameters
hardnesshardness alpja.

◆ SetHardnessFunction()

void GPUHydraulicErosionParticle::SetHardnessFunction ( int index)

Set the hardness function to use in the shader. See function GetHardness in the shader for more details.

Parameters
index0 = Simple strata 1 = Warped strata 2 = 3D noise 3 = Faulting The hardness function can be combined with an alpha map

◆ SetPrecipitation()

void GPUHydraulicErosionParticle::SetPrecipitation ( const ScalarField2 & precipitation)

Set the precipitation map.

Parameters
precipitationthe map

◆ SetUniforms() [1/3]

void GPUHydraulicErosionParticle::SetUniforms ( float depositSpeed,
float erosionSpeed,
float evaporationSpeed,
float randomSedimentation,
float randomSedFreq,
float randomSedAmp )

Set the uniforms in the shader.

Parameters
depositSpeed
erosionSpeed
evaporationSpeed
randomSedimentation
randomSedFreq
randomSedAmp

◆ SetUniforms() [2/3]

void GPUHydraulicErosionParticle::SetUniforms ( float depositSpeed,
float erosionSpeed,
float evaporationSpeed,
int erosionBrushRadius,
int maxLifeTime )

Set the uniforms in the shader.

Parameters
depositSpeed
erosionSpeed
evaporationSpeed
erosionBrushRadius
maxLifeTime

◆ SetUniforms() [3/3]

void GPUHydraulicErosionParticle::SetUniforms ( float inertia,
float sedimentCapacity,
float depositSpeed,
float evaporateSpeed,
float erodeSpeed,
int maxLifetime,
int erosionBrushRadius )

Set the uniforms in the shader.

Parameters
inertia
sedimentCapacity
depositSpeed
evaporateSpeed
erodeSpeed
maxLifetime
erosionBrushRadius

◆ Step()

void GPUHydraulicErosionParticle::Step ( int n)

Performs N step of hydraulic erosion on the underlying heightfield.

Parameters
nstep count

◆ UseHardness()

void GPUHydraulicErosionParticle::UseHardness ( bool use = true)

Flag for using the hardness alpha map or not.

Parameters
use

◆ UsePrecipitation()

void GPUHydraulicErosionParticle::UsePrecipitation ( bool use = true)

Flag for using the precipitation map or not.

Parameters
use