Checkered Map Maker

Blitz3D Forums/Blitz3D Beginners Area/Checkered Map Maker

Heres a little checkered map maker I made to test out my pathfinding routine. The maximum map size is 170x170 which creates a map of 85 tiles x 85 tiles due to the vertice per surface limit.

Graphics3D 800,600,16,2
SetBuffer BackBuffer()

Global fpstime,fpscount,FPS
;170X170 IS THE MAX DUE TO VERTICE LIMITATIONS
Global MapX = 170
Global MapZ = 170
Dim Vert%(8)
Dim Tri%(7)
Global Mesh%
Global Surf%

Global Cam = CreateCamera()
PositionEntity Cam,MapX/2,MapX,MapZ/2

Global Lite = CreateLight()
PositionEntity Lite,MapX/2,MapX,MapZ/2

Sphere = CreateSphere()
PositionEntity Sphere,MapX/2,1,MapZ/2


PointEntity Cam,Sphere

Global Map = CreateGrid()

CameraClsColor Cam,255,0,0
HideEntity Sphere

While Not KeyHit(1)
	Cls
	
	If KeyHit(17)
		If w = 0 Then
			WireFrame True
			w = 1
		Else
			WireFrame  False
			w = 0
		EndIf
		
	EndIf
	
	RenderWorld()
	UpdateWorld()
	
	FPS()
	Text 10,30,"Verts: "+CountVertices(Surf)
	Text 10,50,"Tris: "+CountTriangles(Surf)
	
	Flip False
	
Wend
ClearWorld()
End()


Function CreateGrid()
	Mesh = CreateMesh()
	Surf = CreateSurface(Mesh)
	EntityFX Mesh,2+1
	
	
	For z = 0 To MapZ-1 Step 2
		;THIS WILL STAGGER THE COLORS FOR Z
		If b = 2 Then
			a = 2
			b = 0
		Else
			a = 0
			b = 2
		EndIf
		
		For x = 0 To MapX-1 Step 2
			;THIS WILL STAGGER THE COLORS FOR X
			If a = 2 Then
				CreateTile(Surf,x,z,255,255,255,255)
				a = 0
			Else
				CreateTile(Surf,x,z,5,5,5,255)
				a = 2
			EndIf
		Next
	Next
	
	Return Mesh
End Function

Function CreateTile(surf%, x%, z%, r%, g%, b%, a%) 
	a = 0
	
	For Col% = z To z+2
		For Row% = x To x+2
			Vert(a) = AddVertex(surf,Row,0,Col)
			VertexColor surf, Vert(a),r,g,b,a
			a = a + 1
		Next
	Next
	
	Tri(0) = AddTriangle(surf,Vert(0),Vert(3),Vert(1)) : Tri(1) = AddTriangle(surf,Vert(1),Vert(3),Vert(4))
	Tri(2) = AddTriangle(surf,Vert(1),Vert(4),Vert(2)) : Tri(3) = AddTriangle(surf,Vert(2),Vert(4),Vert(5))
	Tri(4) = AddTriangle(surf,Vert(3),Vert(6),Vert(4)) : Tri(5) = AddTriangle(surf,Vert(4),Vert(6),Vert(7))
	Tri(6) = AddTriangle(surf,Vert(4),Vert(7),Vert(5)) : Tri(7) = AddTriangle(surf,Vert(5),Vert(7),Vert(8))
End Function

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


This only shows a red screen. I hit W for wireframe, but it doesn't make a diffrence. Is it supposed to look like that?

Hmmm, thats strange, I copied and pasted the whole source into a new bb and it worked fine.

Ill check it out more and see if theres anything funny going on.

My crappy onboard gfx may be the cause.

lol. You can try downloading my source files for pathfinding v13 here...

http://hosted.filefront.com/Xyled777/

It has the same checkered map maker in it. Or you can download the PFV13.exe and see if that works.

