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
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