The 3D grid

Blitz3D Forums/Blitz3D Programming/The 3D grid

Actually, I began with this just a week ago. :-)

This code adds picking to the program.
It creates an infinity plane which is pickable, but entityalpha is set to 0 to make in invisible.
I did it this way to be able to add tiles to the big mesh too, instead of only removing them.
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

; Create an endless plane (used to do the picking), move it down a bit, set a different color and set pickmode
plane = CreatePlane()
MoveEntity plane, 0, -0.1, 0
EntityColor plane, 255, 0, 0
EntityPickMode plane, 2
EntityAlpha plane, 0

; Create camera and position it above the world
cam1 = CreateCamera()
AmbientLight 255, 255, 255
PositionEntity cam1, 100, 100, 100
TurnEntity cam1, 90, 0, 0

; Create a big mesh where each tile is created based on the value in the array
Global gridmesh = CreateGrid()
Global texture = LoadTexture("texture.jpg")
EntityTexture gridmesh, texture

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

	If MouseHit(1) Then
		PickedEnt = CameraPick(cam1, MouseX(), MouseY())
		; Calculate which tile to be switched on/off
		x = PickedX() / GridSize
		z = PickedZ() / GridSize
		If (x>= 0) And (x<=MapX) And (z>=0) And (z<=MapZ) Then
			; Toggle the tile on/off
			If mapgrid(x,z) = 1 Then mapgrid(x,z) = 0 Else mapgrid(x,z) = 1
			; Delete the big mesh
			FreeEntity gridmesh
			; Re-create the big mesh
			gridmesh = CreateGrid()
			; Apply the texture to it
			EntityTexture gridmesh, texture
		EndIf
	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)), 0, 0)
	v1 = AddVertex(surf, (x*GridSize)+((GridSize/2)), 0, (z*GridSize)+((GridSize/2)), 1, 0)
	v2 = AddVertex(surf, (x*GridSize)+((GridSize/2)), 0, (z*GridSize)+((GridSize/2)*(-1)), 1, 1)
	v3 = AddVertex(surf, (x*GridSize)+((GridSize/2)*(-1)), 0, (z*GridSize)+((GridSize/2)*(-1)), 0, 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


By a quick peek at your code, it seems that you create each tile as a seperate mesh, that's why your code renders slower, as it has much more seperate meshes and seperate surfaces.

My code creates one big mesh, and each tile's vertices and triangles are added to it.
It also adds UV coords to the vertices and applies a texture on the mesh.

Now the camera floats above the mesh, looking down.
Move the camera with up/down/left.right, and zoom in/out using pageup/pagedown.
Clicking will toggle the tile on/off.