Making Holes in a mesh

Everything related to 3D programming
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Making Holes in a mesh

Post by applePi »

here is how to delete a whole side from a cube made by CreateCube() by bypassing a triangle weaving after certain index. we get first the full data of the cube using GetMeshData function, for the vertex and for the index info ,then when we press D we call the MakeHole procedure , we delete the mesh and then we make a new fresh one then filling the empty mesh with the old cube info wich we have stored previously in MeshData array
then we weave triangles between the vertices exactly as was before using MeshFace with the indices stored before, but here where we bypass weaving specific triangle at the index we want, note that the number of indices for a cube is 36. you can know how the indices are attached to the vertices by pressing Space key.
note that the vertices are numbered according to their appearance in the code using MeshVertexPosition
while the indices are numered according to the appearance of vertices in MeshFace, it is going 0,1,2,3,4...etc. so every vertex can share several indices. every 3 indices are defining one triangle, this is why 36/3 = 12 is the number of triangles in the cube.
Press D to delete a cube side, then the sphere will be able to fall down under gravity
after you make a hole press S to save the new cube, and to be sure it is a real cube with a hole, comment the CreateCube(0, 40) and replace it with LoadMesh(0, "cube2.mesh")
for more usages of MeshIndex() look example MeshManual2.pb in the examples

Code: Select all

Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)

Global x.f = 0
Global y.f = 10
Global z.f = -30
Global vertexTotal, indexTotal
ExamineDesktops()
If OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "press... D to Delete a cube side,.. W to toggle WireFrame,... Space to show every index attached to what vertex", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)


Define.f KeyX, KeyY
Declare MakeHole()

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
  Add3DArchive(".", #PB_3DArchive_FileSystem)
  
  InitSprite()
  InitKeyboard()
  OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0)-60, 0, 0, 0)
  
CreateMaterial(0, LoadTexture(0, "white.jpg"))
  