Tried to find the bug, still no luck. One thing: the function
CreateTile() takes an alpha parameter, that will be overwritten be the vertex counter variable A%, but is then still used as alpha parameter with the VertexColor command.

I have to say, my card is a little picky with alpha blending, there's something bug-like that forces some special steps in the code, if you want the code to be compatible with this relative common cheapo notebook gfx chip (VIA/S3G Unichrome Pro IGP). This issue was discussed in the b3d bug report section.

wow, ok, that just looks like a simple oversight? Ok, I do apologize for that. Its a major booboo. Thank you for finding that.

go here and get the corrected version...
http://hosted.filefront.com/Xyled777/

thank you for pointing out the error.

For those that want to copy and paste...
Graphics3D 800,600,16,2
SetBuffer BackBuffer()

Global fpstime,fpscount,FPS
;170X170 IS THE MAX DUE TO VERTICE LIMITATIONS
Global MapX = 170
Global MapZ = 170
Dim Vert%(8)
Dim Tri%(7)
Global Mesh%
Global Surf%

Global Cam = CreateCamera()
PositionEntity Cam,MapX/2,MapX,MapZ/2

Global Lite = CreateLight()
PositionEntity Lite,MapX/2,MapX,MapZ/2

Sphere = CreateSphere()
PositionEntity Sphere,MapX/2,1,MapZ/2


PointEntity Cam,Sphere

Global Map = CreateGrid()

CameraClsColor Cam,255,0,0
HideEntity Sphere

While Not KeyHit(1)
	Cls
	
	If KeyHit(17)
		If w = 0 Then
			WireFrame True
			w = 1
		Else
			WireFrame  False
			w = 0
		EndIf
		
	EndIf
	
	RenderWorld()
	UpdateWorld()
	
	FPS()
	Text 10,30,"Verts: "+CountVertices(Surf)
	Text 10,50,"Tris: "+CountTriangles(Surf)
	
	Flip False
	
Wend
ClearWorld()
End()


Function CreateGrid()
	Mesh = CreateMesh()
	Surf = CreateSurface(Mesh)
	EntityFX Mesh,2+1
	
	
	For z = 0 To MapZ-1 Step 2
		;THIS WILL STAGGER THE COLORS FOR Z
		If b = 2 Then
			a = 2
			b = 0
		Else
			a = 0
			b = 2
		EndIf
		
		For x = 0 To MapX-1 Step 2
			;THIS WILL STAGGER THE COLORS FOR X
			If a = 2 Then
				CreateTile(Surf,x,z,255,255,255,255)
				a = 0
			Else
				CreateTile(Surf,x,z,5,5,5,255)
				a = 2
			EndIf
		Next
	Next
	
	Return Mesh
End Function

Function CreateTile(surf%, x%, z%, r%, g%, b%, alph%) 
	a = 0
	
	For Col% = z To z+2
		For Row% = x To x+2
			Vert(a) = AddVertex(surf,Row,0,Col)
			VertexColor surf, Vert(a),r,g,b,alph
			a = a + 1
		Next
	Next
	
	Tri(0) = AddTriangle(surf,Vert(0),Vert(3),Vert(1)) : Tri(1) = AddTriangle(surf,Vert(1),Vert(3),Vert(4))
	Tri(2) = AddTriangle(surf,Vert(1),Vert(4),Vert(2)) : Tri(3) = AddTriangle(surf,Vert(2),Vert(4),Vert(5))
	Tri(4) = AddTriangle(surf,Vert(3),Vert(6),Vert(4)) : Tri(5) = AddTriangle(surf,Vert(4),Vert(6),Vert(7))
	Tri(6) = AddTriangle(surf,Vert(4),Vert(7),Vert(5)) : Tri(7) = AddTriangle(surf,Vert(5),Vert(7),Vert(8))
End Function

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


If the manual is correct, then the alpha parameter is a float from 0.0 to 1.0. I do have however still problems in makeing this code work.

Ahhh, yes that would make sense, I got confused by reading the addvertex command in the docs...

