DarkCoder once wrote an awesome perlin noise function:
paste image GenerateNoise(timer(),512,6,1),0,0
wait key
end
`Perlin Noise Courtesy of Dark Coder ;) ()
Function GenerateNoise( Seed, startingRes, totalOctaves, Sampling )
Randomize Seed
If startingRes > 512 Then startingRes = 512
If totalOctaves > 8 Then totalOctaves = 8
// Generate noise for all octaves
for octaveID = 1 to totalOctaves
memblockID = octaveID
octaveRes = startingRes / 2 ^ (octaveID - 1)
Make Memblock memblockID, octaveRes * octaveRes * 4 // A single float
for x = 0 to octaveRes - 1
for y = 0 to octaveRes - 1
Write Memblock Float memblockID, ( x + y * octaveRes ) * 4, ( Rnd(2000) - 1000 ) * 0.001
next
next
next
// add all layers together
memblockID = totalOctaves + 1
Make Memblock memblockID, 12 + startingRes * startingRes * 4
Write Memblock DWord memblockID, 0, startingRes
Write Memblock DWord memblockID, 4, startingRes
Write Memblock DWord memblockID, 8, 32
minHeight# = -1.9
maxHeight# = 1.9
minInv# = -minHeight#
heightRange# = maxHeight# - minHeight#
_255DivRange# = 255.0 / heightRange#
sResReciprocal# = 1.0 / (startingRes)
for x = 0 to startingRes - 1
for y = 0 to startingRes - 1
x# = x * sResReciprocal#
y# = y * sResReciprocal#
height# = 0.0
Strength# = 1.0
`height# = Memblock Float( 1, ( x + y * startingRes ) * 4 )
for OctaveID = totalOctaves to 1 step -1
`for OctaveID = 7 to 7 step -1
octaveRes = startingRes / 2 ^ (octaveID - 1)
// Store the float pixel we are over on the current octave
memblockX# = x# * octaveRes
memblockY# = y# * octaveRes
// Store the int version for pixel sampling
memblockX = memblockX#
memblockY = memblockY#
// Get the local offset
memblockX# = memblockX# mod 1.0
memblockY# = memblockY# mod 1.0
If Sampling = 1
`BiCubic Sampling
sample1# = Sample( OctaveID, memblockX - 1, memblockY - 1, octaveRes )
sample2# = Sample( OctaveID, memblockX , memblockY - 1, octaveRes )
sample3# = Sample( OctaveID, memblockX + 1, memblockY - 1, octaveRes )
sample4# = Sample( OctaveID, memblockX + 2, memblockY - 1, octaveRes )
mHeight1# = CubicInterpolate( sample1#, sample2#, sample3#, sample4#, memblockX# )
sample1# = Sample( OctaveID, memblockX - 1, memblockY , octaveRes )
sample2# = Sample( OctaveID, memblockX , memblockY , octaveRes )
sample3# = Sample( OctaveID, memblockX + 1, memblockY , octaveRes )
sample4# = Sample( OctaveID, memblockX + 2, memblockY , octaveRes )
mHeight2# = CubicInterpolate( sample1#, sample2#, sample3#, sample4#, memblockX# )
sample1# = Sample( OctaveID, memblockX - 1, memblockY + 1, octaveRes )
sample2# = Sample( OctaveID, memblockX , memblockY + 1, octaveRes )
sample3# = Sample( OctaveID, memblockX + 1, memblockY + 1, octaveRes )
sample4# = Sample( OctaveID, memblockX + 2, memblockY + 1, octaveRes )
mHeight3# = CubicInterpolate( sample1#, sample2#, sample3#, sample4#, memblockX# )
sample1# = Sample( OctaveID, memblockX - 1, memblockY + 2, octaveRes )
sample2# = Sample( OctaveID, memblockX , memblockY + 2, octaveRes )
sample3# = Sample( OctaveID, memblockX + 1, memblockY + 2, octaveRes )
sample4# = Sample( OctaveID, memblockX + 2, memblockY + 2, octaveRes )
mHeight4# = CubicInterpolate( sample1#, sample2#, sample3#, sample4#, memblockX# )
mHeight# = CubicInterpolate( mHeight1#, mHeight2#, mHeight3#, mHeight4#, memblockY# )
Else
`BiLinear Sampling
sample1# = Sample( OctaveID, memblockX , memblockY, octaveRes )
sample2# = Sample( OctaveID, memblockX + 1, memblockY, octaveRes )
mHeightX# = Linear_Interpolate( sample1#, sample2#, memblockX# )
sample1# = Sample( OctaveID, memblockX , memblockY + 1, octaveRes)
sample2# = Sample( OctaveID, memblockX + 1, memblockY + 1, octaveRes)
mHeightY# = Linear_Interpolate( sample1#, sample2#, memblockX# )
mHeight# = Linear_Interpolate( mHeightX#, mHeightY#, memblockY# )
EndIf
height# = height# + mHeight# * Strength#
Strength# = Strength# * 0.5
next
height = (height# + minInv#) * _255DivRange#
if height > 255 then height = 255
if height < 0 then height = 0
Write Memblock Byte memblockID, 12 + ( x + y * startingRes ) * 4 , height // B
Write Memblock Byte memblockID, 12 + ( x + y * startingRes ) * 4 + 1, height // G
Write Memblock Byte memblockID, 12 + ( x + y * startingRes ) * 4 + 2, height // R
Write Memblock Byte memblockID, 12 + ( x + y * startingRes ) * 4 + 3, 255 // A
next
next
// Create image
Img = GetBlankImgNo()
Make Image From Memblock Img, memblockID
For octaveID = 1 to totalOctaves
Delete Memblock octaveID
Next octaveID
Delete Memblock totalOctaves + 1
Cls
EndFunction Img
Function Sample( memblockID, x, y, memblockRes )
if x < 0 then x = x + memblockRes
if y < 0 then y = y + memblockRes
if x > memblockRes-1 then x = x - memblockRes
if y > memblockRes-1 then y = y - memblockRes
returnValue# = Memblock Float( memblockID, ( x + y * memblockRes ) * 4 )
Endfunction returnValue#
Function Linear_Interpolate( a as float, b as float, x as float )
returnValue# = a * (1.0 - x) + b * x
Endfunction returnValue#
Function CubicInterpolate( aMinOne as float, a as float, b as float, bAddOne as float, acrossAB as float )
P as float
Q as float
R as float
S as float
P = (bAddOne - b) - (aMinOne - a)
Q = (aMinOne - a) - P
R = b - aMinOne
S = a
returnValue# = P * acrossAB ^ 3 + Q * acrossAB ^ 2 + R * acrossAB + S
EndFunction returnValue#
Function GetBlankImgNo()
For Num = 1 To 65500
If Image Exist( Num ) = 0
ExitFunction Num
EndIf
Next Num
EndFunction 0
What that function does, is first create a bunch of random memblocks, representing a grid of a certain resolution. For example, if startingres=512, and octaves=8, that would create grids with random numbers in each spot, of resolution: 512x512, 256x256, 128x128, 64x64, 32x32, 16x16, 8x8, and 4x4. Then, the program adds all of those together. Thats where the interpolation comes in - for example, the 4x4 grid has to be interpolated up to 512x512. Then, you add all the interpolated values together, to get the final result.
What you're doing... doesn't look like its that. Not that i thoroughly read through it (and I can't compile it :\)