CreateMaterial(3, LoadTexture(3, "wood.jpg"))
MaterialBlendingMode(3, #PB_Material_Color)
SetMaterialColor(3, #PB_Material_DiffuseColor, RGBA(255, 255, 255, 100))
MaterialCullingMode(3, #PB_Material_NoCulling)
MaterialBlendingMode(3, #PB_Material_AlphaBlend)
    
  
  ;LoadMesh(0, "cube2.mesh")
  CreateCube(0,40)
  CreateEntity(0, MeshID(0), MaterialID(3))
  
  GetMeshData(0,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate | #PB_Mesh_Normal , 0, MeshVertexCount(0)-1)
  GetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(0, 0)-1)
  vertexTotal = MeshVertexCount(0)
  indexTotal  = MeshIndexCount(0)
  ;Debug vertexTotal
  ;Debug indexTotal
  
  
  EntityPhysicBody(0, #PB_Entity_StaticBody, 1, 0.4, 1)
            
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 5, 110, #PB_Absolute)
    ;CameraFOV(0, 70)
    ;CameraBackColor(0, $330000)
    CameraLookAt(0,0,5,0)
        
    CreateLight(0, RGB(255,255,255), 0, 0, 20, #PB_Light_Directional  )
    LightLookAt(0, 0, 0, -10)

    AmbientColor(RGB(255, 255, 255))
    
    CreateSphere(5, 4)
    CreateEntity(5, MeshID(5), MaterialID(0) ,0, 5, 0)
    EntityPhysicBody(5, #PB_Entity_SphereBody, 1, 0.4, 1)
    wire = 1 ; to toggle wire or solid Frame
    Repeat
      Event = WindowEvent()
                  
      If ExamineKeyboard()
        
        
        If KeyboardReleased(#PB_Key_W)
          
          If wire
            MaterialShadingMode(0, #PB_Material_Wireframe)
            MaterialShadingMode(3, #PB_Material_Wireframe)
            
          wire ! 1
          Else 
            MaterialShadingMode(0, #PB_Material_Solid)
            MaterialShadingMode(3, #PB_Material_Solid)
            
            wire ! 1
        EndIf
      EndIf
        
      If KeyboardReleased(#PB_Key_D)
          ApplyEntityImpulse(5, 0, 0.01, 0 )
          MakeHole()
        ElseIf KeyboardReleased(#PB_Key_S)
          SaveMesh(0, "cube2.mesh")
        ElseIf KeyboardReleased(#PB_Key_Space)
          
          For i=0 To indexTotal-1
          vx = MeshData(MeshDataInd(i)\Index)\x
          vy = MeshData(MeshDataInd(i)\Index)\y
          vz = MeshData(MeshDataInd(i)\Index)\z
          
          Debug "index "+Str(indx) +"  = " + Str(vx)+"  "+Str(vy)+"  " +Str(vz)+"  "
          indx + 1
        Next
      EndIf
        
      EndIf
      rot.f+0.6
      RotateEntity(0,rot,rot,rot)
      ;RotateEntity(0,0,0,rot)
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

Procedure MakeHole()
  FreeEntity(0)
  FreeMesh(0)
      
      CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
      For i=0 To vertexTotal-1
        MeshVertexPosition(MeshData(i)\x, MeshData(i)\y, MeshData(i)\z)
        MeshVertexTextureCoordinate(u, v)
        u+0.01:v+0.01
        ;MeshVertexColor(RGB(Random(255), Random(255), Random(255)))
      Next
      
      For i=0 To indexTotal-1 Step 3
        If i < 30
          MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index, MeshDataInd(i+2)\Index)
          
      EndIf   
      Next
 
      FinishMesh(#True)
      UpdateMeshBoundingBox(0)

      SetMeshMaterial(0, MaterialID(3))
   CreateEntity(0, MeshID(0), MaterialID(3))
      
  SetMeshData(0,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate | #PB_Mesh_Normal | #PB_Mesh_Color , 0, MeshVertexCount(0)-1)
  SetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(0)-1)
  
  EntityPhysicBody(0, #PB_Entity_StaticBody, 1, 0.4, 1)  
EndProcedure

here is another way to do the job more suitable for the study of the subject since it contains a few vertices and faces, just press D to let the sphere go down

Code: Select all

Enumeration
   #MESH
   #LIGHT
   #CAMERA
   #mainwin
 EndEnumeration


Global x.f = 0
Global y.f = 10
Global z.f = -30
Global DeleteFace = 0
ExamineDesktops()
If OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "press... D to delete the front triangle, ... W ..to toggle wire /solid Frame ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)


Define.f KeyX, KeyY
Declare MakeHole()
Declare CreateMatrix()
Declare DrawMatrix()

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
  Add3DArchive(".", #PB_3DArchive_FileSystem)
  
  InitSprite()
  InitKeyboard()
  OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0)-60, 0, 0, 0)
    
  CreateMaterial(3, LoadTexture(3, "white.jpg"))
DisableMaterialLighting(3, #True)  
MaterialBlendingMode(3, #PB_Material_Color)
SetMaterialColor(3, #PB_Material_DiffuseColor, RGBA(255, 255, 255, 100))
MaterialCullingMode(3, #PB_Material_NoCulling)
    

    CreateMatrix()
            
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 30, 30, #PB_Absolute)
    CameraFOV(0, 70)
    ;CameraBackColor(0, $330000)
    CameraLookAt(0,0,5,0)
        
    CreateLight(0, RGB(255,255,255), 0, 0, 20, #PB_Light_Directional  )
    LightLookAt(0, 0, 0, -10)

    AmbientColor(RGB(255, 255, 255))
    
    CreateSphere(5, 3)
    CreateEntity(5, MeshID(5), MaterialID(3) ,0, 3, -5)
    EntityPhysicBody(5, #PB_Entity_SphereBody, 1, 0.4, 1)
    wire = 1
    Repeat
      Event = WindowEvent()
                  
      If ExamineKeyboard()
        
        If KeyboardReleased(#PB_Key_W)
          If wire
            MaterialShadingMode(3, #PB_Material_Wireframe)
            
          wire ! 1
          Else 
            MaterialShadingMode(3, #PB_Material_Solid)
            
            wire ! 1
        EndIf
      EndIf
        
      If KeyboardReleased(#PB_Key_D)
          ApplyEntityImpulse(5, 0, 0.01, 0 )
          MakeHole()
        
        EndIf
        
      EndIf
      rot.f+0.6
      RotateEntity(0,rot,rot,0)
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End


Procedure DrawMatrix()
    MeshVertexPosition(-10, 0, 0) ;vertex 0
    MeshVertexColor(RGB(255, 0, 0))
    MeshVertexPosition(10, 0, 0)  ; vertex 1
    MeshVertexColor(RGB(0, 255, 0))
    MeshVertexPosition(0, 0, -10)   ; vertex 2
    MeshVertexColor(RGB(255, 0, 255))
    MeshVertexPosition(0, 10,-5)     ; vertex 3
    MeshVertexColor(RGB(255, 255, 0))
        
    MeshFace(0, 1, 2)
    MeshFace(1, 2, 3)
    If DeleteFace = 0
      MeshFace(1, 0, 3)
    EndIf
    
    MeshFace(0, 3, 2)
    
EndProcedure  
Procedure CreateMatrix()
  
  CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
  DrawMatrix()
  FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(3))
    
  CreateEntity(0, MeshID(0), #PB_Material_None)
  RotateEntity(0, 20,0,0)
  ScaleEntity(0, 2, 2, 2)
  EntityPhysicBody(0, #PB_Entity_StaticBody, 1, 0.4, 1)
EndProcedure


Procedure MakeHole()
  FreeEntity(0)
  FreeMesh(0)
      
  DeleteFace = 1
  CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
  DrawMatrix()
  FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(3))
    
  CreateEntity(0, MeshID(0),  #PB_Material_None)
  RotateEntity(0, 20,0,0)
  ScaleEntity(0, 2, 2, 2)
  EntityPhysicBody(0, #PB_Entity_StaticBody, 1, 0.4, 1)
EndProcedure
Last edited by applePi on Sat Aug 23, 2014 10:46 am, edited 3 times in total.
User avatar
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Making Holes in a mesh

Post by falsam »

Nice these examples. Thank you. :)

➽ Windows 11 64-bit - PB 6.0 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Making Holes in a mesh

Post by applePi »

thank you falsam, here is how to make half sphere created from CreateSphere()
Image
here we deleted all indexes above 816.
the same code as above, it seems this approach are universal, the only problem is to know which triangle correspond to which index
PS: if we change line 163 to If i < 1530 , when pressing D we will have a sphere with a small hole at the bottom, the challenge is to let the small ball to pass through that hole

Code: Select all

Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)

Global x.f = 0
Global y.f = 10
Global z.f = -30
Global vertexTotal, indexTotal
ExamineDesktops()
If OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "press... D to Delete a cube side,.. W to toggle WireFrame,... Space to show every index attached to what vertex", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)


Define.f KeyX, KeyY
Declare MakeHole()

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
  Add3DArchive(".", #PB_3DArchive_FileSystem)
  
  InitSprite()
  InitKeyboard()
  OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0)-10, 0, 0, 0)
  
CreateMaterial(0, LoadTexture(0, "white.jpg"))
  
CreateMaterial(3, LoadTexture(3, "MRAMOR6X6.jpg"))
MaterialBlendingMode(3, #PB_Material_Color)
SetMaterialColor(3, #PB_Material_DiffuseColor, RGBA(255, 255, 255, 100))
MaterialCullingMode(3, #PB_Material_NoCulling)
;MaterialBlendingMode(3, #PB_Material_AlphaBlend)
    
  
  ;LoadMesh(0, "cube2.mesh")
  CreateSphere(0,40)
  CreateEntity(0, MeshID(0), MaterialID(3))
  
  GetMeshData(0,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate | #PB_Mesh_Normal , 0, MeshVertexCount(0)-1)
  GetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(0, 0)-1)
  vertexTotal = MeshVertexCount(0)
  indexTotal  = MeshIndexCount(0)
  ;Debug vertexTotal
  ;Debug indexTotal
  
  
  EntityPhysicBody(0, #PB_Entity_StaticBody, 1, 0.4, 1)
            
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 5, 110, #PB_Absolute)
    ;CameraFOV(0, 70)
    ;CameraBackColor(0, $330000)
    CameraLookAt(0,0,5,0)
        
    CreateLight(0, RGB(255,255,255), 0, 0, 20, #PB_Light_Directional  )
    LightLookAt(0, 0, 0, -10)

    AmbientColor(RGB(255, 255, 255))
    
    CreateSphere(5, 4)
    CreateEntity(5, MeshID(5), MaterialID(0) ,0, 5, 0)
    EntityPhysicBody(5, #PB_Entity_SphereBody, 1, 0.4, 1)
    wire = 1 ; to toggle wire or solid Frame
    Repeat
      Event = WindowEvent()
                  
      If ExamineKeyboard()
        
        
        If KeyboardReleased(#PB_Key_W)
          
          If wire
            MaterialShadingMode(0, #PB_Material_Wireframe)
            MaterialShadingMode(3, #PB_Material_Wireframe)
            
          wire ! 1
          Else 
            MaterialShadingMode(0, #PB_Material_Solid)
            MaterialShadingMode(3, #PB_Material_Solid)
            
            wire ! 1
        EndIf
      EndIf
        
      If KeyboardReleased(#PB_Key_D)
          ApplyEntityImpulse(5, 0, 0.01, 0 )
          MakeHole()
        ElseIf KeyboardReleased(#PB_Key_S)
          SaveMesh(0, "cube2.mesh")
        ElseIf KeyboardReleased(#PB_Key_Space)
          
          For i=0 To indexTotal-1
          vx = MeshData(MeshDataInd(i)\Index)\x
          vy = MeshData(MeshDataInd(i)\Index)\y
          vz = MeshData(MeshDataInd(i)\Index)\z
          
          Debug "index "+Str(indx) +"  = " + Str(vx)+"  "+Str(vy)+"  " +Str(vz)+"  "
          indx + 1
        Next
      EndIf
        
      EndIf
      rot.f+0.6
      RotateEntity(0,rot,rot,rot)
      ;RotateEntity(0,0,0,rot)
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

Procedure MakeHole()
  FreeEntity(0)
  FreeMesh(0)
      
      CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
      For i=0 To vertexTotal-1
        MeshVertexPosition(MeshData(i)\x, MeshData(i)\y, MeshData(i)\z)
        MeshVertexTextureCoordinate(u, v)
        u+0.01:v+0.01
        ;MeshVertexColor(RGB(Random(255), Random(255), Random(255)))
      Next
      
      For i=0 To indexTotal-1 Step 3
        If i < 816
          MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index, MeshDataInd(i+2)\Index)
          
      EndIf   
      Next
 
      FinishMesh(#True)
      UpdateMeshBoundingBox(0)

      SetMeshMaterial(0, MaterialID(3))
   CreateEntity(0, MeshID(0), MaterialID(3))
      
  SetMeshData(0,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate | #PB_Mesh_Normal | #PB_Mesh_Color , 0, MeshVertexCount(0)-1)
  SetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(0)-1)
  
  EntityPhysicBody(0, #PB_Entity_StaticBody, 1, 0.4, 1)  
EndProcedure

Last edited by applePi on Sat Aug 23, 2014 10:47 am, edited 3 times in total.
User avatar
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Making Holes in a mesh

Post by falsam »

Thanks applePi. These codes are part of my collection of codes. Very useful :)

➽ Windows 11 64-bit - PB 6.0 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect.
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Making Holes in a mesh

Post by DK_PETER »

Very nice examples applePi. ;-)
applePi wrote:the challenge is to let the small ball to pass through that hole
Change the WorldGravity() a bit and the ball will behave more like a ball than a 'heavy' balloon.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Making Holes in a mesh

Post by applePi »

thanks falsam and DK_PETER , just i want to announce: some forgotten unnecessary procedures are removed from the above first and third examples which makes a hole in a cube made from CreateCube and CreateSphere, now the code are too short.
Last edited by applePi on Sat Aug 23, 2014 10:47 am, edited 1 time in total.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Making Holes in a mesh

Post by IdeasVacuum »

The hole making is possible because we 'know' how the mesh shape is constructed and it's geometrics, plus we are removing triangles logically. The big challenge is allowing a user-defined hole shape to be cut from any mesh shape..... :twisted:
Image
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Making Holes in a mesh

Post by applePi »

that seems mission impossible for me IdeasVacuum, there is in the latest MP3D library an example in which we click a sphere by mouse we delete a triangle using such line:
MP_FreeTriangle(pickedmesh, triangleindex)
Image

but i have loaded instead of a sphere a "car.x" model and when clicking on it it is destroyed completely. there is in the PB examples mousePick.pb PointPick.pb examples. it is may be we can destroy a single triangle in a sphere using the mouse . haven't tried it yet.
how the modeling software do it !!, remains a mystery, it is possible they consider any model a sphere but distorted topologically !!. this seems a whole science alone.
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: Making Holes in a mesh

Post by Samuel »

IdeasVacuum wrote:The big challenge is allowing a user-defined hole shape to be cut from any mesh shape..... :twisted:
That is pretty scary, but there's an even bigger challenge in it. Which is to allow a user-defined hole that doesn't take several minutes to generate.
At least in my past experiences it was.
marc_256
Enthusiast
Enthusiast
Posts: 751
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: Making Holes in a mesh

Post by marc_256 »

@applePi

very nice work, like always ...

@IdeasVacuum

Yes, the challenge ...

my latest work ? :mrgreen:

Image

easyyyyyy :mrgreen: :mrgreen: :mrgreen: :mrgreen: :mrgreen:
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Making Holes in a mesh

Post by applePi »

more variations and some fun, let a worm to eat the house from triangle 0 and up , press Space key and any time press S to save the model as "house.mesh" with its latest status.
i have used a procedure DeleteTriangle()
if you want to delete the house from the bottom up then uncomment lines 124, 139
Image

Code: Select all

#plane = 23
Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)

Global x.f = 0
Global y.f = 10
Global z.f = -30
Global vertexTotal, indexTotal
Global face.l, tr.l, tr1.l
ExamineDesktops()
If OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "press ... Space to Delete triangles,.... W to toggle WireFrame", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)


Define.f KeyX, KeyY
Declare DeleteTriangle()

If InitEngine3D()
  
  Add3DArchive(".", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/models", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data\Scripts", #PB_3DArchive_FileSystem)
  
  
  InitSprite()
  InitKeyboard()
  OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0)-5, 0, 0, 0)
  
  CreateMaterial(0, LoadTexture(0, "white.jpg"))
  CreateMaterial(1, LoadTexture(1, "terrain_texture.jpg"))
  
CreateMaterial(3, LoadTexture(3, "fw12b.jpg"))
;MaterialBlendingMode(3, #PB_Material_Color)
;SetMaterialColor(3, #PB_Material_DiffuseColor, RGBA(255, 255, 255, 100))
MaterialCullingMode(3, #PB_Material_NoCulling)
;MaterialBlendingMode(3, #PB_Material_AlphaBlend)
  
;CreateCube(0,40)
;CreateSphere(0,40)
  LoadMesh(0, "tudorhouse.mesh")
  CreateEntity(0, MeshID(0), MaterialID(3))
  
  GetMeshData(0,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate | #PB_Mesh_Normal , 0, MeshVertexCount(0)-1)
  GetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(0, 0)-1)
  vertexTotal = MeshVertexCount(0)
  indexTotal  = MeshIndexCount(0)
  ;Debug vertexTotal
  ;Debug indexTotal
  
  ;EntityPhysicBody(0, #PB_Entity_ConvexHullBody , 1, 0.4, 1)
  EntityPhysicBody(0, #PB_Entity_StaticBody , 1, 0.4, 1)
  
CreatePlane(#plane, 1000, 2000, 1, 1, 1, 1)
CreateEntity (#plane, MeshID(#plane), MaterialID(0), 0,-600,0)
;EntityPhysicBody(#plane, #PB_Entity_StaticBody , 1, 0.4, 1)


    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 100, 2000, #PB_Absolute)
    ;CameraFOV(0, 70)
    ;CameraBackColor(0, $330000)
    CameraLookAt(0,0,-10,0)
        
    CreateLight(0, RGB(255,255,255), 0, 0, 20, #PB_Light_Directional  )
    LightLookAt(0, 0, 0, -10)

    AmbientColor(RGB(255, 255, 255))
 
    wire = 1 ; to toggle wire or solid Frame
    WorldGravity(-100)
    
    ;WorldDebug(#PB_World_DebugBody)
      Repeat
      Event = WindowEvent()
                  
      If ExamineKeyboard()
        
        
        If KeyboardReleased(#PB_Key_W)
          
          If wire
            MaterialShadingMode(0, #PB_Material_Wireframe)
            MaterialShadingMode(3, #PB_Material_Wireframe)
            
          wire ! 1
          Else 
            MaterialShadingMode(0, #PB_Material_Solid)
            MaterialShadingMode(3, #PB_Material_Solid)
            
            wire ! 1
        EndIf
      EndIf
        
      
        If KeyboardReleased(#PB_Key_S)
          SaveMesh(0, "house.mesh")
        EndIf
        
        If KeyboardPushed(#PB_Key_Space) 
          
          DeleteTriangle()
          
        EndIf
        
        
      EndIf
      rot.f+0.6
      ;RotateEntity(0,rot,rot,rot)
      RotateEntity(0,0,rot,0)
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

Procedure DeleteTriangle()
  ;indexTotal  = MeshIndexCount(0)
  FreeEntity(0)
      FreeMesh(0)
            
      CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
      For i=0 To vertexTotal-1
        MeshVertexPosition(MeshData(i)\x, MeshData(i)\y, MeshData(i)\z)
        MeshVertexTextureCoordinate(u, v)
        u+0.01:v+0.01
        
      Next
      wer.l=0
      
      
      For i=tr1 To indexTotal-3 Step 3
        ;For i=3 To indexTotal-3 Step 3
      
                  
          MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index, MeshDataInd(i+2)\Index)
          

      Next
      tr1+3
      
      FinishMesh(#True)
      UpdateMeshBoundingBox(0)

      SetMeshMaterial(0, MaterialID(3))
   CreateEntity(0, MeshID(0), MaterialID(3))
      
  SetMeshData(0,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate | #PB_Mesh_Normal | #PB_Mesh_Color , 0, MeshVertexCount(0)-1)
  SetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(0)-1)
      
  ;EntityPhysicBody(0, #PB_Entity_ConvexHullBody, 1, 0.4, 1)  
  ;EntityPhysicBody(0, #PB_Entity_StaticBody, 1, 0.4, 1)  
  
EndProcedure


the next code show the sphere eaten from its bottom, since we have used the continuously decreased index, the sphere have #PB_Entity_ConvexHullBody note that its physics body stay the same unlike the house in the previous example which have a staticBody. this is what i don't understand.

Code: Select all

#plane = 23
Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)

Global x.f = 0
Global y.f = 10
Global z.f = -30
Global vertexTotal, indexTotal
Global face.l, tr.l, tr1.l
ExamineDesktops()
If OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "press ... Space to Delete triangles,.... W to toggle WireFrame", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)


Define.f KeyX, KeyY
Declare DeleteTriangle()

If InitEngine3D()
  
  Add3DArchive(".", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/models", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data\Scripts", #PB_3DArchive_FileSystem)
  
  
  InitSprite()
  InitKeyboard()
  OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0)-5, 0, 0, 0)
  
  CreateMaterial(0, LoadTexture(0, "white.jpg"))
    
CreateMaterial(3, LoadTexture(3, "dirt.jpg"))
MaterialBlendingMode(3, #PB_Material_Color)
SetMaterialColor(3, #PB_Material_DiffuseColor, RGBA(255, 255, 255, 100))
MaterialCullingMode(3, #PB_Material_NoCulling)
MaterialBlendingMode(3, #PB_Material_AlphaBlend)
  
;CreateCube(0,40)
CreateSphere(0,40)
  ;LoadMesh(0, "sphere.mesh")
  CreateEntity(0, MeshID(0), MaterialID(3))
  
  GetMeshData(0,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate | #PB_Mesh_Normal , 0, MeshVertexCount(0)-1)
  GetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(0, 0)-1)
  vertexTotal = MeshVertexCount(0)
  indexTotal  = MeshIndexCount(0)
  ;Debug vertexTotal
  ;Debug indexTotal
  
  EntityPhysicBody(0, #PB_Entity_ConvexHullBody , 1, 0.4, 1)
  ;EntityPhysicBody(0, #PB_Entity_StaticBody , 1, 0.4, 1)
  
CreatePlane(#plane, 1000, 2000, 1, 1, 1, 1)
CreateEntity (#plane, MeshID(#plane), MaterialID(0), 0,-100,0)
EntityPhysicBody(#plane, #PB_Entity_StaticBody , 1, 0.4, 1)


    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 100, 200, #PB_Absolute)
    ;CameraFOV(0, 70)
    ;CameraBackColor(0, $330000)
    CameraLookAt(0,0,-10,0)
        
    CreateLight(0, RGB(255,255,255), 0, 0, 20, #PB_Light_Directional  )
    LightLookAt(0, 0, 0, -10)

    AmbientColor(RGB(255, 255, 255))
 
    wire = 1 ; to toggle wire or solid Frame
    WorldGravity(-100)
    ;WorldDebug(#PB_World_DebugEntity)
    WorldDebug(#PB_World_DebugBody)
      Repeat
      Event = WindowEvent()
                  
      If ExamineKeyboard()
        
        
        If KeyboardReleased(#PB_Key_W)
          
          If wire
            MaterialShadingMode(0, #PB_Material_Wireframe)
            MaterialShadingMode(3, #PB_Material_Wireframe)
            
          wire ! 1
          Else 
            MaterialShadingMode(0, #PB_Material_Solid)
            MaterialShadingMode(3, #PB_Material_Solid)
            
            wire ! 1
        EndIf
      EndIf
        
      
        If KeyboardReleased(#PB_Key_S)
          SaveMesh(0, "sphere.mesh")
        EndIf
        
        If KeyboardPushed(#PB_Key_Space) 
                    
          If indexTotal>9
          DeleteTriangle()
        Else
          Debug "you have deleted all the triangles except one, we keep"
          Debug "one triangle to prevent program from crashing"
        EndIf
        EndIf
        
        
      EndIf
      rot.f+0.6
      ;RotateEntity(0,rot,rot,rot)
      RotateEntity(0,0,rot,0)
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

Procedure DeleteTriangle()
  indexTotal  = MeshIndexCount(0)
  FreeEntity(0)
      FreeMesh(0)
            
      CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
      For i=0 To vertexTotal-1
        MeshVertexPosition(MeshData(i)\x, MeshData(i)\y, MeshData(i)\z)
        MeshVertexTextureCoordinate(u, v)
        u+0.01:v+0.01
        
      Next
      wer.l=0
      
      ;For i=tr1 To indexTotal-3 Step 3
        For i=3 To indexTotal-3 Step 3
      
        
          MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index, MeshDataInd(i+2)\Index)
          
        
      Next
      ;tr1+3
      
      FinishMesh(#True)
      UpdateMeshBoundingBox(0)

      SetMeshMaterial(0, MaterialID(3))
   CreateEntity(0, MeshID(0), MaterialID(3), 0,-60,0)
      
  SetMeshData(0,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate | #PB_Mesh_Normal | #PB_Mesh_Color , 0, MeshVertexCount(0)-1)
  SetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(0)-1)
      
  EntityPhysicBody(0, #PB_Entity_ConvexHullBody, 1, 0.4, 1)  
  ;EntityPhysicBody(0, #PB_Entity_StaticBody, 1, 0.4, 1)  
  
EndProcedure

IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Making Holes in a mesh

Post by IdeasVacuum »

ApplePi, you are very creative, I always look forward to seeing posts from you. 8)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Making Holes in a mesh

Post by IdeasVacuum »

Marc - hidden line view of an assembly. That's advanced stuff. Do you use PB Ogre or have you found an engine that is more suited to engineering?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
marc_256
Enthusiast
Enthusiast
Posts: 751
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: Making Holes in a mesh

Post by marc_256 »

hi IdeasVacuum,

Now, I'm writing my own 'hidden line' routines,
I use 3 different approaches, (from my Apple II time - but very fast now)
to be honest, I have still some bugs, so I need to correct some details with image editor. :oops:
depends on the viewing angle.

marc,
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Making Holes in a mesh

Post by IdeasVacuum »

.... I think (I'm certainly not an Ogre expert), using the correct Shader, Ogre can do hidden line.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply