Qbasicnews.com

Full Version: Best algorithm for a 2D Perlin Noise ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Antoni Gual saying to Jark "it's really fast"... Must be a kind of Spanish virus that crossed the Pyrenean moutains :wink:
Antoni, this virus is dangerous...

Code:
DIM Cloud%(129, 129)

RANDOMIZE TIMER
T0 = TIMER

' Init the corners
Cloud%(1, 1) = 128
Cloud%(1, 129) = 128
Cloud%(129, 1) = 128
Cloud%(129, 129) = 128

' Init the edges
FOR Rank% = 1 TO 7

Grid% = 2 ^ (7 - Rank% + 1)
nStep% = 2 ^ (Rank% - 1) + 1

FOR kx% = 1 TO nStep% - 1
x% = (kx% - 1) * Grid% + 1: y% = 1
Alt% = (Cloud%(x%, y%) + Cloud%(x% + Grid%, y%)) / 2
zNew% = INT(Alt% * (1 + (RND - .5) / (2 ^ Rank%)))
Cloud%(x% + Grid% / 2, 1) = zNew%
Cloud%(x% + Grid% / 2, 129) = zNew%
NEXT kx%

FOR ky% = 1 TO nStep% - 1
x% = 1: y% = (ky% - 1) * Grid% + 1
Alt% = (Cloud%(x%, y%) + Cloud%(x%, y% + Grid%)) / 2
zNew% = INT(Alt% * (1 + (RND - .5) / (2 ^ Rank%)))
Cloud%(1, y% + Grid% / 2) = zNew%
Cloud%(129, y% + Grid% / 2) = zNew%
NEXT ky%

NEXT Rank%


' Fill the clouds
FOR Rank% = 1 TO 7

Grid% = 2 ^ (7 - Rank% + 1)
nStep% = 2 ^ (Rank% - 1) + 1

FOR kx% = 1 TO nStep% - 1
x% = (kx% - 1) * Grid% + 1
FOR ky% = 1 TO nStep% - 1
y% = (ky% - 1) * Grid% + 1

Alt% = (Cloud%(x%, y%) + Cloud%(x% + Grid%, y%) + Cloud%(x%, y% + Grid%) + Cloud%(x% + Grid%, y% + Grid%)) / 4
Cloud%(x% + Grid% / 2, y% + Grid% / 2) = INT(Alt% * (1 + (RND - .5) / (2 ^ Rank%)))
Alt% = (Cloud%(x%, y%) + Cloud%(x% + Grid%, y%)) / 2
IF y% <> 1 THEN Cloud%(x% + Grid% / 2, y%) = INT(Alt% * (1 + (RND - .5) / (2 ^ Rank%)))
Alt% = (Cloud%(x%, y%) + Cloud%(x%, y% + Grid%)) / 2
IF x% <> 1 THEN Cloud%(x%, y% + Grid% / 2) = INT(Alt% * (1 + (RND - .5) / (2 ^ Rank%)))
Alt% = (Cloud%(x% + Grid%, y%) + Cloud%(x% + Grid%, y% + Grid%)) / 2
IF (x% + Grid%) <> 129 THEN Cloud%(x% + Grid%, y% + Grid% / 2) = INT(Alt% * (1 + (RND - .5) / (2 ^ Rank%)))
Alt% = (Cloud%(x%, y% + Grid%) + Cloud%(x% + Grid%, y% + Grid%)) / 2
IF (y% + Grid%) <> 129 THEN Cloud%(x% + Grid% / 2, y% + Grid%) = INT(Alt% * (1 + (RND - .5) / (2 ^ Rank%)))

NEXT ky%
NEXT kx%

NEXT Rank%
dt = TIMER - T0
CLS
PRINT dt
You could also change all
Code:
result= INT(anything/something)
to
Code:
result=anything\something

Integer division is faster...

Also you could precalculate an array of powers of 2... Big Grin
It's only a sphere, and I don't control everything yet, but that's a beginning...

[Image: Sphere.jpg]
I seriously need to develop an regular antialiasing routine... my smoothering filter only handles black spots.

[Image: Pcube.jpg]
:o :o :o :o :o :o :o :o :o :o :o :o :o
*jaw drops!!!!
Pages: 1 2