Skip to content

Instantly share code, notes, and snippets.

@yorung
Created November 15, 2017 14:37
Show Gist options
  • Save yorung/6cd63021defaa167716bd5c4c84c2b54 to your computer and use it in GitHub Desktop.
Save yorung/6cd63021defaa167716bd5c4c84c2b54 to your computer and use it in GitHub Desktop.
Calc gerstner wave in C++.
struct Wave
{
Vec2 dir;
float amplitude;
float waveLength;
};
struct ImmutableCB
{
float waveSteepness;
float waveSpeed;
Wave waves[20];
};
static Vec3 CalcGerstnerWaveOffset(const ImmutableCB& cb, Vec3 location, float time)
{
Vec3 sum = Vec3(0, 0, 0);
for (int i = 0; i < dimof(cb.waves); i++)
{
const Wave& wave = cb.waves[i];
float wi = 2 / wave.waveLength;
float rad = (dot(wave.dir, Vec2(location.x, location.z)) + time * cb.waveSpeed) * wi;
float sine = std::sin(rad);
float cosine = std::cos(rad);
sum.y += sine * wave.amplitude;
sum.x += wave.dir.x * cosine * cb.waveSteepness / (wi * dimof(cb.waves));
sum.z += wave.dir.y * cosine * cb.waveSteepness / (wi * dimof(cb.waves));
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment