Perlin noise question

Started by
4 comments, last by Bacterius 12 years, 4 months ago
Hey. Im looking into perlin noise at the moment, wanting to generate height maps and textures etc. i looked around on the internet and got a few things going (im only at the 1st stage of generating "pure" noise)

I found the following example



int bmp_dim = 256;
Bitmap bmp = new Bitmap(bmp_dim, bmp_dim);
Random r = new Random(); //to create a new image every time
int thisr = r.Next(0, 5000);

for(int i=0; i<bmp_dim; i++) {
for(int j=0; j<bmp_dim; j++) {

float t = Noise2d(j+thisr, i+thisr); //offset creates
//a different image every time
if(t<0) t = -t;
if(t>1) t = 1.0f;
int fin = (int)(t*255.0f); //get in color range
Color col = Color.FromArgb(fin, fin, fin);
bmp.SetPixel(j, i, col);
}
}
[attachment=6154:img.jpg]


Heres the noise function



float Noise2d(int x, int y)
{
int n;
n = x + y * 57;
n = (n<<13) ^ n;
float res = (float)( 1.0 - ( (n * (n * n * 15731 + 789221)
+ 1376312589) & 0x7fffffff ) / 1073741824.0);
return res;
}



I took out the random offset (the variable named thisr) used to make a different noise with each compilation, and just used the x/y of the current place in the loop.

Problem is my noise seems to have patterns in it, it seems to loop. see image below (horizontal pattern)
[attachment=6155:img.jpg]

I had a look into the values and they are not the same, they dont repeat exactly from what i can see, i suspect its just the differences and intervals between them

It may look small but surely it could have adverse effects if it keeps looping, I thought the idea was that its supposed to be random and totally without patterns.

the site i got the example from has a button to generate new noise and this does not occur on the website Link here


is this meant to happen, can i fix it

cheers

Advertisement
This is a better example, same everything but double width and height. its a lot easier to see the patterns

[attachment=6156:img.jpg]
Your Noise2d function is pretty bad. This one is much better and about as fast:
float Noise2d(int x, int y) {
unsigned n = x;
n ^= 0xaacff006;
n *= 0xdc3deee5;
n = (n<<13) | (n>>19);
n += y;
n ^= 0x0a87317a;
n *= 0x38656b5e;
n = (n<<13) | (n>>19);
n += 0xa6c5636a;
n *= 0x7d4677b3;
const float inv_2_31 = 1.0f / 2147483648.0f;
float res = 1.0f - n * inv_2_31;
return res;
}
Ah thanks,

I made the changes but the results are :

[attachment=6178:img.jpg]




Is it meant to be as stretched? I was expecting purely random noise.

I wasn't sure about converting the int x/y to unsigned. i just cast them.




thanks for the help
Hmmm... You are right. My bit mixing is a bit worse than I was hoping for. Adding a few operations at the end seems to get rid of the problem.

float Noise2d(int x, int y) {
unsigned n = x;
n ^= 0xaacff006;
n *= 0xdc3deee5;
n = (n<<13) | (n>>19);
n += y;
n ^= 0x0a87317a;
n *= 0x38656b5e;
n = (n<<13) | (n>>19);
n += 0xa6c5636a;
n *= 0x7d4677b3;
n = (n<<13) | (n>>19);
n ^= 0x57a90182;
n *= 0xe09358ab;
const float inv_2_31 = 1.0f / 2147483648.0f;
float res = 1.0f - n * inv_2_31;
return res;
}


Sorry about that.
Why don't you just use the NextDouble() function of your Random object? Its purpose is to generate "pure" white noise which is just what you're looking for, and it's pretty fast. And it will generate something totally different each time you run your program (although you can make it so it generates the same noise if needed, like for debugging purposes). No point reinventing the wheel! Also, NextDouble() automatically returns floats between zero and one, making your bound checking unnecessary :wink:


int bmp_dim = 256;
Bitmap bmp = new Bitmap(bmp_dim, bmp_dim);
Random r = new Random();

for(int i=0; i<bmp_dim; i++)
for(int j=0; j<bmp_dim; j++)
{
float t = (float)r.NextDouble(); // problem solved
int fin = (int)(t*255.0f); //get in color range
Color col = Color.FromArgb(fin, fin, fin);
bmp.SetPixel(j, i, col);
}

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

This topic is closed to new replies.

Advertisement