October 24, 2021, 04:08:31

Author Topic: [bb] Removing Triangles and Vertices by Mr Snidesmin [ 1+ years ago ]  (Read 795 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Removing Triangles and Vertices
Author : Mr Snidesmin
Posted : 1+ years ago

Description : A few useful functions. Note: they will probably be slow for large meshes.

RemoveTriangle(surface%, triangle_index%)
-removes specified triangle from a surface

RemoveVertex(surface%, vertex_index%)
-removes specified vertex from a surface

VertexIsUsed(surface%, vi%)
-returns true if vertex index is used in a triangle.

ClearUnusedVertices%(surface%)
-removes unused vertices in a surface


Code :
Code: BlitzBasic
  1. Function VertexIsUsed(surface%, vi%)
  2.         For i% = 0 To CountTriangles(surface) - 1
  3.                 If TriangleVertex(surface, i, 0) = vi Then Return True
  4.                 If TriangleVertex(surface, i, 1) = vi Then Return True
  5.                 If TriangleVertex(surface, i, 2) = vi Then Return True
  6.         Next
  7.         Return False
  8. End Function
  9.  
  10. Function ClearUnusedVertices%(surface%)
  11.         For i% = 0 To CountVertices(surface) - 1
  12.                 If Not VertexIsUsed(surface, i) Then
  13.                         RemoveVertex surface, i
  14.                 End If
  15.         Next
  16. End Function
  17.  
  18.  
  19. Function RemoveTriangle(surface%, triangle_index%)
  20.         s0% = surface
  21.         m% = CreateMesh()
  22.         s% = CreateSurface(m)
  23.        
  24.         ;copy, excluding triangle:
  25.         For i% = 0 To CountVertices(s0)-1
  26.                 vi% = AddVertex(s, VertexX(s0, i),VertexY(s0, i),VertexZ(s0, i),VertexU(s0, i),VertexV(s0, i),VertexW(s0, i))
  27.                 VertexNormal s, vi, VertexNX(s0, i), VertexNY(s0, i), VertexNZ(s0, i)
  28.                 VertexColor s, vi, VertexRed(s0, i), VertexGreen(s0, i), VertexBlue(s0, i), VertexAlpha(s0, i)
  29.         Next
  30.         For i% = 0 To CountTriangles(s0)-1
  31.                 If i <> triangle_index Then
  32.                         AddTriangle s, TriangleVertex(s0, i, 0), TriangleVertex(s0, i, 1), TriangleVertex(s0, i, 2)
  33.                 End If
  34.         Next
  35.        
  36.         ;copy back
  37.         CopySurface s, s0
  38.         FreeEntity m
  39. End Function
  40.  
  41.  
  42. Function RemoveVertex(surface%, vertex_index%)
  43.         s0% = surface
  44.         m% = CreateMesh()
  45.         s% = CreateSurface(m)
  46.        
  47.         ;copy, excluding vertex:
  48.         For i% = 0 To CountVertices(s0)-1
  49.                 If i <> vertex_index Then
  50.                         vi% = AddVertex(s, VertexX(s0, i),VertexY(s0, i),VertexZ(s0, i),VertexU(s0, i),VertexV(s0, i),VertexW(s0, i))
  51.                         VertexNormal s, vi, VertexNX(s0, i), VertexNY(s0, i), VertexNZ(s0, i)
  52.                         VertexColor s, vi, VertexRed(s0, i), VertexGreen(s0, i), VertexBlue(s0, i), VertexAlpha(s0, i)
  53.                 End If
  54.         Next
  55.         For i% = 0 To CountTriangles(s0)-1
  56.                 v0% = TriangleVertex(s0, i, 0)
  57.                 v1% = TriangleVertex(s0, i, 1)
  58.                 v2% = TriangleVertex(s0, i, 2)
  59.                
  60.                 If v0 > vertex_index Then v0 = v0 - 1
  61.                 If v1 > vertex_index Then v1 = v1 - 1
  62.                 If v2 > vertex_index Then v2 = v2 - 1
  63.                 AddTriangle s, v0, v1, v2
  64.         Next
  65.        
  66.         ;copy back
  67.         CopySurface s, s0
  68.         FreeEntity m
  69. End Function
  70.  
  71.  
  72.  
  73. Function CopySurface(source%, dest%, clear_unused_vertices%=False)
  74.         s0% = source
  75.         s% = dest
  76.         ClearSurface s
  77.        
  78.         For i% = 0 To CountVertices(s0)-1
  79.                 vi% = AddVertex(s, VertexX(s0, i),VertexY(s0, i),VertexZ(s0, i),VertexU(s0, i),VertexV(s0, i),VertexW(s0, i))
  80.                 VertexNormal s, vi, VertexNX(s0, i), VertexNY(s0, i), VertexNZ(s0, i)
  81.                 VertexColor s, vi, VertexRed(s0, i), VertexGreen(s0, i), VertexBlue(s0, i), VertexAlpha(s0, i)
  82.         Next
  83.         For i% = 0 To CountTriangles(s0)-1
  84.                 AddTriangle s, TriangleVertex(s0, i, 0), TriangleVertex(s0, i, 1), TriangleVertex(s0, i, 2)
  85.         Next
  86. End Function


Comments :


jfk EO-11110(Posted 1+ years ago)

 Unfortunately, as far as I see this works only with meshes with one single surface, right? all surfaces should be cloned and added to the recreated mesh instead.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal