One surface

Blitz3D Forums/Blitz3D Programming/One surface

Hi!

Is it possible to convert a multiple surface mesh to a single sutface mesh? (I don't care how it looks texture wise, as long as the shape is the same)

Yes, as long as the mesh doesn't contain too many verts/tris to fit in a single surface. Just build the new mesh from scratch by iterating through all the verts/tris and copying them into the new mesh.

You could even save the texturing by combining the textures and adjusting the UVs accordingly, but you could potentially end up with some very large textures.

thanks guys. I'm not very good at manually building a mesh, I'm gonna take a shot at it!

based on some code by Sswift, I am trying to achieve this, but all meshes change in some sort of abstract cubism :(

Type triangle
	Field index
	Field surface
End Type



Function newMeshOneSurface(ThisMesh)

		; Create a new mesh.
		NewMesh = CreateMesh()	
		Surfaces=CountSurfaces(ThisMesh)
		destSurface = CreateSurface(NewMesh)

		; Loop through each surface of the mesh.
		For Surface_Index = 1 To Surfaces

			; Get the pointer to the this surface and the number of vertices in it.
			SrcSurface = GetSurface(ThisMesh, Surface_Index)
			Tris = CountTriangles(SrcSurface)
	
			; Copy all the vertices from the source surface to the destination surface.
			SrcVerts = CountVertices(SrcSurface)
			For VertLoop = 0 To SrcVerts-1
		
				Vx#  = VertexX#(SrcSurface, VertLoop)
				Vy#  = VertexY#(SrcSurface, VertLoop)
				Vz#  = VertexZ#(SrcSurface, VertLoop)
				Vu#  = VertexU#(SrcSurface, VertLoop)
				Vv#  = VertexV#(SrcSurface, VertLoop)		
				Vw#  = VertexW#(SrcSurface, VertLoop)
				Vnx# = VertexNX#(SrcSurface, VertLoop)
				Vny# = VertexNY#(SrcSurface, VertLoop)
				Vnz# = VertexNZ#(SrcSurface, VertLoop)						
				Vr   = VertexRed(SrcSurface, VertLoop)
				Vg   = VertexGreen(SrcSurface, VertLoop)
				Vb   = VertexBlue(SrcSurface, VertLoop)
				AddVertex(DestSurface, Vx#, Vy#, Vz#, Vu#, Vv#, Vw#)
				VertexNormal(DestSurface, VertLoop, Vnx#, Vny#, Vnz#)
				VertexColor(DestSurface, VertLoop, Vr, Vg, Vb) 
	
			Next

			; Copy all triangles from the source surface to the destination surface.	
			SrcTris  = CountTriangles(SrcSurface)
			For TriLoop = 0 To SrcTris-1
	
				Copy_Tri = True
				
				For ThisTri.Triangle = Each Triangle
									
					; If this triangle is a coincident triangle that should be removed...
					If (ThisTri\Surface = SrcSurface) And (ThisTri\Index = TriLoop) 
						
						; Do not copy the triangle.
						Copy_Tri = False
						
						; Exit ThisTri loop early.
						Exit
						
					EndIf
						
				Next
	
	
				; If it's okay to copy this triangle...
				If Copy_Tri			
		
					V0 = TriangleVertex(SrcSurface, TriLoop, 0)
					V1 = TriangleVertex(SrcSurface, TriLoop, 1)
					V2 = TriangleVertex(SrcSurface, TriLoop, 2)
					AddTriangle(DestSurface, V0, V1, V2)
		
				EndIf
	
			Next
		
		Next
		Return NewMesh 
	End Function


Does the addmesh commands not combine and create one surface? I'm sure it does, but you lose the texture info i think for all but one surface.

Yeah, addmesh will work as long as the surfaces share the same brush properties.

I would do it like this (untested):

Function CollapseMesh(Mesh)

	BaseSurface = GetSurface(Mesh, 1)
	For i = 2 To CountSurfaces(Mesh)
		S = GetSurface(Mesh, i)
		For j = 0 To CountTriangles(S) - 1

			V0 = TriangleVertex(S, j, 0)
			V1 = TriangleVertex(S, j, 1)
			V2 = TriangleVertex(S, j, 2)

			NV0 = AddVertex(BaseSurface, VertexX#(S, V0), VertexY#(S, V0), VertexZ#(S, V0), VertexU#(S, V0), VertexV#(S, V0))
			VertexNormal BaseSurface, NV0, VertexNX#(S, V0), VertexNY#(S, V0), VertexNZ#(S, V0)
			VertexColor BaseSurface, NV0, VertexRed#(S, V0), VertexGreen#(S, V0), VertexBlue#(S, V0), VertexAlpha#(S, V0)

			NV1 = AddVertex(BaseSurface, VertexX#(S, V1), VertexY#(S, V1), VertexZ#(S, V1), VertexU#(S, V1), VertexV#(S, V1))
			VertexNormal BaseSurface, NV1, VertexNX#(S, V1), VertexNY#(S, V1), VertexNZ#(S, V1)
			VertexColor BaseSurface, NV1, VertexRed#(S, V1), VertexGreen#(S, V1), VertexBlue#(S, V1), VertexAlpha#(S, V1)

			NV2 = AddVertex(BaseSurface, VertexX#(S, V2), VertexY#(S, V2), VertexZ#(S, V2), VertexU#(S, V2), VertexV#(S, V2))
			VertexNormal BaseSurface, NV2, VertexNX#(S, V2), VertexNY#(S, V2), VertexNZ#(S, V2)
			VertexColor BaseSurface, NV2, VertexRed#(S, V2), VertexGreen#(S, V2), VertexBlue#(S, V2), VertexAlpha#(S, V2)

			AddTriangle BaseSurface, NV0, NV1, NV2
		Next
	Next

End Function


w00t. Nice guys. Rottbott, looks good, but the mesh has still 23 surfaces. However, one texture is smeared all over the mesh.