"When adding a vertex its default color is 255,255,255,255"

But the alpha value shouldnt interfere because I havent set the EntityFX of the mesh to 32 as the docs have read. But I will change the value to 1, just to be safe.

Do you have another pc you can try this on? Just to verify it works?

I added entityfx 32 to the mesh and tested a 0 and 1 parameter, they both worked as expected. I dont understand why it doesnt work on your machine.

You can copy and paste this code into a new file and try it out with entityfx +32 enabled

Graphics3D 800,600,16,2
SetBuffer BackBuffer()

Global fpstime,fpscount,FPS
;170X170 IS THE MAX DUE TO VERTICE LIMITATIONS
Global MapX = 170
Global MapZ = 170
Dim Vert%(8)
Dim Tri%(7)
Global Mesh%
Global Surf%

Global Cam = CreateCamera()
PositionEntity Cam,MapX/2,MapX,MapZ/2

Global Lite = CreateLight()
PositionEntity Lite,MapX/2,MapX,MapZ/2

Sphere = CreateSphere()
PositionEntity Sphere,MapX/2,1,MapZ/2


PointEntity Cam,Sphere

Global Map = CreateGrid()

CameraClsColor Cam,255,0,0
HideEntity Sphere

While Not KeyHit(1)
	Cls
	
	If KeyHit(17)
		If w = 0 Then
			WireFrame True
			w = 1
		Else
			WireFrame  False
			w = 0
		EndIf
		
	EndIf
	
	RenderWorld()
	UpdateWorld()
	
	FPS()
	Text 10,30,"Verts: "+CountVertices(Surf)
	Text 10,50,"Tris: "+CountTriangles(Surf)
	
	Flip False
	
Wend
ClearWorld()
End()


Function CreateGrid()
	Mesh = CreateMesh()
	Surf = CreateSurface(Mesh)
	EntityFX Mesh,2+1+32 ;2 is Vertex Colors ilo Brush, 1 is full bright, 32 is enable alpha
	
	
	For z = 0 To MapZ-1 Step 2
		;THIS WILL STAGGER THE COLORS FOR Z
		If b = 2 Then
			a = 2
			b = 0
		Else
			a = 0
			b = 2
		EndIf
		
		For x = 0 To MapX-1 Step 2
			;THIS WILL STAGGER THE COLORS FOR X
			If a = 2 Then
				CreateTile(Surf,x,z,255,255,255,1)
				;CreateTile(Surf,x,z,255,255,255,0) ;This Hides the white tiles
				a = 0
			Else
				CreateTile(Surf,x,z,5,5,5,1)
				;CreateTile(Surf,x,z,5,5,5,0) ;This hides the black tiles
				a = 2
			EndIf
		Next
	Next
	
	Return Mesh
End Function

Function CreateTile(surf%, x%, z%, r%, g%, b%, alph%) 
	a = 0
	
	For Col% = z To z+2
		For Row% = x To x+2
			Vert(a) = AddVertex(surf,Row,0,Col)
			VertexColor surf, Vert(a),r,g,b,alph
			a = a + 1
		Next
	Next
	
	Tri(0) = AddTriangle(surf,Vert(0),Vert(3),Vert(1)) : Tri(1) = AddTriangle(surf,Vert(1),Vert(3),Vert(4))
	Tri(2) = AddTriangle(surf,Vert(1),Vert(4),Vert(2)) : Tri(3) = AddTriangle(surf,Vert(2),Vert(4),Vert(5))
	Tri(4) = AddTriangle(surf,Vert(3),Vert(6),Vert(4)) : Tri(5) = AddTriangle(surf,Vert(4),Vert(6),Vert(7))
	Tri(6) = AddTriangle(surf,Vert(4),Vert(7),Vert(5)) : Tri(7) = AddTriangle(surf,Vert(5),Vert(7),Vert(8))
End Function

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


I've tried many things now, including the Bit 32, no luck. THe whole checkerboard simply remains invisible.

