Get hieght info from a heightmap

BlitzMax Forums/BlitzMax Programming/Get hieght info from a heightmap

ckob(Posted 2006) [#1]
I am trying to write a loadterrain function to work with MiniB3d, I found a pretty good one on the code archives thanks to Ross C but the only command I cant seem to figure out to port over to Bmax and MiniB3d is Colorred which he uses in this line.

VertexCoords surf,index,VertexX(surf,index),ColorRed()/20.0,VertexZ(surf,index)

I know what it does in Blitz3d I guess my question would be how can I do this in Blitzmax using MiniB3d?


fredborg(Posted 2006) [#2]
Here's a simple LoadTerrain function + example:
Strict

Import "../MiniB3D.bmx"

Local width=640,height=480,depth=16,mode=0

Graphics3D width,height,depth,mode

Local  cam:TEntity = CreateCamera()
PositionEntity cam,0,0,-10

Local light:TEntity = CreateLight(1)

Local tex:TTexture = LoadTexture("media/test.png")

Local terrain:TEntity = LoadTerrain("media/test.png")

PositionEntity terrain,0,-100,0
ScaleEntity terrain,1,100,1

EntityTexture terrain,tex

Local cx#=0
Local cy#=0
Local cz#=0

Local pitch#=0
Local yaw#=0
Local roll#=0

' used by fps code
Local old_ms=MilliSecs()
Local renders
Local fps

While Not KeyDown(KEY_ESCAPE)		

	If KeyHit(KEY_ENTER) Then DebugStop

	' control camera

	If KeyDown(KEY_UP) Then cz#=cz#+1.0
	If KeyDown(KEY_LEFT) Then cx#=cx#-1.0
	If KeyDown(KEY_RIGHT) Then cx#=cx#+1.0
	If KeyDown(KEY_DOWN) Then cz#=cz#-1.0
	
	If KeyDown(KEY_W) Then pitch#=pitch#-1.0
	If KeyDown(KEY_A) Then yaw#=yaw#+1.0
	If KeyDown(KEY_S) Then pitch#=pitch#+1.0
	If KeyDown(KEY_D) Then yaw#=yaw#-1.0

	MoveEntity cam,cx#*0.5,cy#*0.5,cz#*0.5
	RotateEntity cam,pitch#,yaw#,roll#
	
	cx#=0
	cy#=0
	cz#=0

	cx#=0
	cy#=0
	cz#=0

	RenderWorld
	renders=renders+1

	' calculate fps
	If MilliSecs()-old_ms>=1000
		old_ms=MilliSecs()
		fps=renders
		renders=0
	EndIf
	
	DebugText 0,0,"FPS: "+String(fps)

	Flip

Wend
End

Function LoadTerrain:TMesh(url:Object,parent:TEntity=Null)

	Local pix:TPixmap

	If TPixmap(url)
		pix = TPixmap(url)
	Else
		pix = LoadPixmap(url)
		If pix = Null Return Null
	EndIf

	Local mesh:TMesh = CreateMesh(parent)
	Local surf:TSurface = CreateSurface(mesh,Null)

	Local uscale:Float = 1.0/pix.width
	Local vscale:Float = 1.0/pix.height

	For Local x:Int = 0 Until pix.width
		For Local y:Int = 0 Until pix.height
			Local argb:Int = pix.ReadPixel(x,y)
			Local height:Float = (((argb Shl 16) & $FF) + ((argb Shl 8) & $FF) + (argb & $FF))/(3.0*255.0)

			Local v:Int = AddVertex(surf,x,height,y,x*uscale,y*vscale)
			VertexNormal surf,v,0,0,0
		Next
	Next

	For Local x:Int = 0 Until pix.width-1
		For Local y:Int = 0 Until pix.height-1
			Local v0:Int = x*pix.height + y
			Local v1:Int = v0 + 1
			Local v2:Int = v1 + pix.height
			Local v3:Int = v0 + pix.height
			
			Local t0:Int = AddTriangle(surf,v0,v1,v2)
			Local t1:Int = AddTriangle(surf,v0,v2,v3)
			
			'
			' Simple normal fix
			Local nx0:Float = surf.TriangleNX(t0)
			Local ny0:Float = surf.TriangleNY(t0)
			Local nz0:Float = surf.TriangleNZ(t0)
			Local nx1:Float = surf.TriangleNX(t1)
			Local ny1:Float = surf.TriangleNY(t1)
			Local nz1:Float = surf.TriangleNZ(t1)
						
			VertexNormal surf,v0,surf.VertexNX(v0)+nx0+nx1,surf.VertexNY(v0)+ny0+ny1,surf.VertexNZ(v0)+nz0+nz1
			VertexNormal surf,v1,surf.VertexNX(v1)+nx0    ,surf.VertexNY(v1)+ny0    ,surf.VertexNZ(v1)+nz0
			VertexNormal surf,v2,surf.VertexNX(v2)+nx0+nx1,surf.VertexNY(v2)+ny0+ny1,surf.VertexNZ(v2)+nz0+nz1
			VertexNormal surf,v3,surf.VertexNX(v3)+nx1    ,surf.VertexNY(v3)+ny1    ,surf.VertexNZ(v3)+nz1
		Next
	Next

	'
	' Normalize vertex normals
	For Local v:Int = 0 Until CountVertices(surf)
		Local nx:Float = surf.VertexNX(v)
		Local ny:Float = surf.VertexNY(v)
		Local nz:Float = surf.VertexNZ(v)
		Local d:Float = 1.0 / Sqr(nx*nx + ny*ny + nz*nz)
		VertexNormal surf,v,nx*d,ny*d,nz*d
	Next

	Return mesh

End Function
Updated with a simple normal calculation.


ckob(Posted 2006) [#3]
wow thanks, is there a huge code archive of stuff people have done with Minib3d im missing?


fredborg(Posted 2006) [#4]
I don't think so. I just slapped it together in 10 minutes.


fredborg(Posted 2006) [#5]
I added a simple vertexnormal calculation.


impixi(Posted 2006) [#6]
I've been toying around with minib3d and heightmaps myself (see my sig for a link to some generation code).

The trouble is, the huge, complex meshes required for terrains will perform sluggishly without some sort of LOD implementation.

Can anyone point me in the right direction for terrain LOD algorithms?