Creating a Terrain Mesh Editor

Blitz3D Forums/Blitz3D Programming/Creating a Terrain Mesh Editor

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

This is a bit naff and knocked up quickly but may give you one idea to generate landscapes. Also, have a look at SSwifts Perlin Noise heightmap generator - it's fractal based as I remember. Also, try googling for more info - "random terrains".

Graphics 640,480,32,1 

Const Size = 256
Const Maxheight=255
Dim Terrain#( Size-1 , Size-1 )

MyTerrain = TERRAINcreate( 427, 500, 100 )

;show it
DrawImage MyTerrain , 0 , 0
Flip
MouseWait

;===========================
;===========================
;===========================

Function TERRAINcreate( Seed , Passes ,SmallNo )
	
	;reset values to 0
	Dim Terrain( Size, Size )
	
	;set random seed
	SeedRnd Seed
			
	;generate terrain
	For Parts =1 To Passes
	
		StartX = Rand( 0 , size )
		StartZ = Rand( 0 , size )
		SlopeType = Rand( 0 , 1 )
		
		;Smaller Bits
		If Parts > ( Passes - SmallNo ) 
			StartY# = Rnd( 4, 8 )
			Radius# = Rand( 4,8 )
		Else
			StartY# = Rnd(4,32)
			Radius# = Rand(4,32)
		EndIf
			
		For z=-radius To radius
			For x=-radius To radius
				Distance# = Sqr( x*x + z*z )
				If Distance < Radius
					If SlopeType = 0 py#=Cos( Distance / Radius * 90) * StartY * 2.0
					If SlopeType = 1 py#=( 1.0 - Sin( ( Distance / Radius ) *90 ) ) * StartY *3.0
					px = WRAP( StartX + X , Size)
					pz = WRAP( StartZ + Z, Size)
					NewHeight# = LIMIT( terrain( px, pz) + py , 0, 255 ) 
					Terrain( px , pz ) = NewHeight
				EndIf
			Next
		Next
	Next
		
	;make heightmap
	Heightmap = CreateImage( Size , Size )
	SetBuffer ImageBuffer( Heightmap )
	Color 0,0,0:Rect 0,0,size,size,1
	For z =0 To size -1
		For x =0 To size -1
			c = Terrain( x , z )
			Color c,c,c
			Plot x,z
		Next
	Next
	SetBuffer BackBuffer()
	
	Return Heightmap
						
End Function

;===========================
;===========================
;===========================

Function WRAP#( q# , hi# , lo#=0 )
	
	If q < lo Then q = hi + (q-lo)
	If q >= hi Then q = lo + (q-hi)
	Return q
	
End Function

;===========================
;===========================
;===========================

Function LIMIT#( q# , lo# , hi# )

	If q < lo q = lo
	If q > hi q = hi
	Return q
	
End Function