So I used to write a similar program based on the towel function of my old flag demo. This works':

Graphics3D 800,600,32,2
SetBuffer BackBuffer()



cam=CreateCamera()
CameraRange cam,.1,1000
PositionEntity cam,16,32,-10 
TurnEntity cam,30,0,0



b1=CreateBrush()


;size of map
Const Gridx=120 
Const Gridz=120 

x#=0:y#=0:z#=0



; used for surface
Const Texture_Grid=4

; create Mesh
Dim map#(gridx,gridz)
mesh=Create_Map(gridx,gridz,b1,3)



While KeyDown(1)=0
 RenderWorld()
 Flip
Wend
End


Function Create_Map(tilex,tilez,brush1,tile)
.create_map
	mesh=CreateMesh()
	surf=CreateSurface(mesh,brush1)
	wid#=Float(1)/Float(Texture_grid)
	u0#=wid*Float(tile Mod texture_Grid)
	v0#=wid*Float(tile/texture_grid)
	u1#=u0+wid
	v1#=v0
	u2#=u1
	v2#=v0+wid
	u3#=u0
	v3#=v2
	u#=0
	v#=0
	stp#=1.0/Float(tilex)
	For z#=0 To tilez-1
		u=0
		For x#=0 To tilex-1
			h1#=map(x,z)
			h2#=map(x+1,z)
			h3#=map(x+1,z+1)
			h4#=map(x,z+1)
			AddVertex surf,x,h1,z,u0,v0
			VertexTexCoords surf,cnt,u,v,0,1
			
			AddVertex surf,x+1,h2,z,u1,v1
			VertexTexCoords surf,cnt+1,u+stp,v,0,1
			
			AddVertex surf,x+1,h3,z+1,u2,v2
			VertexTexCoords surf,cnt+2,u+stp,v+stp,0,1
			
			AddVertex surf,x,h4,z+1,u3,v3
			VertexTexCoords surf,cnt+3,u,v+stp,0,1
			
			AddTriangle surf,cnt,cnt+2,cnt+1
			AddTriangle surf,cnt,cnt+3,cnt+2
			
			If (((x And 1)=1) And ((z And 1)=1))   Or   (((x And 1)=0) And ((z And 1)=0))
			 For i=0 To 3
			  VertexColor surf,cnt+i,255,255,255 
			 Next
			Else
			 For i=0 To 3
			  VertexColor surf,cnt+i,5,5,5
			 Next
			EndIf
			
			cnt=cnt+4
			u=u+stp
		Next
		v=v+stp
	Next
	UpdateNormals mesh
	EntityFX mesh,32 Or 2 Or 1
	Return mesh
End Function



I just can't see the diffrence.

Now this is going to drive me nuts, I am still quite a beginner when it comes to code and design, but this is going to bother me, hehehehe. I sure wish I was having this problem so I could try stuff out and pinpoint it.

The differences I see, as Im sure you noticed...
You define a brush parameter in the createsurface command
You dont use CamClscolor or the cls command
You assign UV Coords to the newly created vertices
You updatenormals on the mesh at the end of the function

Maybe you can either try adding these items to my code or deleted them from you code to see if you can cause the same problem or fix the problem with my code.

If anyone else trys out this code and has a similar problem, please post the situation here.

Thanks!

I already tried most of what you've listed. Note: my sample doesn't work here if I don't add flag 32 to the ENtityFX! I guess this is a special feature of this card, that is, as noted before, picky when it comes to alpha. When I create a texture with flag 2 only then it fails and a TextureBuffer call will MAV (where the code works perfectly on most machines). If I add the flag 1 then it works nicely.

Right now I can see only one thing that I didn't try with your source: assigning UVs to the vertices. But I doubt this will help. Well, don 't worry too much. This Chip is really not a good choice to play games, so people won't be too surprised if things don't work.

Man what a bummer. I do appreciate you pluggin away at it to try to pinpoint whats going on. If you keep at it, please post what you find out.

Thanks.