LoadSegTerrain Function - For everyone.

Blitz3D Forums/Blitz3D Programming/LoadSegTerrain Function - For everyone.

Chroma(Posted 2006) [#1]
I'm trying to get a function written that will create a segmented terrain from a heightmap. Maybe there are a few out there that can help and would need something like this. I think a lot of people would benefit from it.

The idea is to build a terrain from a height map and then break it up into a specified number of segments. For example, you load a 128x128 heightmap and specify segments=4. The function would load the heightmap and build a 4x4 terrain with each segment being a separate object and be 32x32. The terrain segments would not be rendered when behind the camera so it would be an easy way to save rendering time. The terrains would also be automatically positioned together and then centered and parented to a pivot at 0,0,0.

I'll start this off. I have some working code but I don't want to influence this with it because you guys may come up with a more highly efficent method.

Function LoadSegTerrain(hmap$,tmap$,lmap$,segments=4)

End Function



Chroma(Posted 2006) [#2]
Assuming your heightmap will be 32, 64, 128, 256, etc. We can just get the imagewidth and use that number as a base for the width and length of quads needed for the terrain. Then take the segments number and figure out how many segments there will be and the dimension in quads of each segment.

Function LoadSegTerrain (hmap$,tmap$,lmap$,segments=4)

     h_map = LoadImage(hmap$)
     ; error trapping
     If Not h_map Return 0
     Local h_width = ImageWidth(h_map)
     Local h_height = ImageHeight(h_map)
     If h_width <> h_height Return 0
     Local seg_quads = ter_width / segments 

End Function



Chroma(Posted 2006) [#3]
I'm using a dim var to save the info atm. A type would probably be better in the future.

; make a var to hold the terrain segment info in
; 20,20,0 are where the actual mesh is stored
; 20,20,1 is the x position
; 20,20,2 is the y position
; 20,20,3 is the z position
Dim Ter_Segment(10,10,2)


; Height Map Loader
Function LoadSegTerrain (hmap$,tmap$,lmap$,seg=4)

    h_map = LoadImage(hmap$)
    ; error trapping
    If Not h_map RuntimeError hmap$+" does not exist."

    Local h_width = ImageWidth(h_map)
    Local h_height = ImageHeight(h_map)
	; error trapping
    If h_width <> h_height RuntimeError hmap$+" is not perfectly square."
    

	; calc the total segments
	Local seg_total = seg * seg
	
	; calc the quad dimensions for each segment 
	Local seg_quads = ter_width / segments 
	
	
	; now we build each segment individually
	For loop = 1 To seg_total
	
		; create the mesh and surface
		mesh = CreateMesh()
		surf = CreateSurface(mesh)
		; vertices
		For y = 0 To seg_quads
			For x = 0 To seg_quads
				AddVertex surf,x,0,y-1,0,x,-y
				VertexTexCoords surf,x+((seg_quads+1)*y),x,-y
			Next
		Next
		; triangles	
		For y = 0 To seg_quads-1
			For x = 0 To seg_quads-1
				v=x+s
				AddTriangle surf,v,v+seg_quads+1,v+seg_quads+2
				AddTriangle surf,v,v+seg_quads+2,v+1
			Next
		s=s+seg_quads+1
		Next
		
		; now to store the terrain segment and calc where it will be placed
		; To Be Continued....
	Next

End Function



Chroma(Posted 2006) [#4]
Latest incarnation. It loads up a heighmap and divides it up into the segments ok now. Placement works but needs to be redone.




Chroma(Posted 2006) [#5]
Well there's a time when you don't want 1 quad per pixel. So I gotta figure out how to change the detail level of the segment.


Chroma(Posted 2006) [#6]
New version. Parents other segments to the first segment. Now you can scale the terrain normally.




jfk EO-11110(Posted 2006) [#7]
(why something other than 1 quad/texel?)

This may be very useful for terrains, although I rarely use terrains, except for water.

One problem of terrein segments is they don't tile seamlessy because they use automatic normals.

Personally I am using FLE in the open source version 3 to create terrain-meshes with alphablended vertices. Since they are huge I added a segmentation that will split the mesh in clusters after loading. I simply clone the vertex information , including the normals, therefor the segmentation is not visible, but allows fustrum culling and skip rendering things that out of the camera range.

Nevertheless, the limitation is still ~32k Tris per surface, so a solution that is SAVING segments would be welcome.

Let me know if you want the source.


Chroma(Posted 2006) [#8]
Sure the source would be great. It would let me get past this terrain thing and get on with other stuff. My email is current in my profile.

Thx jfk.


Red Ocktober(Posted 2006) [#9]
this is interesting... i'm working on terrains in my sim now...

jfk, why do you not use terrains much... i use terrains in my lil OOP engine for land masses, which in turn is used in the sub sim...

i use em mainly for the built in functions in Blitz, which allows me to have my actors fall and land on the terrain quite easily...

i also found terrians conducive to making an easy collision system which not only allows the sub to 'bottom' out on the terrain, but also allows me to make a simple collision detection functions for the sub on the surface...

i use something else for visually representing the water (ocean)...


as far as Chroma's situation goes, and i may be missing something here... but why split up an exisiting terrain into smaller segments... why not not make smaller, fixed sized terrain segments from your heightmap in the first place... then, have functions that automatially position em in a treadmill like grid...

1 2 3
4 5 6
7 8 9

represented by an array...

the default terrain could be a relatively flat and level segment, and would automatically be used if a unique segment is not found... using a treadmill like algorithm, the camera would always be on terrain 5 at all times...

movement past the arbitrary boundaries would adjust each segment accordingly, putting a new terrain segment in position 5, and surrounding it with the other terrain segs... seamless to the player...

the other segments would not be loaded or rendered, only when they come into range of the grid, which should be sized to be within the visual range of the view...

--Mike


jfk EO-11110(Posted 2006) [#10]
Red. I don't use terrains because of their limited capabilities in texturing, as well as of their "pop-in" LOD behaviour. Tho, it's still pretty useful for water.


Red Ocktober(Posted 2006) [#11]
ahhh... texturing... i see now, thx...

--Mike


jfk EO-11110(Posted 2006) [#12]
Chroma: here it is: far from finished or perfect, tho already great fun:
http://www.melog.ch/dl/fle_gpl_2006.zip


Chroma(Posted 2006) [#13]
Red, you should read the code. I'm already doing exactly what you recommended. Loading a heightmap and creating terrain segments from it and then positioning them in the correct spots...