Skip to content

Instantly share code, notes, and snippets.

@chuwilliamson
Created March 1, 2016 16:29
Show Gist options
  • Save chuwilliamson/31a20e24d9a5c4fc181f to your computer and use it in GitHub Desktop.
Save chuwilliamson/31a20e24d9a5c4fc181f to your computer and use it in GitHub Desktop.
PerlinNoise
void BringTheNoise()
{
/*
To start with, we will be using glm’s perlin noise function to generate our noise instead of
implementing our own. The glm function simply takes in a vec2 and returns a float between -1 and 1.
Before just passing in our x and y we want to make some changes to them. First, we scale our point
down, in this case by 10. This is because Perlin noise always returns 0 on integer boundaries. The
Perlin noise function also returns a number between -1 and 1. For our height map we want a
number between 0 and 1 so we can later multiply it by our max height. To do this we multiply by 0.5
and then add by 0.5.
*/
m_perlinDimensions = 64;
m_perlinData = new float[m_perlinDimensions * m_perlinDimensions];
// scale helps to control the noise
float scale = (1.0f / m_perlinDimensions) * 3;
int octaves = 6;
for (unsigned int x = 0; x < m_perlinDimensions; ++x) {
for (unsigned int y = 0; y < m_perlinDimensions; ++y) {
float amplitude = 1.f;
float persistence = 0.3f;
m_perlinData[x * m_perlinDimensions + y] = 0;
for (int o = 0; o < octaves; ++o)
{
float freq = powf(2, (float)o);
float perlin_sample = glm::perlin(vec2((float)x, (float)y) * scale * freq) * 0.5f + 0.5f;
m_perlinData[x * m_perlinDimensions + y] += perlin_sample * amplitude;
amplitude *= persistence;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment