Remove vertex? (clean mesh)

Blitz3D Forums/Blitz3D Programming/Remove vertex? (clean mesh)

LamptonWorm(Posted 2008) [#1]
Hi all,

If I create two cubes and place them right next to each other on the x-axis (one is a child of the other), is it possible to weld the two shapes together by identifying the overlapping vertices and removing one of them?

I didn't see a 'delete vertex' command when I was browsing the docs, sorry if I missed it!

Basically in my editor I place a load of meshes then save them out as a single mesh for collision, but there are times when I place one wall next to another, and there are 'extra' poly/verts where two shapes overlay exactly, so for performace I'd like to detect and remove these for more geometrically sound models.

Thoughts welcome!

Cheers,
LW.


SytzeZ(Posted 2008) [#2]
Maybe you could create a function that copies the whole surface without without those vertices


GfK(Posted 2008) [#3]
There isn't a 'delete vertex' command. To delete a vertex you must manually rebuild the entire mesh, sans-aforementioned vertex.

If you want to weld vertices then you should do it the same way 3DS Max etc does it; i.e. with proximity checks between each vertex.


Bobysait(Posted 2008) [#4]
use ClearSurface Surface,true
=> It cleans ALL vertices within the surface.
The way to weld vertex together :
( according, you 'll maybe weld two vertices with different UV ... so take car what you expect ^^ )
+> Copy vertices into array/Types etc... ( copy infos : Coords,Normals,UV, Color )
+> Copy triangle into an array
copy the 3 TriangleVertex(Surfave,Triangle,0/1/2)

Clearsurface Surface,True,True => it will "delete" every vertices and every triangles.

+> rebuild surface using the stored array.
Take care with the Id you'll use too rebuild, according you deleted some one those ^^


LamptonWorm(Posted 2008) [#5]
Thanks, looks like this is going to be possible with some work.

Cheers,
LW.


Stevie G(Posted 2008) [#6]
Entityalpha StevieG, 1

@ LamptonWorm,

I have a function which I use for this very purpose. Code and example below. Note that the function expects the mesh which is passed to be unwelded.

Graphics3D 640,480,16,1

Global LIGHT = CreateLight()
Global CUBE = MESHcull( CreateCube() , C_Top+C_Right )
EntityFX CUBE, 4

Global CAMERA = CreateCamera()
PositionEntity CAMERA, 0,5,-10
PointEntity CAMERA, CUBE


Const C_Front = 1
Const C_Rear = 4
Const C_Left = 8
Const C_Right = 2
Const C_Top = 16
Const C_Bottom = 32

While Not KeyDown(1)

	TurnEntity CUBE, 0,1,0 ;1,0,1
	RenderWorld()
	Flip

Wend

;=====================================================================================
;=====================================================================================
;=====================================================================================

Function MESHcull( Mesh , Flags = 0 )

	If Flags = 0 Return Mesh

	copy = CopyMesh( Mesh )
	s = GetSurface( copy, 1 )
	
	Cs = GetSurface( mesh , 1 )
	ClearSurface Cs, 1, 1

	For t = 0 To CountTriangles( s )-1
		;calculate triangle normal
		v0 = TriangleVertex( s, t, 0 )
		v1 = TriangleVertex( s, t, 1 )
		v2 = TriangleVertex( s, t, 2 )
		ax# = VertexX( s, v1 ) - VertexX( s, v0 )
		ay# = VertexY( s, v1 ) - VertexY( s, v0 )	
		az# = VertexZ( s, v1 ) - VertexZ( s, v0 )	
		bx# = VertexX( s, v2 ) - VertexX( s, v1 )
		by# = VertexY( s, v2 ) - VertexY( s, v1 )	
		bz# = VertexZ( s, v2 ) - VertexZ( s, v1 )	
		Nx# = ( ay * bz ) - ( az * by )
		Ny# = ( az * bx ) - ( ax * bz )
		Nz# = ( ax * by ) - ( ay * bx )
		Ns# = Sqr( Nx * Nx + Ny*Ny + Nz*Nz )
		Nx = Nx / Ns
		Ny = Ny / Ns
		Nz = Nz / Ns
				
		OK = True
		For l = 0 To 5
			Flag = 2^l
			If ( Flags And Flag) = Flag
				;determine plane normal
				Select Flag
					Case C_Front	px# = 0  : py# = 0  : pz# = 1	
					Case C_Rear		px# = 0  : py# = 0  : pz# = -1	
					Case C_Left		px# = -1 : py# = 0  : pz# = 0	
					Case C_Right	px# = 1  : py# = 0  : pz# = 0	
					Case C_Top		px# = 0  : py# = 1  : pz# = 0	
					Case C_Bottom	px# = 0  : py# = -1 : pz# = 0	
				End Select
				If Nx = Px And Ny = Py And Nz = Pz OK = False
			EndIf
		Next
								
		If OK
			nv0 = AddVertex( Cs, VertexX( s , v0 ) , VertexY( s, v0 ) , VertexZ( s, v0 ) )
			nv1 = AddVertex( Cs, VertexX( s , v1 ) , VertexY( s, v1 ) , VertexZ( s, v1 ) )
			nv2 = AddVertex( Cs, VertexX( s , v2 ) , VertexY( s, v2 ) , VertexZ( s, v2 ) )
			VertexNormal Cs , nv0 , Nx, Ny, Nz
			VertexNormal Cs , nv1 , Nx, Ny, Nz
			VertexNormal Cs , nv2 , Nx, Ny, Nz
			AddTriangle( Cs , nv0, nv1, nv2 )
		EndIf
		
	Next
		
	FreeEntity copy
	Return Mesh
		
End Function		


Entityalpha StevieG, 0


Ross C(Posted 2008) [#7]
lmao @ stevie

Your collision responce is still being processed. Best watch yourself.

Oh wait, your making yourself visible. I see. :oS

HideEntity RossC


LamptonWorm(Posted 2008) [#8]
Thanks Stevie G, I'll have a play.

Cheers,
LW.


_33(Posted 2008) [#9]
I'm trying to understand this function StevieG. Is it a function that deals with cubes only?

Cheers, and thanks for any detail on the workings.


Doiron(Posted 2008) [#10]
The cube just looks like a test mesh.
The function copies the original mesh, iterates through all tris, and then returns the newly processed mesh.


Stevie G(Posted 2008) [#11]

I'm trying to understand this function StevieG. Is it a function that deals with cubes only?



It can deal with other shapes but can only cull triangles which have face normals which exactly match one of the 6 culling normals. So, for example if you created a cylinder and wanted to remove the bottom triangles only you would have to apply an unweld function which combines the cylinder into one surface ( blitz normally creates 2 x surfaces ) and then call MESHcull( cyclinder, C_Bottom ). I use it to build meshes in code and remove triangles which aren't seen.

Big10p has a function in the archives which culls triangles based on a rotated plane which would probably be better for you.

Stevie


LamptonWorm(Posted 2009) [#12]
Hi,

Quick question - can the function be adapted to preserve any texture applied to the mesh that gets culled? I tried re-texturing after but no joy (yet).

Cheers!
LW.


Warner(Posted 2009) [#13]
I think that you need to set the UV coordinates for each vertex. You can read them with VertexU and VertexV and set them with VertexTexCoords.
nv0 = AddVertex( Cs, VertexX( s , v0 ), VertexY( s, v0 ), VertexZ( s, v0 ), VertexU(s, v0), VertexV(s, v0) )
nv1 = AddVertex( Cs, VertexX( s , v1 ), VertexY( s, v1 ), VertexZ( s, v1 ), VertexU(s, v1), VertexV(s, v1) )
nv2 = AddVertex( Cs, VertexX( s , v2 ), VertexY( s, v2 ), VertexZ( s, v2 ), VertexU(s, v2), VertexV(s, v2) )



LamptonWorm(Posted 2009) [#14]
Nice - thanks!