Integer Noise Functions in B3D
Blitz3D Forums/Blitz3D Programming/Integer Noise Functions in B3D
| ||
Integer noise functions don't seem to work in B3D. I took this C++ code: double IntegerNoise (int n) { n = (n >> 13) ^ n; int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; return 1.0 - ((double)nn / 1073741824.0); } And converted it to Basic and it gives me a 1.0 for (0,0) and a 0.0 for everything else. The resource I was looking at (http://libnoise.sourceforge.net/noisegen/index.html) said that the large numbers were prime numbers, and could be modified as long as they remained prime. I did so, and still got the same results. I have a feeling this might have something to do with Blitz not have sufficient floating point precision...? Any help is appreciated! |
| ||
I'm not sure, but shouldn't it be: Function Noise#(x#,y#) instead of: Function Noise(x#,y#) ? EDIT: Oops, you wrote "Integer noise functions don't seem to work in B3D.", so forget my suggestion :-) Last edited 2011 Last edited 2011 |
| ||
It works without double precision, you just made a couple of errors in your code. - the function returns a float between -1 and 1, so you need to declare that the function returns a float. - in C, ^ means Xor, not exponentiation - Because the value returned is between -1 and 1, you should take Abs of it to get something that's between 0 and 1. Negative values for colours lead to oddness. Here's the fixed code: Graphics 256,256,0,2 SetBuffer BackBuffer() Function Noise#(x#,y#) Local n% = Int(x)+Int(y)*57 n = (n Shl 13) Xor n Local nn% = (n*(n*n*60493+19990303)+1376312589) And $7fffffff Return 1.0-(Float(nn)/1073741824.0) End Function For y = 0 To 255 For x = 0 To 255 i# = Abs(Noise(x,y)) Color i*255,i*255,i*255 Plot x,y Next Next Flip WaitKey() End |
| ||
Oh, well pffft! Thanks! The pattern appears to repeat, though. Is there a reason for this or a way to fix it? |
| ||
Not really in the spirit of the original but the simplest way to keep it from repeating within the sample is to use the x or y as an offset i.e. change: n = (n Shl 13) Xor n to something like n = ((n Shl 13) Xor n) + x or just use the current millisecs value to replace any of the longer numbers even :S Last edited 2011 |
| ||
OK, and there's no way to change the sample size? EDIT: Never mind, I figured it out - change the *57 to any number and that is the amount of pixels before it repeats the pattern again. EDIT 2: Also, using just "x" as the offset won't work correctly because non-whole numbers give strange results. This should be changed to Floor(x), and the same goes for the Int(x)'s. Last edited 2011 |