Archived

This topic is now archived and is closed to further replies.

Andos

2D Perlin noise in one function

Recommended Posts

Andos    124
I have read a dozens of articles about perlin noise and i can get some fine results in 1D. When it comes to 2D i can''t follow it anymore. (I looked at http://freespace.virgin.net/hugo.elias/models/m_perlin.htm) I made lot''s of different approaches for a decent function that could do it ) I was wondering of anyone simplified it into one function that i could look at to compare?

Share this post


Link to post
Share on other sites
ToohrVyk    1596
The good news is, I simplified it into a couple functions...
The bad news is, it''s a curryfied O''Caml function...

I''ll post it here anyways:


(*-------*)

let add_noise buffer amplitude =

let w = width buffer
and h = height buffer in

for i = 0 to w - 1 do

for j = 0 to h - 1 do

buffer.(i).(j) <- buffer.(i).(j) +. amplitude *. ( 1. -. ( Random.float 2.) );

done;
done;

buffer
(*-------*)

let rec generate filter x y amplitude falloff =

if ( x <= 1 ) && ( y <= 1 ) then

add_noise ( create 1 1 0. ) (amplitude *. falloff)

else

add_noise (
resize filter (
generate filter ((x+1)/2) ((y+1)/2) (amplitude *.falloff) falloff ) x y )
(amplitude *.falloff)
(*-------*)
let resize filter buffer w h =

let result = Array.make_matrix w h ( buffer.(0).(0) ) in

for i = 0 to w - 1 do

for j = 0 to h - 1 do

let x = float_of_int(i) /. float_of_int ( w - 1 )
and y = float_of_int(j) /. float_of_int ( h - 1 ) in

result.(i).(j) <- filter buffer x y;

done;

done;

result


Then, call "generate" with, as arguments, your filter (linear, nearest-point, or whatever) function, the width and height, the amplitude, and amplitude reduction.

ToohrVyk

Share this post


Link to post
Share on other sites