Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
MT5 bridge not working on MT5 v. 5 build 4160
by EternallyCurious. 04/25/24 20:49
Trading Journey
by howardR. 04/24/24 20:04
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Scripts not found
by juergen_wue. 04/20/24 18:51
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (AndrewAMD, howardR, EternallyCurious, 1 invisible), 770 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Mega_Rod, EternallyCurious, howardR, 11honza11, ccorrea
19048 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Voronoi Heightmap Generator #433972
12/09/13 21:10
12/09/13 21:10
Joined: Aug 2002
Posts: 3,258
Mainz
oliver2s Offline OP
Expert
oliver2s  Offline OP
Expert

Joined: Aug 2002
Posts: 3,258
Mainz
Script to create Voronoi Heightmaps. These are used to create realistic terrain erosion heightmaps (in combination with Perlin Noise or Diamond Square)

Code:
//Voronoi Heightmap Generator
//recommended default values:
// size_ 			= 512
// points_			= 32
var* voronoi_map_tmp_pixels=NULL;
var* voronoi_map_tmp_points=NULL;
///////////////////////////////
BMAP* voronoi_map(double size_,var points_)
{
	//create data
	voronoi_map_tmp_pixels=(var*)sys_malloc(sizeof(var) * size_*size_);
	voronoi_map_tmp_points=(var*)sys_malloc(sizeof(var) * points_*2);
	
	//create random points
	int i; for(i=0;i<points_;i++)
	{
		voronoi_map_tmp_points[i*2+0]=integer(random(size_));//px
		voronoi_map_tmp_points[i*2+1]=integer(random(size_));//py
	}
	
	//fill empty pixels
	var pmax=0;
	int py; for(py=0;py<size_;py++)
	{
		int px; for(px=0;px<size_;px++)
		{
			var fmax=999999;
			var closest_p=fmax;
			var second_closest_p=fmax;
			
			int i; for(i=0;i<points_;i++)
			{
				VECTOR vec_from_,vec_to_;
				vec_set(vec_from_,vector(px,py,0));
				vec_set(vec_to_,vector(voronoi_map_tmp_points[i*2+0],voronoi_map_tmp_points[i*2+1],0));
				var distance=vec_dist(vec_from_,vec_to_);
				
				if(distance<closest_p)
				{
					second_closest_p=closest_p;
					closest_p=distance;
				}
				else if(distance<second_closest_p)
				{
					second_closest_p=distance;
				}
			}
			
			var c1=1;
			var c2=-1;
			var d1=255-closest_p;
			var d2=255-second_closest_p;
			
			voronoi_map_tmp_pixels[px*size_+py]=c1*d1 + c2*d2;
			
			//find brightest pixel to prepare normalizing
			if(voronoi_map_tmp_pixels[px*size_+py]>pmax){pmax=voronoi_map_tmp_pixels[px*size_+py];}
		}
	}
	
	//normalize heightmap (range 0-255)
	if(pmax!=0)
	{
		var factor=255/pmax;
		int py; for(py=0;py<size_;py++)
		{
			int px; for(px=0;px<size_;px++)
			{
				voronoi_map_tmp_pixels[px*size_+py]*=factor;
				voronoi_map_tmp_pixels[px*size_+py]=clamp(integer(voronoi_map_tmp_pixels[px*size_+py]),0,255);
			}
		}
	}

	//create bmap
	BMAP* bmp_tmp=bmap_createblack(size_,size_,24);
	
	//fill bmap
	int py; for(py=0;py<size_;py++)
	{
		int px; for(px=0;px<size_;px++)
		{
			//get heightmap color
			var grey=voronoi_map_tmp_pixels[px*size_+py];
			
			VECTOR bmp_color_;
			bmp_color_.x=grey;
			bmp_color_.y=grey;
			bmp_color_.z=grey;
			
			//add to heightmap
			var format=bmap_lock(bmp_tmp,0);
			var pixel=pixel_for_vec(bmp_color_,100,format);
			pixel_to_bmap(bmp_tmp, px, py, pixel);
			bmap_unlock(bmp_tmp);
		}
	}
	
	//free pixels and bmaps
	sys_free(voronoi_map_tmp_pixels);
	sys_free(voronoi_map_tmp_points);
	
	return bmp_tmp;
}



Example:
Code:
void main()
{
	video_mode=9;
	
	wait(1);
	
	BMAP* bmp=voronoi_map(512,32);
	
	while(1)
	{
		DEBUG_BMAP(bmp,0,1);
		
		wait(1);
	}
}



Further reading:
http://oddlabs.com/download/terrain_generation.pdf


Last edited by oliver2s; 01/15/14 12:45. Reason: Code Update / Bug Fix
Re: Voronoi Heightmap Generator [Re: oliver2s] #433974
12/09/13 22:09
12/09/13 22:09
Joined: Mar 2011
Posts: 3,150
Budapest
sivan Offline
Expert
sivan  Offline
Expert

Joined: Mar 2011
Posts: 3,150
Budapest
many thanks! it seems to be best to start with the article.


Free world editor for 3D Gamestudio: MapBuilder Editor
Re: Voronoi Heightmap Generator [Re: sivan] #433988
12/10/13 09:22
12/10/13 09:22
Joined: Aug 2002
Posts: 3,258
Mainz
oliver2s Offline OP
Expert
oliver2s  Offline OP
Expert

Joined: Aug 2002
Posts: 3,258
Mainz
Yes, today I will try to go further with the article and erosion heightmaps.

Re: Voronoi Heightmap Generator [Re: oliver2s] #434520
12/20/13 07:54
12/20/13 07:54
Joined: Jan 2004
Posts: 3,023
The Netherlands
Helghast Offline
Expert
Helghast  Offline
Expert

Joined: Jan 2004
Posts: 3,023
The Netherlands
This is funny, I actually had been working on exactly this over the last week too! (not in GS though).
I ended up creating fBM and layering that over the voronoi heightmaps (I used worley noise though instead) to get the erosion effect.
That link you posted looks way better though, will definitely attempt that next!

Link to fBM algorithm: http://code.google.com/p/fractalterraingeneration/wiki/Fractional_Brownian_Motion


Formerly known as dennis_fantasy
Portfolio - http://www.designorhea.com/
Project - http://randomchance.cherrygames.org/
Re: Voronoi Heightmap Generator [Re: Helghast] #434537
12/20/13 15:40
12/20/13 15:40
Joined: Aug 2002
Posts: 3,258
Mainz
oliver2s Offline OP
Expert
oliver2s  Offline OP
Expert

Joined: Aug 2002
Posts: 3,258
Mainz
Never heard of fBM. Thanks for information and link.

Re: Voronoi Heightmap Generator [Re: oliver2s] #435851
01/15/14 12:47
01/15/14 12:47
Joined: Aug 2002
Posts: 3,258
Mainz
oliver2s Offline OP
Expert
oliver2s  Offline OP
Expert

Joined: Aug 2002
Posts: 3,258
Mainz
I've updated the code because of a bug: if there where too many height points, the chance that the script crashes was high, because of division by zero. Fixed it.


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1