Repository navigation
Expand file tree
/
Copy pathNoise.cpp
More file actions
78 lines (60 loc) · 2.06 KB
/
Copy pathNoise.cpp
File metadata and controls
78 lines (60 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include"Noise.h"
void Noise::PerlinNoise1D(int nCount, float* fSeed, int nOctaves, float fBias, float* fOutput)
{
for (int x = 0; x < nCount; x++)
{
float fNoise = 0.0f;
float fScaleAcc = 0.0f;
float fScale = 1.0f;
for (int o = 0; o < nOctaves; o++)
{
int nPitch = nCount >> o;
int nSample1 = (x / nPitch) * nPitch;
int nSample2 = (nSample1 + nPitch) % nCount;
float fBlend = (float)(x - nSample1) / (float)nPitch;
float fSample = (1.0f - fBlend) * fSeed[nSample1] + fBlend * fSeed[nSample2];
fScaleAcc += fScale;
fNoise += fSample * fScale;
fScale = fScale / fBias;
}
fOutput[x] = fNoise / fScaleAcc;
}
}
void Noise::PerlinNoise2D(int nWidth, int nHeight, float* fSeed, int nOctaves, float fBias, float* fOutput)
{
for (int x = 0; x < nWidth; x++)
{
for (int y = 0; y < nHeight; y++)
{
float fNoise = 0.0f;
float fScaleAcc = 0.0f;
float fScale = 1.0f;
for (int o = 0; o < nOctaves; o++)
{
int nPitch = nWidth >> o;
int nSampleX1 = (x / nPitch) * nPitch;
int nSampleY1 = (y / nPitch) * nPitch;
int nSampleX2 = (nSampleX1 + nPitch) % nWidth;
int nSampleY2 = (nSampleY1 + nPitch) % nWidth;
float fBlendX = (float)(x - nSampleX1) / (float)nPitch;
float fBlendY = (float)(y - nSampleY1) / (float)nPitch;
float fSampleT = coserp(fBlendX, fSeed[nSampleY1 * nWidth + nSampleX1], fSeed[nSampleY1 * nWidth + nSampleX2]);
float fSampleB = coserp(fBlendX, fSeed[nSampleY2 * nWidth + nSampleX1], fSeed[nSampleY2 * nWidth + nSampleX2]);
fScaleAcc += fScale;
float blend2 = (1 - cos(fBlendY * pi)) / 2; // only for coserp
fNoise += (blend2 * (fSampleB - fSampleT) + fSampleT) * fScale;
fScale = fScale / fBias;
}
fOutput[y * nWidth + x] = fNoise / fScaleAcc;
}
}
}
float Noise::lerp(float blend, float sample1, float sample2)
{
return (1.0f - blend) * sample1 + blend * sample2;
}
float Noise::coserp(float blend, float sample1, float sample2)
{
float blend2 = (1.0f - cos(blend * pi)) / 2;
return lerp(blend2, sample1, sample2); //(sample1 * (1.0f - blend2) + sample2 * blend2);
}