The 3D grid

Blitz3D Forums/Blitz3D Programming/The 3D grid

Graphics3D 800,600,0,2

SetBuffer BackBuffer()

Const MapX = 100
Const MapZ = 100
Const GridSize = 2

;;;;Grid Array//Determines how many squares in the grid
Dim mapgrid(MapX,MapZ)

For z = 0 To MapZ-1
	For x = 0 To MapX-1
		mapgrid(x,z) = Rand(0,1)
	Next
Next

cam1 = CreateCamera()
AmbientLight 255, 255, 255
PositionEntity cam1, 0, 20, 0

gridmesh = CreateGrid()

While Not KeyHit(1)
	If KeyDown(200) Then MoveEntity cam1, 0, 0, 1
	If KeyDown(208) Then MoveEntity cam1, 0, 0, -1
	If KeyDown(203) Then TurnEntity cam1, 0, 1, 0
	If KeyDown(205) Then TurnEntity cam1, 0, -1, 0

	; If you want to test mesh-generating speed, replace keyhit(57) by keydown(57), then every frame a new random mesh will be generated
	; while holding down the spacebar
	If KeyHit(57) Then
		; Create a random tiled map (place new random numbers in the array)
		For z = 0 To MapZ-1
			For x = 0 To MapX-1
				mapgrid(x,z) = Rand(0,1)
			Next
		Next

		; Remove the mesh from memory
		FreeEntity gridmesh
		; Re-create the mesh
		CreateGrid()
	EndIf

	UpdateWorld 
	RenderWorld

	FPS()
	Text 10, 20, "Tris rendered: " + TrisRendered()

	Flip
	Cls
Wend



Function CreateGrid()
	Local mesh = CreateMesh()
	Local surf = CreateSurface(mesh)

	For z = 0 To MapZ-1
		For X = 0 To MapX-1
			If mapgrid(x,z) = 1 Then
				CreateTile(surf,x,z)
			EndIf
		Next
	Next

	Return mesh
End Function

Function CreateTile(surf, x, z)
	v0 = AddVertex(surf, (x*GridSize)+((GridSize/2)*(-1)), 0, (z*GridSize)+((GridSize/2)))
	v1 = AddVertex(surf, (x*GridSize)+((GridSize/2)), 0, (z*GridSize)+((GridSize/2)))
	v2 = AddVertex(surf, (x*GridSize)+((GridSize/2)), 0, (z*GridSize)+((GridSize/2)*(-1)))
	v3 = AddVertex(surf, (x*GridSize)+((GridSize/2)*(-1)), 0, (z*GridSize)+((GridSize/2)*(-1)))
	t0 = AddTriangle(surf, v0, v1, v3)
	t1 = AddTriangle(surf, v1, v2, v3)
End Function



;;;;;;//FPS
Global fpstime,fpscount,fps

Function FPS()
	fpscount=fpscount+1
	If MilliSecs()>fpstime Then 
		fpstime=MilliSecs()+1000
		fps=fpscount
		fpscount=0
	EndIf 
	Text 10,0,"FPS: "+fps
End Function
;;;;;\\FPS

This code generates a mesh with size 100x100 (1000x1000 creates a MAV, because meshes are limited to 65535 vertices) as one big mesh.
It creates one mesh and adds vertices to it to generate the entire mesh, based on the value in the corresponding index in the array.
First the code places random values (0 or 1) in the array.
The the CreateGrid loops through the array and if it sees "1", it creates a tile in the big mesh.
If it's 0, do nothing.

You can move the camera using the up/down arrowkeys, and turn the camera using left/right arrowkeys.
When you hit the spacebar, the program places new random values in the array, frees the current big mesh and re-creates the mesh.
You will see that this works very fast, as continued hitting the spacebar doesn't even slow down.
If you want to create a map of size 1000x1000, I suggest that you create 100 big meshes, each of size 100x100, and place them next to eachother.
This way you reduce surface-count (this big mesh has only 1 surface).
If you where to create 1000x1000 seperate meshes, then you would have 1000.000 meshes and thus 1000.000 surfaces and your computer will crawl while rendering that much surfaces.