LoadSegTerrain Function - For everyone.

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

New version. Parents other segments to the first segment. Now you can scale the terrain normally.

Graphics3D 640,480,32,2
SetBuffer BackBuffer()

width = GraphicsWidth()
height = GraphicsHeight()

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

camera = CreateCamera()
CameraRange camera,1,10000
PositionEntity camera,0,500,0

light = CreateLight()

terrain = LoadSegTerrain("hmap2.bmp",2)
ScaleEntity terrain,20,1,20


MoveMouse width/2,height/2

While Not KeyHit(1)
Cls
mx=MouseX():my=MouseY()

If KeyHit(17) w=1-w:WireFrame w

	mxs#=mxs-MouseXSpeed()*0.15
	mys#=mys+MouseYSpeed()*0.3
	MoveMouse mx,my
	mxd#=curvevalue(mxd,mxs,10)
	myd#=curvevalue(myd,mys,10)
	If myd=>90 myd=90:mys=90
	If myd=<-90 myd=-90:mys=-90
	RotateEntity camera,myd,mxd,0
	
	If KeyDown(200) MoveEntity camera,0,0,0.25
	If KeyDown(208) MoveEntity camera,0,0,-0.25
	If KeyDown(203) MoveEntity camera,-0.15,0,0
	If KeyDown(205) MoveEntity camera,0.15,0,0

	MoveMouse width/2,height/2

UpdateWorld
RenderWorld

Text 5,5,"Tris: "+TrisRendered()

Flip False
Wend
End

;============
;=Curvevalue= 
;============
Function curvevalue#(current#,destination#,curve)
	current#=current#+((destination#-current#)/curve)
	Return current#
End Function
;============


; Height Map Loader
Function LoadSegTerrain (hmap$,seg=4);(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 = h_width / seg 
	
	; now we build each segment individually
	For loop = 1 To seg_total
	s=0
		; 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
		If z_count > seg-1 z_off = z_off + 1:z_count=0
		; color the segment to show it's actually working
		EntityColor mesh,Rand(50,255),Rand(50,255),Rand(50,255)
		Ter_Segment(loop,0)=mesh
		Ter_Segment(loop,1)=z_count*seg_quads
		Ter_Segment(loop,2)=0
		Ter_Segment(loop,3)=z_off*seg_quads
		PositionEntity Ter_Segment(loop,0),Ter_Segment(loop,1),0,Ter_Segment(loop,3)
		z_count = z_count + 1
		If loop > 1 EntityParent Ter_Segment(loop,0),Ter_Segment(1,0)
	Next
	
	Return Ter_Segment(1,0)

End Function