Any ideas on this?

Blitz3D Forums/Blitz3D Programming/Any ideas on this?

EntityOrder messes with the z-ordering of the meshes triangles, so there is nothing you can do about it.

Correct, I think the zbuffer is disabled when drawing meshes that use EntityOrder. But you should be able to compensate for this by using the function below. (demo included so you can see the difference)

PCD: Try running the gun mesh through this ZSortMesh() function. It rebuilds the triangle index so that polys furthest along the Z axis will get rendered first.

Graphics3D 800,600,32,2

cam=CreateCamera()
PositionEntity cam,-1,1,-4
light=CreateLight()
RotateEntity light,20,20,0

;Create an unsorted mesh
mesh=CreateSphere(16)
EntityColor mesh,255,0,0
mesh2=CreateSphere(16)
PositionMesh mesh2,0,0,4
AddMesh mesh2,mesh
FreeEntity mesh2

EntityOrder mesh,-1

While Not KeyHit(1)

	If KeyHit(57) ZSortMesh mesh

	RenderWorld
	Text 0,0,"Press SPACE to Z-Sort mesh"
	Flip	
Wend




Type ztri
	Field z#
	Field v0,v1,v2
End Type

Function ZSortMesh(mesh)
	Local temp.ztri = New ztri
	Local tris.ztri[4096] ; assuming meshes have 4096 or less triangles, increase as needed
	
	For i=0 To 4095
		tris[i] = New ztri
	Next

	surfCount = CountSurfaces(mesh)
	
	For i = 1 To surfCount
		surf = GetSurface(mesh,i)
		triCount = CountTriangles(surf)

		;Store each triangles Z distance (based on its vertices average Z value)
		For t = 0 To triCount-1
			z = VertexZ(surf,TriangleVertex(surf,t,0))
			z=z+VertexZ(surf,TriangleVertex(surf,t,1))
			z=z+VertexZ(surf,TriangleVertex(surf,t,2))
			z=z/3

			tris[t]\z = z
			tris[t]\v0 = TriangleVertex(surf,t,0) ;store triangle vertex indexes
			tris[t]\v1 = TriangleVertex(surf,t,1)
			tris[t]\v2 = TriangleVertex(surf,t,2)
		Next

		;Sort the triangles based on Z distance
		For x=0 To triCount-1
			For y=0 To triCount-1
				If x<>y; don't compare with itself
					If tris[y]\z < tris[x]\z
						;Swap the 2 tris
						temp\z  = tris[y]\z
						temp\v0 = tris[y]\v0
						temp\v1 = tris[y]\v1
						temp\v2 = tris[y]\v2

						tris[y]\z  = tris[x]\z
						tris[y]\v0 = tris[x]\v0
						tris[y]\v1 = tris[x]\v1
						tris[y]\v2 = tris[x]\v2
						
						tris[x]\z  = temp\z
						tris[x]\v0 = temp\v0
						tris[x]\v1 = temp\v1
						tris[x]\v2 = temp\v2
					End If
				End If
			Next
		Next

		;Clear the existing triangles
		ClearSurface surf,False,True

		;Add the sorted triangles
		For x = 0 To triCount-1
			AddTriangle surf,tris[x]\v0,tris[x]\v1,tris[x]\v2
		Next
	Next
	Delete Each ztri	
End Function