I need to generate clouds for my 2d game made with c++ and sdl, and I found this site: http://www.animeimaging.com/asp/PerlinNoise.aspx
I took there code and just converted it over, but when I call PerlinNoise(), all that I get is "Pure" Noise, nothing cloudlike. I can't figure out what the problem is, since I just copied pretty much. Thanks, bye.
Code:
void PerlinNoise()
{
Slock(screen);
for (x = 0; x < 256; x++)
{
for (y = 0; y < 256; y++)
{
float temp = FinalNoise(x, y);
temp = temp*0.5f + 0.5f;
if(temp<0) temp = 0.0f;
if(temp>1) temp = 1.0f;
Uint8 fin = (int)(temp*255.0f);
DrawPixel(screen, x, y, fin, fin , fin);
}
}
Sulock(screen);
}
float Noise(int X, int Y)
{
int n = x + y * 57;
n = (n<<13) ^ n;
return (float)( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff ) / 1073741824.0);
}
float Interpolate(float x, float y, float a)
{
float fac1 = 3*pow(1-a, 2) - 2*pow(1-a,3);
float fac2 = 3*pow(a, 2) - 2*pow(a, 3);
return x*fac1 + y*fac2;
}
float GetValue(float x, float y)
{
int Xint = (int)x;
int Yint = (int)y;
float Xfrac = x - Xint;
float Yfrac = y - Yint;
float x0y0 = Noise(Xint, Yint);
float x1y0 = Noise(Xint+1, Yint);
float x0y1 = Noise(Xint, Yint+1);
float x1y1 = Noise(Xint+1, Yint+1);
float v1 = Interpolate(x0y0, x1y0, Xfrac);
float v2 = Interpolate(x0y1, x1y1, Xfrac);
return Interpolate(v1, v2, Yfrac);
}
float Smooth_Noise(int x, int y)
{
float corners = ( Noise(x-1, y-1) + Noise(x+1, y-1) + Noise(x-1, y+1) + Noise(x+1, y+1) ) / 16.0f;
float sides = ( Noise(x-1, y) +Noise(x+1, y) + Noise(x, y-1) + Noise(x, y+1) ) / 8.0f;
float center = Noise(x, y) / 4.0f;
return corners + sides + center;
}
float FinalNoise(float x, float y)
{
float P = 0.5f;
int octaves = 4;
float total = 0.0f;
int freq = 1;
float pers = 1;
for(int i=0; i<octaves; i++)
{
total += GetValue(x*freq,y*freq) * pers;
freq *= 2;
pers *= P;
}
return total;
}