Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by howardR. 04/28/24 09:55
basik85278
by basik85278. 04/28/24 08:56
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Help with plotting multiple ZigZag
by M_D. 04/26/24 20:03
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
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
0 registered members (), 728 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Perturbation Image Filter #433995
12/10/13 14:10
12/10/13 14:10
Joined: Aug 2002
Posts: 3,258
Mainz
oliver2s Offline OP
Expert
oliver2s  Offline OP
Expert

Joined: Aug 2002
Posts: 3,258
Mainz
This code manipulates an image with a Perturbation Filter. It's used to create more realistic heightmaps.



Code:
var linear_interpolate(var a, var b, var x)
{
	return (a*(1-x) + b*x);
}

//Perturbation Filter
//recommended default values:
//magnitude_ 		= 0.3
///////////////////////////////
void bmap_perturbation_filter(BMAP* bmap_,var magnitude_)
{
	var size_x_=bmap_width(bmap_);
	var size_y_=bmap_height(bmap_);
		
	var format=bmap_format(bmap_);
	BMAP* bmap_tmp=bmap_createblack(size_x_,size_y_,format);

	int py; for(py=0;py<size_y_;py++)
	{
		int px; for(px=0;px<size_x_;px++)
		{
			COLOR color_;
			var format=bmap_lock(bmap_,0);var alpha_;
			var pixel=pixel_for_bmap(bmap_, px, py);
			pixel_to_vec(color_,alpha_,format,pixel);
			bmap_unlock(bmap_);
				
			var grey=clamp(color_.red*0.21 + color_.green*0.71 + color_.blue*0.07, 0, 255);
			var p = (magnitude_*(grey-128))/255;
				
			var x_coord = px + integer(size_x_*p);
			var x_coord_lo = x_coord;
			var x_coord_hi = x_coord_lo + 1;
			var x_frac = x_coord - x_coord_lo;
			var y_coord = py + size_y_*p;
			var y_coord_lo = y_coord;
			var y_coord_hi = y_coord_lo + 1;
			var y_frac = y_coord - y_coord_lo;
			x_coord_lo=clamp(x_coord_lo,0,size_x_-1);
			x_coord_hi=clamp(x_coord_hi,0,size_x_-1);
			y_coord_lo=clamp(y_coord_lo,0,size_y_-1);
			y_coord_hi=clamp(y_coord_hi,0,size_y_-1);
				
			COLOR v1,v2,v3,v4;
			var a1,a2,a3,a4;
				
			var format=bmap_lock(bmap_,0);
			var pixel=pixel_for_bmap(bmap_,x_coord_lo,y_coord_lo);pixel_to_vec(v1,a1,format,pixel);
			var pixel=pixel_for_bmap(bmap_,x_coord_hi,y_coord_lo);pixel_to_vec(v2,a2,format,pixel);
			var pixel=pixel_for_bmap(bmap_,x_coord_lo,y_coord_hi);pixel_to_vec(v3,a3,format,pixel);
			var pixel=pixel_for_bmap(bmap_,x_coord_hi,y_coord_hi);pixel_to_vec(v4,a4,format,pixel);
			bmap_unlock(bmap_);
				
			var val1a = linear_interpolate(v1.red,v2.red,x_frac);
			var val2a = linear_interpolate(v3.red,v4.red,x_frac);
			var val1b = linear_interpolate(v1.green,v2.green,x_frac);
			var val2b = linear_interpolate(v3.green,v4.green,x_frac);
			var val1c = linear_interpolate(v1.blue,v2.blue,x_frac);
			var val2c = linear_interpolate(v3.blue,v4.blue,x_frac);
			var val1d = linear_interpolate(a1,a2,x_frac);
			var val2d = linear_interpolate(a3,a4,x_frac);
				
			color_.red=linear_interpolate(val1a,val2a,y_frac);
			color_.green=linear_interpolate(val1b,val2b,y_frac);
			color_.blue=linear_interpolate(val1c,val2c,y_frac);
			alpha_=linear_interpolate(val1d,val2d,y_frac);
				
			var format=bmap_lock(bmap_tmp,0);
			var pixel=pixel_for_vec(color_,alpha_,format);
			pixel_to_bmap(bmap_tmp, px, py, pixel);
			bmap_unlock(bmap_tmp);
		}
	}
		
	bmap_blit(bmap_,bmap_tmp,NULL,NULL);
	bmap_purge(bmap_tmp);bmap_remove(bmap_tmp);
}


Last edited by oliver2s; 12/10/13 14:19.
Re: Perturbation Image Filter [Re: oliver2s] #433996
12/10/13 14:15
12/10/13 14:15
Joined: Mar 2011
Posts: 3,150
Budapest
sivan Offline
Expert
sivan  Offline
Expert

Joined: Mar 2011
Posts: 3,150
Budapest
thanks again! altogether you made a nice code collection what I really need.


Free world editor for 3D Gamestudio: MapBuilder Editor
Re: Perturbation Image Filter [Re: sivan] #433997
12/10/13 14:17
12/10/13 14:17
Joined: Aug 2002
Posts: 3,258
Mainz
oliver2s Offline OP
Expert
oliver2s  Offline OP
Expert

Joined: Aug 2002
Posts: 3,258
Mainz
Next thing is to write the erosion algorithm laugh

EDIT: just saw I did forget to include the function "linear_interpolate", just added it to the code above.

Last edited by oliver2s; 12/10/13 14:20.
Re: Perturbation Image Filter [Re: oliver2s] #433998
12/10/13 14:27
12/10/13 14:27
Joined: Mar 2011
Posts: 3,150
Budapest
sivan Offline
Expert
sivan  Offline
Expert

Joined: Mar 2011
Posts: 3,150
Budapest
is a new contribution coming tomorrow? laugh


Free world editor for 3D Gamestudio: MapBuilder Editor
Re: Perturbation Image Filter [Re: sivan] #433999
12/10/13 14:32
12/10/13 14:32
Joined: Aug 2002
Posts: 3,258
Mainz
oliver2s Offline OP
Expert
oliver2s  Offline OP
Expert

Joined: Aug 2002
Posts: 3,258
Mainz
Yes, I think so laugh

Re: Perturbation Image Filter [Re: sivan] #434000
12/10/13 14:39
12/10/13 14:39
Joined: Dec 2013
Posts: 3
C
cream_soda Offline
Guest
cream_soda  Offline
Guest
C

Joined: Dec 2013
Posts: 3
Hello everyone. On the website under "Old version" you can free download A7 + patch. So I wonder if it has any restrictions (ability to compile to exe, for example) and how well does it work on windows 7? Waiting for your reply.

Re: Perturbation Image Filter [Re: cream_soda] #434001
12/10/13 14:42
12/10/13 14:42
Joined: Aug 2002
Posts: 3,258
Mainz
oliver2s Offline OP
Expert
oliver2s  Offline OP
Expert

Joined: Aug 2002
Posts: 3,258
Mainz
Please open your own thread in "The Past" forum: http://www.opserver.de/ubb7/ubbthreads.php?ubb=postlist&Board=33&page=1


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