Add Mesh to Mesh to make a compound Mesh

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

Add Mesh to Mesh to make a compound Mesh

Post by applePi »

EDIT: falsam have provided here http://www.purebasic.fr/english/viewtop ... 28#p458028 AddMesh Procedure which can add also complex meshes with submeshes, easy to use and straightforward , so use it in all your meshes additions. Oct/12/2016

i have found a work around the problem described by falsam here
http://forums.purebasic.fr/english/view ... 36&t=56083
in concise there is a table (ground) and added to it 4 walls (all made from scaled cubes) with AttachEntityObject(...) function. and we enable physics to the all objects. when we rotate the table we see that the sphere with enough speed will go through the walls.
several users have reported this phenomena.
there is a solution even it needs a more coding, but it is automatic.
i will construct the same table and walls like this:
1-
CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
CreateCube(30,10)
CopyMesh(30,31)
; make a copy of the original cube mesh
2- scale the mesh as you want with TransformMesh: such as:
TransformMesh(31,0,0,0, 7.6,1,7.6,0,0,0)
from doc: TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
3- GetMeshData into the Arrays MeshData and MeshDataInd (created previously)
and don't forget to use the switch #PB_Mesh_UVCoordinate
4- to make the ground, use this:

Code: Select all

ArrSize = ArraySize(MeshData())
;main mesh: the ground or the Table
   For c=0 To ArrSize
      
      x.f = MeshData(c)\x 
      y.f = MeshData(c)\y
      z.f = MeshData(c)\z
      MeshVertexPosition(x,y,z)
      MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v) 
         
   Next   
   
   ArrSizeInd = ArraySize(MeshDataInd()) 
   For i=0 To ArrSizeInd Step 3
     MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
   Next
and don't forget to add:
AddSubMesh(#PB_Mesh_TriangleList)
5- continue to the next Left wall and any object you want, and finally use FinishMesh(#True)
and CreateEntity(...)
and so you have in reality a One Mesh with strong physics if enabled.
note that AddSubMesh(#PB_Mesh_TriangleList) will add submesh with a number above the the parent mesh 0 so here the last submesh is the big sphere sticky on the ground and its number is 5. the texturing can be made for every submesh alone such as SetEntityMaterial(0,MaterialID(3),2) ;will texture the submesh 2 Right Wall with materialId 3
Note: don't choose very small sphere since it will penetrate the other objects whether it is made using this method or not.
also it is necessary to add:
DisableEntityBody(#sphere, #False)
ApplyEntityImpulse(#sphere, 0, 0, 0 )

at the moment when you rotate the table, to keep the sphere alive
Note: now when you save the Mesh you will save all the compound structure, press 'S' to save the mesh to "compound.mesh", look at it in a mesh viewer and you will see it like what you have already designed.

workes only in PB 5.50 beta 1 and later , will work in 5.42 but with one texture for all

Code: Select all

    Enumeration
       
       #LIGHT
       #CAMERA
       #mainwin
       #Plane
      #sphere
     
    EndEnumeration

    Structure vector3d
      x.f
      y.f
      z.f
    EndStructure

    Structure vertex
      x.f
      y.f
      z.f
    EndStructure


    Define.f KeyX, KeyY, MouseX, MouseY

    Global Dim MeshData.PB_MeshVertex(0)
    Global Dim MeshDataInd.PB_MeshFace(0)
    Global Dim MeshData2.PB_MeshVertex(0)
    Global Dim MeshDataInd2.PB_MeshFace(0)
    ExamineDesktops()
    If OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "press Z / X to rotate Ground left/Right, ... use mouse + Arrows to rotate + move Camera", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

    Define.f KeyX, KeyY

    Declare CreateMatrix()


    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/3D/Data/Scripts",#PB_3DArchive_FileSystem)
       
      Parse3DScripts()
     
      InitSprite()
      InitKeyboard()
      InitMouse()
      OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)
       
     
        CreateMaterial(0, LoadTexture(0, "White.jpg"))
        DisableMaterialLighting(0, #True)
        MaterialShadingMode(0, #PB_Material_Wireframe     )
        MaterialCullingMode(0, #PB_Material_NoCulling)
               
        CreateCamera(#camera, 0, 0, 100, 100)
        MoveCamera(#camera, 0, 70, 80, #PB_Absolute)
        CameraFOV(#camera, 70)
        CameraBackColor(#camera, RGB(255,200,200))
        CameraLookAt(#camera,0,0,0)
           
        CreateLight(0, RGB(255,255,255), 0, 20, 30)
        AmbientColor(RGB(200, 200, 200))
       
        CreateMaterial(3, LoadTexture(3, "snow_1024.jpg"))
        DisableMaterialLighting(3, #False)
        SetMaterialColor(3, #PB_Material_AmbientColor, RGB(250, 255, 0))
        SetMaterialColor(3, #PB_Material_SpecularColor, RGB(255, 255, 0))
       
        CreateMaterial(1, LoadTexture(1, "MRAMOR6X6.jpg"))
        MaterialCullingMode(1, #PB_Material_NoCulling)

        CreateMaterial(2, LoadTexture(2, "RustySteel.jpg"))
        MaterialCullingMode(2, #PB_Material_NoCulling)
       
        CreateMaterial(4, LoadTexture(4, "ground_diffuse.png"))
        MaterialCullingMode(4, #PB_Material_NoCulling)
       
        CreateMaterial(5, LoadTexture(5, "Geebee2.bmp"))
        MaterialCullingMode(5, #PB_Material_NoCulling)
       
        CreateMatrix()
     
        SetEntityMaterial(0,MaterialID(5),5) ;the sphere sticky on the ground
        SetEntityMaterial(0,MaterialID(1),0) ;Ground
        SetEntityMaterial(0,MaterialID(3),1) ;Left wall
        SetEntityMaterial(0,MaterialID(3),2) ;Right wall
       
        WorldGravity(-20)
           
        CreateSphere(#sphere, 3)
        CreateEntity(#sphere, MeshID(#sphere), MaterialID(5), 0, 60, 4)
        CreateEntityBody(#sphere, #PB_Entity_SphereBody, 0.5,0.5,0.2)
               
        Repeat
          Event = WindowEvent()
           
          If ExamineMouse()
            MouseX = -MouseDeltaX()/20
            MouseY = -MouseDeltaY()/20
          EndIf
         
             
          If ExamineKeyboard()
             
            If KeyboardPushed(#PB_Key_Left)
              KeyX = -1
            ElseIf KeyboardPushed(#PB_Key_Right)
              KeyX = 1
            Else
              KeyX = 0
            EndIf
           
            If KeyboardPushed(#PB_Key_Up)
              KeyY = -1
            ElseIf KeyboardPushed(#PB_Key_Down)
              KeyY = 1
            Else
              KeyY = 0
            EndIf
           
            If KeyboardPushed(#PB_Key_Z)
              DisableEntityBody(#sphere, #False)
              ApplyEntityImpulse(#sphere, 0, 0, 0 )
             
              RotateEntity(0,0,0,0.5, #PB_Relative)
            ElseIf KeyboardPushed(#PB_Key_X)
              DisableEntityBody(#sphere, #False)
              ApplyEntityImpulse(#sphere, 0, 0, 0 )
             
              RotateEntity(0,0,0,-0.5, #PB_Relative)
            EndIf
           If KeyboardPushed(#PB_Key_S)
              SaveMesh(0, "Compound.mesh")
            EndIf         
            EndIf
           
            rot.f+0.6
            RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
          MoveCamera(#Camera, KeyX, 0, KeyY)
         
          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 CreateMatrix()
      CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
     
      CreateCube(30,10)
      CopyMesh(30,31)
      ;TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
      TransformMesh(31,0,0,0, 7.6,1,7.6,0,0,0)
      GetMeshData(31,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate , 0, MeshVertexCount(31)-1)
      GetMeshData(31,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(31, 0)-1)
      ArrSize = ArraySize(MeshData())
     
       ;main mesh: the ground or the Table
       For c=0 To ArrSize
         
          x.f = MeshData(c)\x
          y.f = MeshData(c)\y
          z.f = MeshData(c)\z
          MeshVertexPosition(x,y,z)
          MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v)
             
       Next   
       
       ArrSizeInd = ArraySize(MeshDataInd())
       For i=0 To ArrSizeInd Step 3
         MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
       Next
       
       AddSubMesh(#PB_Mesh_TriangleList)
       
       ;left wall
       CopyMesh(30,31)
       TransformMesh(31,-35,15,0, 0.5,2,7.6,0,0,0)
      GetMeshData(31,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate, 0, MeshVertexCount(31)-1)
      GetMeshData(31,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(31, 0)-1)
      ArrSize = ArraySize(MeshData())
     
      For c=0 To ArrSize
         
          x.f = MeshData(c)\x
          y.f = MeshData(c)\y
          z.f = MeshData(c)\z
          MeshVertexPosition(x,y,z)
          MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v)
         
       Next   
       
       ArrSizeInd = ArraySize(MeshDataInd())
       For i=0 To ArrSizeInd Step 3
         MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
       Next
         
       AddSubMesh(#PB_Mesh_TriangleList)
       
       ;right wall
       CopyMesh(30,31)
       TransformMesh(31, 35,15,0, 0.5,2,7.6,0,0,0)
      GetMeshData(31,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate, 0, MeshVertexCount(31)-1)
      GetMeshData(31,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(31, 0)-1)
      ArrSize = ArraySize(MeshData())
     
      For c=0 To ArrSize
         
          x.f = MeshData(c)\x
          y.f = MeshData(c)\y
          z.f = MeshData(c)\z
          MeshVertexPosition(x,y,z)
          MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v)
         
       Next   
       
       ArrSizeInd = ArraySize(MeshDataInd())
       For i=0 To ArrSizeInd Step 3
         MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
       Next
       
       AddSubMesh(#PB_Mesh_TriangleList)
       
       ; front wall
       CopyMesh(30,31)
       TransformMesh(31,0,11,38, 6.5,1,0.2,0,0,0)
      GetMeshData(31,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate, 0, MeshVertexCount(31)-1)
      GetMeshData(31,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(31, 0)-1)
      ArrSize = ArraySize(MeshData())
     
      For c=0 To ArrSize
         
          x.f = MeshData(c)\x
          y.f = MeshData(c)\y
          z.f = MeshData(c)\z
          MeshVertexPosition(x,y,z)
          MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v)
         
       Next   
       
       ArrSizeInd = ArraySize(MeshDataInd())
       For i=0 To ArrSizeInd Step 3
         MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
       Next
       
       AddSubMesh(#PB_Mesh_TriangleList)
       
       ; back wall
       CopyMesh(30,31)
       TransformMesh(31,0,11,-38, 6.5,1,0.2,0,0,0)
      GetMeshData(31,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate, 0, MeshVertexCount(31)-1)
      GetMeshData(31,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(31, 0)-1)
      ArrSize = ArraySize(MeshData())
     
      For c=0 To ArrSize
         
          x.f = MeshData(c)\x
          y.f = MeshData(c)\y
          z.f = MeshData(c)\z
          MeshVertexPosition(x,y,z)
          MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v)
         
       Next   
       
       ArrSizeInd = ArraySize(MeshDataInd())
       For i=0 To ArrSizeInd Step 3
         MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
       Next
       
       ;just extra sphere
       AddSubMesh(#PB_Mesh_TriangleList)
         
       CreateSphere(31,10)
       TransformMesh(31,0,11,-20, 1,1,1,0,0,0)
      GetMeshData(31,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate , 0, MeshVertexCount(31)-1)
      GetMeshData(31,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(31, 0)-1)
      ArrSize = ArraySize(MeshData())
     
      For c=0 To ArrSize
         
          x.f = MeshData(c)\x
          y.f = MeshData(c)\y
          z.f = MeshData(c)\z
          MeshVertexPosition(x,y,z)
          MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v)
         
      Next
       
       
       ArrSizeInd = ArraySize(MeshDataInd())
       For i=0 To ArrSizeInd Step 3
         MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
       Next
       
       
      FinishMesh(#True)
      SetMeshMaterial(0, MaterialID(0))
       
      CreateEntity(0, MeshID(0), MaterialID(2), 0,-10,0)
     
      CreateEntityBody(0, #PB_Entity_StaticBody, 1, 0.2, 1)
     
    EndProcedure

if you want to attach objects other than what is available in purebasic (other than spheres, cylinders, etc) then this is also can be done, but you need to do everything yourself and the code will be bigger, this code make the ground and the walls from scratch, also it draw a Cone using the TubeXYZ function. and all the complex object are one mesh with several submeshes.
Note:note that i have used #PB_Entity_ConvexHullBody in EntityPhysicBody(#sphere2, #PB_Entity_ConvexHullBody, 1,0.2,1) for the big sphere over the cone opening, else if we use #PB_Entity_SphereBody it will go through the cone opening. thats something to consider, sometimes #PB_Entity_ConvexHullBody are better to use

Code: Select all

Enumeration
   #MESH = 970
   #LIGHT
   #CAMERA
   #mainwin
   #Window
  #Plane
  #sphere
  #sphere2

EndEnumeration

Structure vector3d
  x.f
  y.f
  z.f
EndStructure

Structure vertex
  x.f
  y.f
  z.f
EndStructure


Define.f KeyX, KeyY, MouseX, MouseY

Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
ExamineDesktops()
If OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "press Z / X to rotate Ground left/Right, ... use mouse + Arrows to rotate + move Camera", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

Define.f KeyX, KeyY

Declare DrawTube (x.f, y.f,z.f, x2.f, y2.f, z2.f, RadiusS.f, RadiusE.f, TubeShape)
Declare TubeXYZ (tube, x.f, y.f, z.f, x2.f, y2.f, z2.f, RadiusS.f, RadiusE.f, TubeShape, Material)
Declare CreateMatrix()
Declare DrawMatrix()
Declare Ground()
Declare Wall_Left()
Declare Wall_Right()
Declare Wall_Back()
Declare Wall_Front()

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/3D/Data/Scripts",#PB_3DArchive_FileSystem)
    
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)
    
  
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    MaterialShadingMode(0, #PB_Material_Wireframe     )
    MaterialCullingMode(0, #PB_Material_NoCulling)
            
    CreateCamera(#camera, 0, 0, 100, 100)
    MoveCamera(#camera, 0, 70, 80, #PB_Absolute)
    CameraFOV(#camera, 70)
    CameraBackColor(#camera, RGB(255,200,200))
    CameraLookAt(#camera,0,0,0)
        
    CreateLight(0, RGB(255,255,255), 0, 20, 30)
    AmbientColor(RGB(200, 200, 200))
    
    CreateMaterial(3, LoadTexture(3, "snow_1024.jpg"))
    DisableMaterialLighting(3, #False)
    SetMaterialColor(3, #PB_Material_AmbientColor, RGB(250, 255, 0))
    SetMaterialColor(3, #PB_Material_SpecularColor, RGB(255, 255, 0))
    
    CreateMaterial(1, LoadTexture(1, "MRAMOR6X6.jpg"))
    MaterialCullingMode(1, #PB_Material_NoCulling)

    CreateMaterial(2, LoadTexture(2, "RustySteel.jpg"))
    MaterialCullingMode(2, #PB_Material_NoCulling)
    
    CreateMaterial(4, LoadTexture(4, "ground_diffuse.png"))
    MaterialCullingMode(4, #PB_Material_NoCulling)
    
    CreateMaterial(5, LoadTexture(5, "Geebee2.bmp"))
    MaterialCullingMode(5, #PB_Material_NoCulling)
    
    CreateMatrix()
    ;WorldDebug(#PB_World_DebugEntity) 
    ;WorldDebug(#PB_World_DebugBody  )
    ;RotateEntity(0, 0,0,150)
    ;RotateMaterial(1, 20, #PB_Material_Fixed )
    SetEntityMaterial(0,MaterialID(4),5)
    SetEntityMaterial(0,MaterialID(1),0)
    SetEntityMaterial(0,MaterialID(3),1)
    SetEntityMaterial(0,MaterialID(3),2)
    
    WorldGravity(-20)
    CreateSphere(#sphere2, 4)
    CreateEntity(#sphere2, MeshID(#sphere2), MaterialID(5), 0, 200, 0)
    EntityPhysicBody(#sphere2, #PB_Entity_ConvexHullBody, 1,0.2,1)
    
    CreateSphere(#sphere, 3)
    CreateEntity(#sphere, MeshID(#sphere), MaterialID(5), 0, 70, 4)
    EntityPhysicBody(#sphere, #PB_Entity_SphereBody, 0.5,0.5,0.2)
    
    Repeat
      Event = WindowEvent()
        
      If ExamineMouse()
        MouseX = -MouseDeltaX()/20 
        MouseY = -MouseDeltaY()/20
      EndIf
      
          
      If ExamineKeyboard()
         
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -1
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = 1
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -1
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = 1
        Else
          KeyY = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Z)
          DisableEntityBody(#sphere, #False)
          ApplyEntityImpulse(#sphere, 0, 0, 0 )
          DisableEntityBody(#sphere2, #False)
          ApplyEntityImpulse(#sphere2, 0, 0, 0 )
          RotateEntity(0,0,0,0.5, #PB_Relative) 
        ElseIf KeyboardPushed(#PB_Key_X)
          DisableEntityBody(#sphere, #False)
          ApplyEntityImpulse(#sphere, 0, 0, 0 )
          DisableEntityBody(#sphere2, #False)
          ApplyEntityImpulse(#sphere2, 0, 0, 0 )
          RotateEntity(0,0,0,-0.5, #PB_Relative) 
        EndIf
            
        EndIf
        
      
        rot.f+0.6
        RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(#Camera, KeyX, 0, KeyY)
      ;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 DrawMatrix()
  Ground()
  AddSubMesh(#PB_Mesh_TriangleList) 
  Wall_Left()
  
  AddSubMesh(#PB_Mesh_TriangleList) 
  Wall_Right()
  AddSubMesh(#PB_Mesh_TriangleList) 
  Wall_Back()
  AddSubMesh(#PB_Mesh_TriangleList) 
  Wall_Front()
  AddSubMesh(#PB_Mesh_TriangleList) 
      tube = 200
      ;TubeXYZ (tube, x.f, y.f, z.f, x2.f, y2.f, z2.f, RadiusS.f, RadiusE.f, TubeShape, Material)
      TubeXYZ (tube, 0, 6,0, 0, 40,0, 20, 3, 8, 1) ; call the tube making function
      
    
EndProcedure  
Procedure CreateMatrix()
  
  CreateMesh(0, #PB_Mesh_TriangleList, #True)
  DrawMatrix()
  FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(0))
    
  CreateEntity(0, MeshID(0), MaterialID(2), 0,-10,0)
  ;EntityPhysicBody(0, #PB_Entity_ConvexHullBody, 1, 0.2, 1)
  EntityPhysicBody(0, #PB_Entity_StaticBody, 1, 0.2, 1)
  ;ScaleEntity(0, 20, 4, 20)
  GetMeshData(0,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(0)-1)
  GetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(0, 0)-1)
EndProcedure

;beginning of frenet sqaure approximation 5 procedures
Procedure vector_cross(*v1.vector3d, *v2.vector3d, *vout.vector3d)
    *vout\x = (*v1\y * *v2\z) - (*v2\y * *v1\z)
    *vout\y = (*v1\z * *v2\x) - (*v2\z * *v1\x)
    *vout\z = (*v1\x * *v2\y) - (*v2\x * *v1\y)
EndProcedure

Procedure.f vector_magnitude(*v.vector3d)
    mag.f
    mag = Sqr(*v\x * *v\x + *v\y * *v\y + *v\z * *v\z)
    If mag = 0:mag = 1:EndIf
    ProcedureReturn mag
EndProcedure

Procedure vector_normalize (*v.vector3d)
    mag.f
    mag = vector_magnitude(*v)
    *v\x = *v\x / mag
    *v\y = *v\y / mag
    *v\z = *v\z / mag
EndProcedure


Procedure vector_add (*v1.vector3d, *v2.vector3d, *vout.vector3d)
    *vout\x = *v1\x + *v2\x
    *vout\y = *v1\y + *v2\y
    *vout\z = *v1\z + *v2\z
EndProcedure

Procedure vector_sub (*v1.vector3d, *v2.vector3d, *vout.vector3d)
    *vout\x = *v1\x - *v2\x
    *vout\y = *v1\y - *v2\y
    *vout\z = *v1\z - *v2\z
EndProcedure
;end of frenet sqaure approximation 5 procedures  


    
Procedure  DrawTube (x.f, y.f,z.f, x2.f, y2.f,z2.f, RadiusS.f, RadiusE.f, TubeShape)
  #Rings = 2
  Dim vertx.vertex(2)
  lineVec.vector3d

  u.f: r.f
      
  txu.f : txv.f
   
  x1.f = x
  y1.f = y
  z1.f = z
  
  bands = TubeShape ; 2 flat tube, 3 triangular tube, 4 rectangular, 8 almost cylindrical tube
    
  ;vector of the line between start vertex and the end vertex
  lineVec\x = x2 - x1
  lineVec\y = y2 - y1
  lineVec\z = z2 - z1
  

  tt.f = 0
  
  For i=0 To 2
      
      x = x1 + lineVec\x * tt 
      y = y1 + lineVec\y * tt 
      z = z1 + lineVec\z * tt
      tt + 1
      
       vertx(i)\x = x
       vertx(i)\y = y
       vertx(i)\z = z
       
   Next 
        
     current_point.vector3d
        next_point.vector3d
        T.vector3d
        B.vector3d
        N.vector3d
        p.f
     For i = 0 To 1
       If i=0 ; to manage the radius of tube start and tube end
         Radius.f = RadiusS
         Else 
           Radius.f=RadiusE
       EndIf
        ;center point
                
        current_point\x = vertx(i)\x
        current_point\y = vertx(i)\y
        current_point\z = vertx(i)\z
        ;next point For Frenet square
        
        next_point\x = vertx(i+1)\x
        next_point\y = vertx(i+1)\y
        next_point\z = vertx(i+1)\z

        ;T  = P' - P
        vector_sub(next_point, current_point, T)

        ;N = P' + P
        vector_add(next_point, current_point, N)

        ;B = T x N
        vector_cross(T, N, B)

        ;N = B x T
        vector_cross(B, T, N)

        ;Normalize vectors Or Else it won't work
        vector_normalize(B)
        vector_normalize(N)
        
        For j = 0 To bands
              new_point_x.f
              new_point_y.f
              
              ;rotate around the current point using normal rotation makes bands
            new_point_x = Sin(j * (#PI*2) / bands) * Radius
            new_point_y = Cos(j * (#PI*2) / bands) * Radius
            
                ;this is the coordinates of our point along the curve
            x = N\x * new_point_x + B\x * new_point_y + current_point\x
            y = N\y * new_point_x + B\y * new_point_y + current_point\y
            z = N\z * new_point_x + B\z * new_point_y + current_point\z
            
            MeshVertexPosition(x, y, z)
            MeshVertexTextureCoordinate(txu, txv)
            MeshVertexNormal(x, y, z)
            txv = txv + 1/#Rings
                         
         Next 
         txv = 0
         txu = txu + 1/bands
         
      Next 

      v.l
      For j = 0 To bands -1
          MeshFace(v,v+1,v + bands+1)
          MeshFace(v + bands+1,v + bands+2,v+1 )
         
          v + 1   
          
      Next 
 
  EndProcedure
  
  Procedure TubeXYZ (tube, x.f, y.f,z.f, x2.f, y2.f,z2.f, RadiusS.f, RadiusE.f, TubeShape, Material)
    x.f+0.00001 : y.f+0.00001 : z.f+0.00001 ; +0.00001 necessary if x,y,z or x2,y2,z2 are zero !!!
    x2.f+0.00001 : y2.f+0.00001 : z2.f+0.00001     
    
    ;CreateMesh(tube , #PB_Mesh_TriangleList, #PB_Mesh_Dynamic )
    
    DrawTube (x, y, z, x2, y2, z2, RadiusS, RadiusE, TubeShape)
    ;FinishMesh(tube)
    ;NormalizeMesh(tube)
    ;BuildMeshTangents(tube)
    ;CreateEntity(tube,MeshID(tube),MaterialID(Material))
    
    
  EndProcedure
  
  Procedure Ground()
    txu.f: txv.f
    MeshVertexPosition(40,2,40)
    MeshVertexTextureCoordinate(txu.f, txv.f)
    MeshVertexNormal(40,2,40)
    txv.f + 1/1
            
    MeshVertexPosition(40,-2,40)
    ;MeshVertexTextureCoordinate(txu, txv)
    MeshVertexNormal(40,-2,40)
    ;txv = 0
    ;txu+1/2
            
    MeshVertexPosition(-40,-2,40)
    ;MeshVertexTextureCoordinate(txu, txv)
    MeshVertexNormal(-40,-2,40)
    ;txv + 1/1
            
    MeshVertexPosition(-40,2,40)
    MeshVertexTextureCoordinate(txu, txv)
    MeshVertexNormal(-40,2,40)
    txv = 0
    txu + 1/1
            
    MeshVertexPosition(40,2,-40)
    MeshVertexTextureCoordinate(txu, txv)
    MeshVertexNormal(40,2,-40)
    txv + 1/1
            
    MeshVertexPosition(40,-2,-40)
    ;MeshVertexTextureCoordinate(txu, txv)
    MeshVertexNormal(40,-2,-40)
    ;txv = 0
    ;txu + 1/2
    MeshVertexPosition(-40,-2,-40)
    ;MeshVertexTextureCoordinate(txu, txv)
    MeshVertexNormal(-40,-2,-40)
    ;txv + 1/1
            
    MeshVertexPosition(-40,2,-40)
    MeshVertexTextureCoordinate(txu, txv)
    MeshVertexNormal(-40,2,-40)
     
      
    MeshFace(0,2,1)
    MeshFace(0,3,2)
    MeshFace(4,5,6)
    MeshFace(4,6,7)
    MeshFace(0,1,4)
    MeshFace(1,5,4)
    MeshFace(2,3,6)
    MeshFace(3,7,6)
    MeshFace(0,4,3)
    MeshFace(3,4,7)
    MeshFace(1,2,5)
    MeshFace(2,6,5)
EndProcedure
    
    Procedure Wall_Left()
      MeshVertexPosition(-38, 8,40)
      MeshVertexPosition(-38, 2,40)
      MeshVertexPosition(-40, 2,40)
      MeshVertexPosition(-40,8,40)
      MeshVertexPosition(-38,8,-40)
      MeshVertexPosition(-38,2,-40)
      MeshVertexPosition(-40,2,-40)
      MeshVertexPosition(-40,8,-40)
      
      
      MeshFace(0,2,1)
      MeshFace(0,3,2)
      MeshFace(4,5,6)
      MeshFace(4,6,7)
      MeshFace(0,1,4)
      MeshFace(1,5,4)
      MeshFace(2,3,6)
      MeshFace(3,7,6)
      MeshFace(0,4,3)
      MeshFace(3,4,7)
      MeshFace(1,2,5)
      MeshFace(2,6,5)
    EndProcedure
    

    Procedure Wall_Right()
      MeshVertexPosition(40,8,40)
      MeshVertexPosition(40,2,40)
      MeshVertexPosition(38,2,40)
      MeshVertexPosition(38,8,40)
      MeshVertexPosition(40,8,-40)
      MeshVertexPosition(40,2,-40)
      MeshVertexPosition(38,2,-40)
      MeshVertexPosition(38,8,-40)
      
      
      MeshFace(0,2,1)
      MeshFace(0,3,2)
      MeshFace(4,5,6)
      MeshFace(4,6,7)
      MeshFace(0,1,4)
      MeshFace(1,5,4)
      MeshFace(2,3,6)
      MeshFace(3,7,6)
      MeshFace(0,4,3)
      MeshFace(3,4,7)
      MeshFace(1,2,5)
      MeshFace(2,6,5)
    EndProcedure
    
Procedure Wall_Back()
      MeshVertexPosition(38,8,-38)
      MeshVertexPosition(38,2,-38)
      MeshVertexPosition(-38,2,-38)
      MeshVertexPosition(-38,8,-38)
      MeshVertexPosition(38,8,-40)
      MeshVertexPosition(38,2,-40)
      MeshVertexPosition(-38,2,-40)
      MeshVertexPosition(-38,8,-40)
            
      
      MeshFace(0,2,1)
      MeshFace(0,3,2)
      MeshFace(4,5,6)
      MeshFace(4,6,7)
      MeshFace(0,1,4)
      MeshFace(1,5,4)
      MeshFace(2,3,6)
      MeshFace(3,7,6)
      MeshFace(0,4,3)
      MeshFace(3,4,7)
      MeshFace(1,2,5)
      MeshFace(2,6,5)
    EndProcedure  
    
Procedure Wall_Front()
      MeshVertexPosition(38,8,40)
      MeshVertexPosition(38,2,40)
      MeshVertexPosition(-38,2,40)
      MeshVertexPosition(-38,8,40)
      MeshVertexPosition(38,8,38)
      MeshVertexPosition(38,2,38)
      MeshVertexPosition(-38,2,38)
      MeshVertexPosition(-38,8,38)
      
      
      MeshFace(0,2,1)
      MeshFace(0,3,2)
      MeshFace(4,5,6)
      MeshFace(4,6,7)
      MeshFace(0,1,4)
      MeshFace(1,5,4)
      MeshFace(2,3,6)
      MeshFace(3,7,6)
      MeshFace(0,4,3)
      MeshFace(3,4,7)
      MeshFace(1,2,5)
      MeshFace(2,6,5)
    EndProcedure    
    
  
 
     
Last edited by applePi on Thu Aug 30, 2018 4:00 pm, edited 7 times in total.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: make a compound object without softening its Physics Bod

Post by applePi »

to show the users that we can make extremely complex objects and still have physics body here is again the spikey sphere code but made using the above described approach, and using the Comtois version of the sphere http://purebasic.fr/english/viewtopic.p ... 15#p418885 , because after this code i will show you how to adapt this new sphere proceture to make half sphere. this spikey sphere will fall to the ground and run over it.
to prevent the Alien from flying press Space to stop it rotating since the rotation gives it some impulse
Image

Code: Select all

#CameraSpeed = 1

Enumeration
  #Window
  #Plane = 970
  #Camera
  #Light1
  #Light2
  #sphere

EndEnumeration
;Debug #sphere

Structure vector3d
  x.f
  y.f
  z.f
EndStructure

Structure vertex
  x.f
  y.f
  z.f
EndStructure

Declare DrawTube (x.f, y.f,z.f, x2.f, y2.f, z2.f, RadiusS.f, RadiusE.f, TubeShape)
Declare TubeXYZ (tube, x.f, y.f, z.f, x2.f, y2.f, z2.f, RadiusS.f, RadiusE.f, TubeShape, Material)
Declare CreateSphere2(No, mRadius, mNumRings, mNumSegments)

Global Dim MeshDataVertex.PB_MeshVertex(0)
Global Dim MeshDataIndex.PB_MeshFace(0)



Global Dim MeshData.PB_MeshVertex(0)

Global xx.f, yy.f
Global ArrSize

Define.f KeyX, KeyY, MouseX, MouseY

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/3D/Data/Scripts",#PB_3DArchive_FileSystem)
    
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
ExamineDesktops()
DesktopW = DesktopWidth(0)
DesktopH = DesktopHeight(0)

If OpenWindow(#Window, 0, 0, DesktopW, DesktopH, "spikey , W toggle wireFrame...., . Space toggle rotation, ... mouse/arrow keys: rotate/move camera ")
  If OpenWindowedScreen(WindowID(#Window), 0, 0, DesktopW, DesktopH, 0, 0, 0)
  
    CreateMaterial(3, LoadTexture(3, "snow_1024.jpg"))
    DisableMaterialLighting(3, #False)
    SetMaterialColor(3, #PB_Material_AmbientColor, RGB(250, 255, 0))
    SetMaterialColor(3, #PB_Material_SpecularColor, RGB(255, 255, 0))
    
        
    CreatePlane(#Plane, 150, 150, 1, 1, 10, 10)
    CreateEntity(#Plane,MeshID(#Plane),MaterialID(3),0.1,-10,0.2)
    EntityPhysicBody(#Plane, #PB_Entity_StaticBody)
    
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    
    CreateMaterial(1, LoadTexture(1, "Dirt.jpg"))
    MaterialCullingMode(1, #PB_Material_NoCulling)

    CreateMaterial(2, LoadTexture(2, "RustySteel.jpg"))
    MaterialCullingMode(2, #PB_Material_NoCulling)
    
    CreateMaterial(4, LoadTexture(4, "ground_diffuse.png"))
    MaterialCullingMode(4, #PB_Material_NoCulling)
    
    CreateMaterial(5, LoadTexture(5, "Geebee2.bmp"))
    MaterialCullingMode(5, #PB_Material_NoCulling)
       
    CreateSphere2(#sphere,3,6,6) ; call the new version : CreatSphere2
    ;CreateEntity(#sphere,MeshID(#sphere), MaterialID(1),0,0,0 )
    AddSubMesh(#PB_Mesh_TriangleList)
        
    ;getting info about the big sphere data
    GetMeshData(#sphere,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Normal , 0, MeshVertexCount(#sphere)-1)
    ArrSize = ArraySize(MeshData())
            
    SetMeshMaterial(#sphere, MaterialID(1),0)
        
    tube = 0 ; we begins the tubes mesh numbering from 0 
    For c=0 To ArrSize
      AddSubMesh(#PB_Mesh_TriangleList)
      ; getting data of sphere vertices positions to use this in drawing the tubes with TubeXYZ(...) function
      x.f = MeshData(c)\x 
      y.f = MeshData(c)\y
      z.f = MeshData(c)\z
      
      x2.f = 3*MeshData(c)\x
      y2.f = 3*MeshData(c)\y
      z2.f = 3*MeshData(c)\z
            
      ;TubeXYZ (tube, x.f, y.f, z.f, x2.f, y2.f, z2.f, RadiusS.f, RadiusE.f, TubeShape, Material)
      TubeXYZ (tube, x.f, y.f,z.f, x2.f, y2.f,z2.f, 0.3, 0.0, 8, 4) ; call the tube making function
      submesh+1
      SetMeshMaterial(#sphere, MaterialID(4),submesh)
            
      tube + 1 ; next tube mesh numbering
      
    Next c
    ;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
   
    FinishMesh(#True)
    CreateEntity(#sphere, MeshID(#sphere), #PB_Material_None , 0,10,0)
    EntityPhysicBody(#sphere, #PB_Entity_ConvexHullBody, 1,0.2,1)
    
    CreateCamera(#Camera, 0, 0, 100, 100)
    MoveCamera(#Camera, 0, 5, 30, #PB_Absolute)
    CameraLookAt(#Camera,0,0,0)
    CameraBackColor(#Camera, RGB(0,100,0))


    CreateLight(#Light1, RGB(0, 0, 0), -20, 40, 20)
    SetLightColor(#Light1, #PB_Light_DiffuseColor, RGB(255,255,255))
    CreateLight(#Light2, RGB(0, 0, 0), 20, 40, 20)
    SetLightColor(#Light2, #PB_Light_DiffuseColor, RGB(0,0,255))
    
    AmbientColor(RGB(255, 255, 255))

  EndIf
EndIf

wireFrame = 1: rot=1 

RotateEntity(#sphere, 0,0,170)
Repeat
  
  Event = WindowEvent()
        
      If ExamineMouse()
        MouseX = -MouseDeltaX()/20 
        MouseY = -MouseDeltaY()/20
      EndIf
      
          
      If ExamineKeyboard()
         
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -0.2
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = 0.2
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -0.2
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = 0.2
        Else
          KeyY = 0
        EndIf
        
        If KeyboardReleased(#PB_Key_W)
          If wireFrame
            MaterialShadingMode(1, #PB_Material_Wireframe)
            MaterialShadingMode(4, #PB_Material_Wireframe)
          wireFrame ! 1
        Else 
          MaterialShadingMode(1, #PB_Material_Solid)
          MaterialShadingMode(4, #PB_Material_Solid)
          wireFrame ! 1
        EndIf
      EndIf
      If KeyboardReleased(#PB_Key_S)
        SaveMesh(#sphere, "spiky.mesh")
      EndIf
      
            
      EndIf
  
      RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(#Camera, KeyX, 0, KeyY)
                      
      RotateEntity(#sphere, 0,rot,0,#PB_Relative)
      
      If KeyboardReleased(#PB_Key_Space)
        rot ! 1 
        SaveMesh(#sphere, "sphere2.mesh"); it saves only the mother sphere discarding what we have added to it previously

      EndIf
                
      RenderWorld()
      
            
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
    
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
  
End

;beginning of frenet sqaure approximation 5 procedures
Procedure vector_cross(*v1.vector3d, *v2.vector3d, *vout.vector3d)
    *vout\x = (*v1\y * *v2\z) - (*v2\y * *v1\z)
    *vout\y = (*v1\z * *v2\x) - (*v2\z * *v1\x)
    *vout\z = (*v1\x * *v2\y) - (*v2\x * *v1\y)
EndProcedure

Procedure.f vector_magnitude(*v.vector3d)
    mag.f
    mag = Sqr(*v\x * *v\x + *v\y * *v\y + *v\z * *v\z)
    If mag = 0:mag = 1:EndIf
    ProcedureReturn mag
EndProcedure

Procedure vector_normalize (*v.vector3d)
    mag.f
    mag = vector_magnitude(*v)
    *v\x = *v\x / mag
    *v\y = *v\y / mag
    *v\z = *v\z / mag
EndProcedure


Procedure vector_add (*v1.vector3d, *v2.vector3d, *vout.vector3d)
    *vout\x = *v1\x + *v2\x
    *vout\y = *v1\y + *v2\y
    *vout\z = *v1\z + *v2\z
EndProcedure

Procedure vector_sub (*v1.vector3d, *v2.vector3d, *vout.vector3d)
    *vout\x = *v1\x - *v2\x
    *vout\y = *v1\y - *v2\y
    *vout\z = *v1\z - *v2\z
EndProcedure
;end of frenet sqaure approximation 5 procedures  

Procedure  DrawTube (x.f, y.f,z.f, x2.f, y2.f,z2.f, RadiusS.f, RadiusE.f, TubeShape)
  #Rings = 2
  Dim vertx.vertex(2)
  lineVec.vector3d

  u.f: r.f
      
  txu.f : txv.f
   
  x1.f = x
  y1.f = y
  z1.f = z
  
  bands = TubeShape ; 2 flat tube, 3 triangular tube, 4 rectangular, 8 almost cylindrical tube
    
  ;vector of the line between start vertex and the end vertex
  lineVec\x = x2 - x1
  lineVec\y = y2 - y1
  lineVec\z = z2 - z1
  

  tt.f = 0
  
  For i=0 To 2
      
      x = x1 + lineVec\x * tt 
      y = y1 + lineVec\y * tt 
      z = z1 + lineVec\z * tt
      tt + 1
      
       vertx(i)\x = x
       vertx(i)\y = y
       vertx(i)\z = z
       
   Next 
        
     current_point.vector3d
        next_point.vector3d
        T.vector3d
        B.vector3d
        N.vector3d
        p.f
     For i = 0 To 1
       If i=0 ; to manage the radius of tube start and tube end
         Radius.f = RadiusS
         Else 
           Radius.f=RadiusE
       EndIf
        ;center point
                
        current_point\x = vertx(i)\x
        current_point\y = vertx(i)\y
        current_point\z = vertx(i)\z
        ;next point For Frenet square
        
        next_point\x = vertx(i+1)\x
        next_point\y = vertx(i+1)\y
        next_point\z = vertx(i+1)\z

        ;T  = P' - P
        vector_sub(next_point, current_point, T)

        ;N = P' + P
        vector_add(next_point, current_point, N)

        ;B = T x N
        vector_cross(T, N, B)

        ;N = B x T
        vector_cross(B, T, N)

        ;Normalize vectors Or Else it won't work
        vector_normalize(B)
        vector_normalize(N)
        
        For j = 0 To bands
              new_point_x.f
              new_point_y.f
              
              ;rotate around the current point using normal rotation makes bands
            new_point_x = Sin(j * (#PI*2) / bands) * Radius
            new_point_y = Cos(j * (#PI*2) / bands) * Radius
            
                ;this is the coordinates of our point along the curve
            x = N\x * new_point_x + B\x * new_point_y + current_point\x
            y = N\y * new_point_x + B\y * new_point_y + current_point\y
            z = N\z * new_point_x + B\z * new_point_y + current_point\z
            
            MeshVertexPosition(x, y, z)
            MeshVertexTextureCoordinate(txu, txv)
            MeshVertexNormal(x, y, z)
            txv = txv + 1/#Rings
                         
         Next 
         txv = 0
         txu = txu + 1/bands
         
      Next 

      v.l
      For j = 0 To bands -1
          MeshFace(v,v+1,v + bands+1)
          MeshFace(v + bands+1,v + bands+2,v+1 )
         
          v + 1   
          
      Next 
 
  EndProcedure
  
  Procedure TubeXYZ (tube, x.f, y.f,z.f, x2.f, y2.f,z2.f, RadiusS.f, RadiusE.f, TubeShape, Material)
    x.f+0.00001 : y.f+0.00001 : z.f+0.00001 ; +0.00001 necessary if x,y,z or x2,y2,z2 are zero !!!
    x2.f+0.00001 : y2.f+0.00001 : z2.f+0.00001     
    
    DrawTube (x, y, z, x2, y2, z2, RadiusS, RadiusE, TubeShape)
    
  EndProcedure
  
 
 Procedure Build(Radius, NumRings, NumSegments)
  DeltaRingAngle.f = #PI / NumRings
  DeltaSegAngle.f = #PI*2 / NumSegments
  offset = 0
  
  For ring = 0 To NumRings
    r0.f = Radius * Sin(ring * DeltaRingAngle)
    y0.f = Radius * Cos(ring * DeltaRingAngle)
    
    For seg = 0 To NumSegments;
      x0.f = r0 * Sin(seg * DeltaSegAngle)
      z0.f = r0 * Cos(seg * DeltaSegAngle)
      
      MeshVertexPosition(x0, y0, z0)
      MeshVertexNormal(x0, y0, z0); should be normalized if Radius <> 1
      MeshVertexTextureCoordinate(seg / NumSegments, ring / NumRings)
      
      If ring <> NumRings  
        
        If seg <> NumSegments
          
          If ring <> NumRings-1
          
            MeshFace(offset + NumSegments + 2, offset, offset + NumSegments + 1)
          EndIf  
          If ring <> 0
            MeshFace(offset + NumSegments + 2, offset + 1, offset)   
          EndIf  
        EndIf
        offset + 1
      EndIf
    Next
  Next  
EndProcedure

Procedure CreateSphere2(No, Radius, NumRings, NumSegments)
  
  CreateMesh(No, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic )
  
  Build(Radius, NumRings, NumSegments)
  
  
EndProcedure
now to get a half sphere for the Alien creature body change the procedure Build(...) to this:

Code: Select all

Procedure Build(Radius, NumRings, NumSegments)
  DeltaRingAngle.f = #PI / NumRings/2
  DeltaSegAngle.f = #PI*2 / NumSegments
  offset = 0
  
  For ring = 0 To NumRings
    r0.f = Radius * Sin(ring * DeltaRingAngle)
    y0.f = Radius * Cos(ring * DeltaRingAngle)
    
    For seg = 0 To NumSegments;
      x0.f = r0 * Sin(seg * DeltaSegAngle)
      z0.f = r0 * Cos(seg * DeltaSegAngle)
      
      MeshVertexPosition(x0, y0, z0)
      MeshVertexNormal(x0, y0, z0); should be normalized if Radius <> 1
      MeshVertexTextureCoordinate(seg / NumSegments, ring / NumRings)
      
      If ring <> NumRings  
        
        If seg <> NumSegments
          
          ;If ring <> NumRings-1
          
            MeshFace(offset + NumSegments + 2, offset, offset + NumSegments + 1)
          ;EndIf  
          If ring <> 0
            MeshFace(offset + NumSegments + 2, offset + 1, offset)   
          EndIf  
        EndIf
        offset + 1
      EndIf
    Next
  Next  
EndProcedure
User avatar
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: make a compound object without softening its Physics Bod

Post by falsam »

applePi thank you for these codes. I finally understood how to assemble multiple meshes into a single entity.It's great. Thank, thank & .... thank :)

➽ 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: make a compound object without softening its Physics Bod

Post by applePi »

falsam, i have surprised that we can attach also the "tudorhouse.mesh" instead of the big sticky sphere and texturing it with fw12b.jpg, it is great that those models have the UV info inside so the texturing are correct.
CreateMaterial(6, LoadTexture(6, "fw12b.jpg"))
MaterialCullingMode(6, #PB_Material_NoCulling)
...
SetEntityMaterial(0,MaterialID(6),5) ;the House sticky on the ground
....

LoadMesh(31, "tudorhouse.mesh")
TransformMesh(31,0,30,-10, 0.05,0.05,0.05,0,0,0)

so your table have now a tudor house
Image
for the convenience of the users here is the first code in this page but to add tudor house to the table

Code: Select all

Enumeration
   
   #LIGHT
   #CAMERA
   #mainwin
   #Plane
  #sphere
  
EndEnumeration

Structure vector3d
  x.f
  y.f
  z.f
EndStructure

Structure vertex
  x.f
  y.f
  z.f
EndStructure


Define.f KeyX, KeyY, MouseX, MouseY

Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
Global Dim MeshData2.PB_MeshVertex(0)
Global Dim MeshDataInd2.PB_MeshFace(0)
ExamineDesktops()
If OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "press Z / X to rotate Ground left/Right, ... use mouse + Arrows to rotate + move Camera", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

Define.f KeyX, KeyY

Declare CreateMatrix()


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/3D/Data/Scripts",#PB_3DArchive_FileSystem)
    
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)
    
  
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    MaterialShadingMode(0, #PB_Material_Wireframe     )
    MaterialCullingMode(0, #PB_Material_NoCulling)
            
    CreateCamera(#camera, 0, 0, 100, 100)
    MoveCamera(#camera, 0, 70, 80, #PB_Absolute)
    CameraFOV(#camera, 70)
    CameraBackColor(#camera, RGB(255,200,200))
    CameraLookAt(#camera,0,0,0)
        
    CreateLight(0, RGB(255,255,255), 0, 20, 30)
    AmbientColor(RGB(200, 200, 200))
    
    CreateMaterial(3, LoadTexture(3, "snow_1024.jpg"))
    DisableMaterialLighting(3, #False)
    SetMaterialColor(3, #PB_Material_AmbientColor, RGB(250, 255, 0))
    SetMaterialColor(3, #PB_Material_SpecularColor, RGB(255, 255, 0))
    
    CreateMaterial(1, LoadTexture(1, "MRAMOR6X6.jpg"))
    MaterialCullingMode(1, #PB_Material_NoCulling)

    CreateMaterial(2, LoadTexture(2, "RustySteel.jpg"))
    MaterialCullingMode(2, #PB_Material_NoCulling)
    
    CreateMaterial(4, LoadTexture(4, "ground_diffuse.png"))
    MaterialCullingMode(4, #PB_Material_NoCulling)
    
    CreateMaterial(5, LoadTexture(5, "Geebee2.bmp"))
    MaterialCullingMode(5, #PB_Material_NoCulling)
    
    CreateMaterial(6, LoadTexture(6, "fw12b.jpg"))
    MaterialCullingMode(6, #PB_Material_NoCulling)
    
    
    
    CreateMatrix()
  
    SetEntityMaterial(0,MaterialID(6),5) ;the House sticky on the ground
    SetEntityMaterial(0,MaterialID(1),0) ;Ground
    SetEntityMaterial(0,MaterialID(3),1) ;Left wall
    SetEntityMaterial(0,MaterialID(3),2) ;Right wall
    
    WorldGravity(-20)
        
    CreateSphere(#sphere, 3)
    CreateEntity(#sphere, MeshID(#sphere), MaterialID(5), 0, 60, 4)
    EntityPhysicBody(#sphere, #PB_Entity_SphereBody, 0.5,0.5,0.2)
            
    Repeat
      Event = WindowEvent()
        
      If ExamineMouse()
        MouseX = -MouseDeltaX()/20 
        MouseY = -MouseDeltaY()/20
      EndIf
      
          
      If ExamineKeyboard()
         
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -1
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = 1
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -1
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = 1
        Else
          KeyY = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Z)
          DisableEntityBody(#sphere, #False)
          ApplyEntityImpulse(#sphere, 0, 0, 0 )
          
          RotateEntity(0,0,0,0.5, #PB_Relative) 
        ElseIf KeyboardPushed(#PB_Key_X)
          DisableEntityBody(#sphere, #False)
          ApplyEntityImpulse(#sphere, 0, 0, 0 )
          
          RotateEntity(0,0,0,-0.5, #PB_Relative) 
        EndIf
       If KeyboardPushed(#PB_Key_S)
          SaveMesh(0, "Compound.mesh")
        EndIf         
        EndIf
       
        rot.f+0.6
        RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(#Camera, KeyX, 0, KeyY)
      
      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 CreateMatrix()
  CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
  
  CreateCube(30,10)
  CopyMesh(30,31)
  ;TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
  TransformMesh(31,0,0,0, 7.6,1,7.6,0,0,0)
  GetMeshData(31,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate , 0, MeshVertexCount(31)-1)
  GetMeshData(31,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(31, 0)-1)
  ArrSize = ArraySize(MeshData())
  
   ;main mesh: the ground or the Table
   For c=0 To ArrSize
      
      x.f = MeshData(c)\x 
      y.f = MeshData(c)\y
      z.f = MeshData(c)\z
      MeshVertexPosition(x,y,z)
      MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v) 
         
   Next   
   
   ArrSizeInd = ArraySize(MeshDataInd()) 
   For i=0 To ArrSizeInd Step 3
     MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
   Next
   
   AddSubMesh(#PB_Mesh_TriangleList)
   
   ;left wall
   CopyMesh(30,31)
   TransformMesh(31,-35,15,0, 0.5,2,7.6,0,0,0)
  GetMeshData(31,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate, 0, MeshVertexCount(31)-1)
  GetMeshData(31,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(31, 0)-1)
  ArrSize = ArraySize(MeshData())
  
  For c=0 To ArrSize
      
      x.f = MeshData(c)\x 
      y.f = MeshData(c)\y
      z.f = MeshData(c)\z
      MeshVertexPosition(x,y,z)
      MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v) 
      
   Next   
   
   ArrSizeInd = ArraySize(MeshDataInd()) 
   For i=0 To ArrSizeInd Step 3
     MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
   Next
      
   AddSubMesh(#PB_Mesh_TriangleList)
   
   ;right wall
   CopyMesh(30,31)
   TransformMesh(31, 35,15,0, 0.5,2,7.6,0,0,0)
  GetMeshData(31,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate, 0, MeshVertexCount(31)-1)
  GetMeshData(31,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(31, 0)-1)
  ArrSize = ArraySize(MeshData())
  
  For c=0 To ArrSize
      
      x.f = MeshData(c)\x 
      y.f = MeshData(c)\y
      z.f = MeshData(c)\z
      MeshVertexPosition(x,y,z)
      MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v) 
      
   Next   
   
   ArrSizeInd = ArraySize(MeshDataInd()) 
   For i=0 To ArrSizeInd Step 3
     MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
   Next
   
   AddSubMesh(#PB_Mesh_TriangleList)
   
   ; front wall
   CopyMesh(30,31)
   TransformMesh(31,0,11,38, 6.5,1,0.2,0,0,0)
  GetMeshData(31,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate, 0, MeshVertexCount(31)-1)
  GetMeshData(31,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(31, 0)-1)
  ArrSize = ArraySize(MeshData())
  
  For c=0 To ArrSize
      
      x.f = MeshData(c)\x 
      y.f = MeshData(c)\y
      z.f = MeshData(c)\z
      MeshVertexPosition(x,y,z)
      MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v) 
      
   Next   
   
   ArrSizeInd = ArraySize(MeshDataInd()) 
   For i=0 To ArrSizeInd Step 3
     MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
   Next
   
   AddSubMesh(#PB_Mesh_TriangleList)
   
   ; back wall
   CopyMesh(30,31)
   TransformMesh(31,0,11,-38, 6.5,1,0.2,0,0,0)
  GetMeshData(31,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate, 0, MeshVertexCount(31)-1)
  GetMeshData(31,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(31, 0)-1)
  ArrSize = ArraySize(MeshData())
  
  For c=0 To ArrSize
      
      x.f = MeshData(c)\x 
      y.f = MeshData(c)\y
      z.f = MeshData(c)\z
      MeshVertexPosition(x,y,z)
      MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v) 
      
   Next   
   
   ArrSizeInd = ArraySize(MeshDataInd()) 
   For i=0 To ArrSizeInd Step 3
     MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
   Next
   
   ;just extra sphere
   AddSubMesh(#PB_Mesh_TriangleList)
      
   ;CreateSphere(31,10)
   LoadMesh(31, "tudorhouse.mesh")
   TransformMesh(31,0,30,-10, 0.05,0.05,0.05,0,0,0)
  GetMeshData(31,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate , 0, MeshVertexCount(31)-1)
  GetMeshData(31,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(31, 0)-1)
  ArrSize = ArraySize(MeshData())
  
  For c=0 To ArrSize
      
      x.f = MeshData(c)\x 
      y.f = MeshData(c)\y
      z.f = MeshData(c)\z
      MeshVertexPosition(x,y,z)
      MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v) 
      
  Next 
    
   
   ArrSizeInd = ArraySize(MeshDataInd()) 
   For i=0 To ArrSizeInd Step 3
     MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
   Next
   
   
  FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(0))
    
  CreateEntity(0, MeshID(0), MaterialID(2), 0,-10,0)
  
  EntityPhysicBody(0, #PB_Entity_StaticBody, 1, 0.2, 1)
  
EndProcedure
User avatar
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: make a compound object without softening its Physics Bod

Post by falsam »

With this latest code and precedents, it is not possible to have shadows.

I added WorldShadows(#PB_Shadow_Additive) and BuildMeshShadowVolume(0) but no result.

➽ 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
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: make a compound object without softening its Physics Bod

Post by falsam »

Update [PB4.43 & 5.50 LTS]

I created a procedure to add sub-meshes. Thank you for this great code applePi.

Use Lef,Right, Up and Down Key

Code: Select all

EnableExplicit

Enumeration
  #Mainform
EndEnumeration

Global Rx.f=0, Rz.f = 10
Global Event

InitEngine3D()
InitKeyboard()
InitSprite()

Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)

;Ajouter un mesh à un autre mesh - Add a mesh to another mesh
Procedure AddMesh(Mesh, x.f=0 , y.f=0, z.f=0, ScaleX.f=1, ScaleY.f=1, ScaleZ.f=1, RotateX.f=0, RotateY.f=0, RotateZ.f=0)
  Protected Dim MeshData.PB_MeshVertex(0)
  Protected Dim MeshDataInd.PB_MeshFace(0)
  Protected ArrSize, ArrSizeInd, c, i, mdx.f, mdy.f, mdz.f, subMesh.i
  
  For subMesh = 0 To SubMeshCount(Mesh)-1    
    TransformMesh(Mesh, x, y, z, ScaleX,ScaleY,ScaleZ, RotateX,RotateY,RotateZ, subMesh)
    GetMeshData(Mesh, subMesh, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate | #PB_Mesh_Normal, 0, MeshVertexCount(Mesh, subMesh)-1)
    GetMeshData(Mesh, subMesh, MeshDataInd(), #PB_Mesh_Face , 0, MeshIndexCount(Mesh, subMesh)-1)
    
    ArrSize = ArraySize(MeshData())
    For c=0 To ArrSize    
      mdx = MeshData(c)\x 
      mdy = MeshData(c)\y
      mdz = MeshData(c)\z
      MeshVertexPosition(mdx, mdy, mdz)
      
      mdx = MeshData(c)\NormalX 
      mdy = MeshData(c)\NormalY
      mdz = MeshData(c)\NormalZ
      MeshVertexNormal(mdx, mdy, mdz)
      
      mdx = MeshData(c)\TangentX
      mdy = MeshData(c)\TangentY
      mdz = MeshData(c)\TangentZ
      MeshVertexTangent(mdx, mdy, mdz)
      
      MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v)        
    Next   
    
    ArrSizeInd = ArraySize(MeshDataInd()) 
    For i=0 To ArrSizeInd Step 3
      MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
    Next
    AddSubMesh(#PB_Mesh_TriangleList)
  Next 
EndProcedure

OpenWindow(#Mainform,0,0, 1024, 768, "", #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(#Mainform),0,0,1024,768,0, 0, 0)

;Light & Shadow
AmbientColor(RGB(127, 127, 127))
CreateLight(#PB_Any,RGB(151, 251, 151), -5, 10, 5)
WorldShadows(#PB_Shadow_Additive)

;Camera
CreateCamera(0, 0, 0,100,100)
CameraBackColor(0, RGB(145, 182, 201))
MoveCamera(0, 0, 8, 15, #PB_Absolute)  
CameraLookAt(0, 0,0,0)   

;Textures & material
CreateMaterial(0, TextureID(LoadTexture(#PB_Any, "Dirt.jpg")))      
CreateMaterial(1, TextureID(LoadTexture(#PB_Any, "MRAMOR6X6.jpg"))) 

;Create an empty mesh
CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)

;Add Sub Mesh
AddMesh(CreateCube(#PB_Any, 1), 0,0,0, 10,0.05,10)            ;Sub-Mesh 0 to 5
AddMesh(CreateCube(#PB_Any, 1), -5,0.5,0, 0.1,1,10)           ;Sub-Mesh 6 to 11
AddMesh(CreateCube(#PB_Any, 1), 5,0.5,0, 0.1,1,10)            ;Sub-Mesh 12 to 17
AddMesh(CreateCube(#PB_Any, 1), 0,0.5,5, 0.1,1,10, 0,90,0)    ;Sub-Mesh 18 to 23 
AddMesh(CreateCube(#PB_Any, 1), 0,0.5,-5, 0.1,1,10, 0,90,0)   ;Sub-Mesh 24 to 29

;End Create Mesh
FinishMesh(#True)

;Save your mesh if you want
;SaveMesh(0, "test.mesh")

;For each mesh, adding a material
SetMeshMaterial(0, MaterialID(0), 0) 
SetMeshMaterial(0, MaterialID(1), 1) 
SetMeshMaterial(0, MaterialID(1), 2) 
SetMeshMaterial(0, MaterialID(1), 3)
SetMeshMaterial(0, MaterialID(1), 4) 

BuildMeshShadowVolume(0)

;The sandbox is ready
CreateEntity(0, MeshID(0), #PB_Material_None)
CreateEntityBody(0, #PB_Entity_StaticBody, 1, 1, 1)
RotateEntity(0, Rx, 0, Rz)

;Ball
CreateEntity(1, MeshID(CreateSphere(#PB_Any, 0.7)), MaterialID(1), 0, 3, 0)
CreateEntityBody(1, #PB_Entity_SphereBody, 0.5, 0.5, 0.2) 

Repeat
  Repeat
    Event  = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        End
        
    EndSelect
  Until Event = 0
  
  If ExamineKeyboard()
    If KeyboardPushed (#PB_Key_Escape)
      Break
    EndIf
    
    If KeyboardPushed (#PB_Key_Right)
      Rz - 0.2
    EndIf
    
    If KeyboardPushed (#PB_Key_Left)
      Rz + 0.2
    EndIf
    
    If KeyboardPushed (#PB_Key_Up)
      Rx - 0.2
    EndIf
    
    If KeyboardPushed (#PB_Key_Down)
      Rx + 0.2
    EndIf
    
  EndIf
  
  ;BugWare
  ApplyEntityImpulse(1, 0, 0, 0 )
  
  RotateEntity(0, Rx, 0, Rz)
  
  RenderWorld(40)
  FlipBuffers()  
ForEver
Last edited by falsam on Tue Oct 11, 2016 4:34 pm, edited 3 times in total.

➽ 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: make a compound object without softening its Physics Bod

Post by applePi »

thanks falsam for your code, it works with the shadow, i have added this from the Bridge.pb example:
WorldShadows(#PB_Shadow_Modulative, -1, RGB(127, 127, 127))
the #PB_Shadow_Additive seems does not work here
i have checked the PB examples and most examples are using #PB_Shadow_Modulative, 4 examples are using WorldShadows(#PB_Shadow_Additive): carDemo.pb, StaticGeometry.pb, Pendulum.pb, NodeAnimation2.pb
while Character.pb, water.pb are using #PB_Shadow_TextureModulative
the other 23 examples are using #PB_Shadow_Modulative
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: make a compound object without softening its Physics Bod

Post by applePi »

falsam, your version makes the life easier, and it is smaller. i have found that also getting the Normals + Tangents data from the meshes allow the shadow additive to be displayed, it is said more accurate than the shadow modulative but slower , from the Doc:http://www.purebasic.com/documentation/ ... adows.html
we need this:
GetMeshData(Mesh,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate | #PB_Mesh_Normal, 0, MeshVertexCount(Mesh, 0)-1)

here is the new version: using WorldShadows(#PB_Shadow_Additive)

Code: Select all

EnableExplicit

Enumeration
  #Mainform
EndEnumeration

Global Rx.f=0, Rz.f = 10
Global Event

InitEngine3D()
InitKeyboard()
InitSprite()
InitMouse()

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/3D/Data/Scripts",#PB_3DArchive_FileSystem)
Parse3DScripts()

;Add mesh to a mesh (Sub Mesh)
Procedure AddMesh(Mesh, NewX.f=0 , NewY.f=0, NewZ.f=0, ScaleX.f=1, ScaleY.f=1, ScaleZ.f=1, RotateX.f=0, RotateY.f=0, RotateZ.f=0)
  Protected Dim MeshData.PB_MeshVertex(0)
  Protected Dim MeshDataInd.PB_MeshFace(0)
  Protected ArrSize, ArrSizeInd, c, i, x.f, y.f, z.f
  
  TransformMesh(Mesh, NewX,NewY,NewZ, ScaleX,ScaleY,ScaleZ, RotateX,RotateY,RotateZ)
  GetMeshData(Mesh,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate | #PB_Mesh_Normal, 0, MeshVertexCount(Mesh, 0)-1)
  GetMeshData(Mesh,0, MeshDataInd(), #PB_Mesh_Face| #PB_Mesh_Color, 0, MeshIndexCount(Mesh, 0)-1)
  
  ArrSize = ArraySize(MeshData())
  For c=0 To ArrSize    
    x.f = MeshData(c)\x 
      y.f = MeshData(c)\y
      z.f = MeshData(c)\z
      MeshVertexPosition(x,y,z)
      x.f = MeshData(c)\NormalX 
      y.f = MeshData(c)\NormalY
      z.f = MeshData(c)\NormalZ
      MeshVertexNormal(x, y, z)
      x.f = MeshData(c)\TangentX
      y.f = MeshData(c)\TangentY
      z.f = MeshData(c)\TangentZ
      MeshVertexTangent(x, y, z)
      
      MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v)        
  Next   
  
  ArrSizeInd = ArraySize(MeshDataInd()) 
  For i=0 To ArrSizeInd Step 3
    MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
  Next
   
  AddSubMesh(#PB_Mesh_TriangleList)
EndProcedure

OpenWindow(#Mainform,0,0, 1024, 768, "", #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(#Mainform),0,0,1024,768,0, 0, 0)

;Light & Shadow
AmbientColor(RGB(127, 127, 127))
CreateLight(#PB_Any,RGB(151, 251, 151), -20, 10, 5)
WorldShadows(#PB_Shadow_Additive)

;Camera
CreateCamera(0, 0, 0,100,100)
CameraBackColor(0, RGB(145, 182, 201))
MoveCamera(0, 0, 5, 15, #PB_Absolute)  
CameraLookAt(0, 0,0,0)   

;Textures & material
CreateMaterial(0, TextureID(LoadTexture(#PB_Any, "Dirt.jpg")))      
CreateMaterial(1, TextureID(LoadTexture(#PB_Any, "MRAMOR6X6.jpg"))) 

;Create an empty mesh
CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)

;Add Sub Mesh
AddMesh(CreateCube(#PB_Any, 1), 0,0,0, 10,0.1,10)             ;Sub-Mesh 0
AddMesh(CreateCube(#PB_Any, 1), -5,0.5,0, 0.1,1,10)           ;Sub-Mesh 1
AddMesh(CreateCube(#PB_Any, 1), 5,0.5,0, 0.1,1,10)            ;Sub-Mesh 2
AddMesh(CreateCube(#PB_Any, 1), 0,0.5,5, 0.1,1,10, 0,90,0)    ;Sub-Mesh 3
AddMesh(CreateCube(#PB_Any, 1), 0,0.5,-5, 0.1,1,10, 0,90,0)   ;Sub-Mesh 4
;BuildMeshTangents(0)

;End Create Mesh
;NormalizeMesh(0)
FinishMesh(#True)

;Save your mesh if you want
;SaveMesh(0, "test.mesh")

;For each mesh, adding a material
SetMeshMaterial(0, MaterialID(0), 0) 
SetMeshMaterial(0, MaterialID(1), 1) 
SetMeshMaterial(0, MaterialID(1), 2) 
SetMeshMaterial(0, MaterialID(1), 3)
SetMeshMaterial(0, MaterialID(1), 4) 

BuildMeshShadowVolume(0)

;The sandbox is ready
CreateEntity(0, MeshID(0), #PB_Material_None)
EntityPhysicBody(0, #PB_Entity_StaticBody, 1, 1, 1)
RotateEntity(0, Rx, 0, Rz)

;Ball
CreateEntity(1, MeshID(CreateSphere(#PB_Any, 0.7)), MaterialID(1), 0, 3, 0)
EntityPhysicBody(1, #PB_Entity_SphereBody, 0.5, 0.5, 0.2) 

Repeat
  Repeat
    Event  = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        End
        
    EndSelect
  Until Event = 0
  
  If ExamineKeyboard()
    If KeyboardPushed (#PB_Key_Escape)
      Break
    EndIf
    
    If KeyboardPushed (#PB_Key_Right)
      Rz - 0.2
    EndIf
    
    If KeyboardPushed (#PB_Key_Left)
      Rz + 0.2
    EndIf
    
    If KeyboardPushed (#PB_Key_Up)
      Rx - 0.2
    EndIf
    
    If KeyboardPushed (#PB_Key_Down)
      Rx + 0.2
    EndIf
    
  EndIf
  
  ;BugWare
  ApplyEntityImpulse(1, 0, 0, 0 )
  
  RotateEntity(0, Rx, 0, Rz)
  
  RenderWorld(40)
  FlipBuffers()  
ForEver
User avatar
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: make a compound object without softening its Physics Bod

Post by falsam »

One word ....... Wonderful.
applePi wrote:falsam, your version makes the life easier
This is not my version. This is YOUR work and i love it:)


PS : Procedure Name: I wonder if AddSubMesh() is more appropriate than AddMesh()

➽ 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: make a compound object without softening its Physics Bod

Post by applePi »

falsam writes:
Procedure Name: I wonder if AddSubMesh() is more appropriate than AddMesh()
i think AddMesh() are better, because AddSubMesh() are a purebasic keyword, also AddMesh() add a whole mesh with its vertex positions and the way the vertices are connecting to each other to form the triangles, also the UV and normals.
regarding the shadow additive i have posted here http://purebasic.fr/english/viewtopic.php?f=4&t=61258 asking an inspection for the working of the NormalizeMesh() and BuildMeshTangents() why the Shadow Additive can't see its job while see it when we do it getting the normals and tangents manually from the meshes and injecting it to the new united mesh !!.
User avatar
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Add Mesh to Mesh to make a compound Mesh

Post by falsam »

since PB 5.50 beta 1 and later the cube have six submeshes to make it easy to texture its faces separately. so i have edited only the first example below to be compatible with PB 5.50
I also have edited the AddMesh() procedure.
:arrow: http://www.purebasic.fr/english/viewtop ... 28#p458028

An other example.

Code: Select all

Declare AddMesh(Mesh, x.f=0 , y.f=0, z.f=0, ScaleX.f=1, ScaleY.f=1, ScaleZ.f=1, RotateX.f=0, RotateY.f=0, RotateZ.f=0)

InitEngine3D() : InitKeyboard() : InitSprite()
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)

OpenWindow(0,0,0,1024,768,"AddMesh ")
OpenWindowedScreen(WindowID(0),0,0,1024,768)

CreateCamera(0, 0, 0, 100, 100)
CameraBackColor(0, RGB(245, 222, 179))
MoveCamera(0, 0, 10, 20)
CameraLookAt(0, 0, 0, -10)
CreateLight(0, RGB(255, 255, 255), -100, 200, 100)
WorldShadows(#PB_Shadow_Additive)

;Textures & material
CreateMaterial(0, TextureID(LoadTexture(#PB_Any, "Dirt.jpg")))      ;Ground Texture
CreateMaterial(1, TextureID(LoadTexture(#PB_Any, "RustySteel.jpg")));

;Creation du sol - Create ground
CreatePlane(0, 40, 40, 1, 1, 10, 10)
CreateEntity(0, MeshID(0), MaterialID(0))
CreateEntityBody(0, #PB_Entity_StaticBody, 1, 1, 1)

;Creation d'un mesh vide - Create empty mesh
CreateMesh(1, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)

;Ajout mesh - Add mesh
AddMesh(CreateCube(#PB_Any, 1), -1, 0, 0)
AddMesh(CreateCube(#PB_Any, 1), 1,  0, 0)
AddMesh(CreateCylinder(#PB_Any, 0.1, 2), 0, 0, 0, 1, 1, 1, 0, 0, 90)

;Fin de la création du mesh - End of creation
FinishMesh(#True)

;Il est possible de sauvegarder le mesh - It is possible to save the mesh.
;SaveMesh(1, "test.mesh")

;Création de l'entité à une hauteur de 10 unités - Create the entity at a height of 10 units.
CreateEntity(1, MeshID(1), MaterialID(1), 0, 10, 0)

;Entité physique -  physical entity.
CreateEntityBody(1, #PB_Entity_BoxBody, 0.5, 0.5, 0.1) 

While #True  
  Repeat
    Event  = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        End
    EndSelect
  Until Event = 0     
  
  If ExamineKeyboard()  
    If KeyboardReleased(#PB_Key_Escape)
      Break
    EndIf    
  EndIf
    
  RenderWorld()
  FlipBuffers()
Wend

;Ajouter un mesh à un autre mesh - Add a mesh to another mesh
Procedure AddMesh(Mesh, x.f=0 , y.f=0, z.f=0, ScaleX.f=1, ScaleY.f=1, ScaleZ.f=1, RotateX.f=0, RotateY.f=0, RotateZ.f=0)
  Protected Dim MeshData.PB_MeshVertex(0)
  Protected Dim MeshDataInd.PB_MeshFace(0)
  Protected ArrSize, ArrSizeInd, c, i, mdx.f, mdy.f, mdz.f, subMesh.i
  
  For subMesh = 0 To SubMeshCount(Mesh)-1    
    TransformMesh(Mesh, x, y, z, ScaleX,ScaleY,ScaleZ, RotateX,RotateY,RotateZ, subMesh)
    GetMeshData(Mesh, subMesh, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate | #PB_Mesh_Normal, 0, MeshVertexCount(Mesh, subMesh)-1)
    GetMeshData(Mesh, subMesh, MeshDataInd(), #PB_Mesh_Face , 0, MeshIndexCount(Mesh, subMesh)-1)
    
    ArrSize = ArraySize(MeshData())
    For c=0 To ArrSize    
      mdx = MeshData(c)\x 
      mdy = MeshData(c)\y
      mdz = MeshData(c)\z
      MeshVertexPosition(mdx, mdy, mdz)
      
      mdx = MeshData(c)\NormalX 
      mdy = MeshData(c)\NormalY
      mdz = MeshData(c)\NormalZ
      MeshVertexNormal(mdx, mdy, mdz)
      
      mdx = MeshData(c)\TangentX
      mdy = MeshData(c)\TangentY
      mdz = MeshData(c)\TangentZ
      MeshVertexTangent(mdx, mdy, mdz)
      
      MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v)        
    Next   
    
    ArrSizeInd = ArraySize(MeshDataInd()) 
    For i=0 To ArrSizeInd Step 3
      MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
    Next
    AddSubMesh(#PB_Mesh_TriangleList)
  Next 
EndProcedure

➽ 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: Add Mesh to Mesh to make a compound Mesh

Post by applePi »

Thank you falsam very much for adding submeshes to the procedure addMesh since this makes it easier to use and more general. i have tested your procedure by adding facial.mesh and akm.mesh to your sandbox and it works okay, the number of submeshes is 40
insert these after the line 81 in your procedure demo above http://www.purebasic.fr/english/viewtop ... 28#p458028

Code: Select all

Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Models", #PB_3DArchive_FileSystem)
AddMesh(LoadMesh(#PB_Any, "AKM.mesh"), 0,1.2,0, 0.01,0.01,0.01) 
AddMesh(LoadMesh(#PB_Any, "facial.mesh"), 0,1.2,-4, 0.1,0.1,0.1)
the saved mesh as bigMesh.mesh
http://wikisend.com/download/482046/bigMesh.rar
can be opened with Ogre Meshy which show more infos about the mesh file
we can expand the many submeshes to one contineous flat submesh by renumbering the vertices to continue up and up from the last previous submesh and then refilling the indices with the new numbered vertices, even it works with most meshes but not with all, so there is may be some error somewhere in my logic in the following utility which open the bigMesh.mesh (40 submeshes) in the above wikisend link and transform it to one flat mesh, if we save it to OneFlatMesh.mesh by pressing "S" we can reload it to the same utility to check it

Code: Select all

Declare CreateMatrix()

#mesh = 9
#cameraSpeed = 0.1
Define.f KeyX, KeyY, MouseX, MouseY

InitEngine3D() 
InitSprite() 
  InitKeyboard()
  InitMouse()
  ExamineDesktops()
  OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "multi subMeshes to one submesh ...Press S To save the new flat mesh . press ESC To exit", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)
  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)
  Parse3DScripts()
  
  Global Dim MeshData.PB_MeshVertex(0)
  Global Dim MeshDataInd.PB_MeshFace(0)
  Global Dim MeshDataInd2.PB_MeshFace(0)

    CreateMaterial(2, LoadTexture(2, "Dr_Bunsen_Head.jpg")) ;Dr_Bunsen_Head.jpg
    MaterialCullingMode(2, #PB_Material_NoCulling)
        
  ; Light
    CreateLight(0, RGB(255,255,255), 0, 100, 30)
    AmbientColor(RGB(200, 200, 200))
  
  ; Camera
  CreateCamera(0, 0, 0, 100, 100)
  MoveCamera(0, 0, 2, 5, #PB_Absolute )
  CameraLookAt(0, 0, 0, 0)
  CameraBackColor(0, RGB(10, 200,200))
  
  CreateMatrix() ; call proc to merge several meshes, some have several submeshes

   SetEntityMaterial(0,MaterialID(2),0) ; 
       
    Repeat
      
      Repeat
        Event = WindowEvent()
        If Event = #PB_Event_CloseWindow
          Quit = 1
        EndIf
      Until Event = 0 
      
      If ExamineMouse()
        MouseX = -MouseDeltaX()/25
        MouseY = -MouseDeltaY()/25
      EndIf
          
      If ExamineKeyboard()
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#cameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #cameraSpeed
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#cameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #cameraSpeed
        Else
          KeyY = 0
        EndIf
        
        
       If KeyboardReleased(#PB_Key_S)
          SaveMesh(0, "OneFlatMesh.mesh")
        EndIf         
      EndIf
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(0, KeyX, 0, KeyY)
    RotateEntity(0, 0,0.5,0, #PB_Relative)
    RenderWorld()
    FlipBuffers()
  Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1 
  
  Procedure CreateMatrix()
    
    LoadMesh(#mesh, "BigMesh.mesh"); "facial.mesh") ;"sibenik.mesh")
    ;LoadMesh(#mesh, "OneFlatMesh.mesh")
    CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic) ; construct the new mesh
    
    For i=0 To SubMeshCount(#mesh) -1
      Debug "submesh"+"("+i+") = " + MeshVertexCount(#mesh,i)
    Next
    Debug "total number of vertices = " + MeshVertexCount(#mesh)
   
  ;TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
  TransformMesh(#mesh,0,0,0, 0.2,0.2,0.2,0,0,0)
  Debug "submesh count = " +SubMeshCount(#mesh) 
  
  ;AddSubMesh(#PB_Mesh_TriangleList)
  ;Debug SubMeshCount(#mesh)-1
  For subMesh=0 To SubMeshCount(#mesh)-1
  GetMeshData(#mesh,subMesh, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Color | #PB_Mesh_UVCoordinate | #PB_Mesh_Normal , 0, MeshVertexCount(#mesh,subMesh)-1)
  ;GetMeshData(#mesh,subMesh, MeshData(), #PB_Mesh_Vertex , 0, MeshVertexCount(#mesh,subMesh)-1)
  GetMeshData(#mesh,subMesh, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(#mesh, subMesh)-1)
  ArrSize = ArraySize(MeshData())
        
  For c=0 To ArrSize
      x.f = MeshData(c)\x 
      y.f = MeshData(c)\y
      z.f = MeshData(c)\z
      MeshVertexPosition(x,y,z)
      ;MeshVertexColor(MeshData(c)\Color
      MeshVertexNormal(MeshData(c)\NormalX, MeshData(c)\NormalY, MeshData(c)\NormalZ)
      MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v) 
  Next   
   
   ArrSizeInd = ArraySize(MeshDataInd()) 
   ;fill the big MeshDataInd2 with the info in Local MeshDataInd but rearranged sequently as we want one mesh instead of several submehes
   ReDim MeshDataInd2.PB_MeshFace(j+ArrSizeInd)
   For i=0 To ArrSizeInd
     MeshDataInd2(j)\Index = MeshDataInd(i)\Index + subMeshVertxIncr
     j+1
   Next
   
   subMeshVertxIncr = subMeshVertxIncr + MeshVertexCount(#mesh,subMesh)
   ;AddSubMesh(#PB_Mesh_TriangleList)
   Next
   
   ;after finishing loading the MeshDataInd2 with faces connections data we execute it and weaving the triangles (faces)
   ArrSizeInd2 = ArraySize(MeshDataInd2()) 
   ;Debug ArrSizeInd2
   For j=0 To  ArrSizeInd2 Step 3
     MeshFace(MeshDataInd2(j)\Index, MeshDataInd2(j+1)\Index,MeshDataInd2(j+2)\Index)
     
   Next
   
   FreeMesh(#mesh) 
   Debug "total vertices number after uniting submeshes = " + subMeshVertxIncr  
   Debug "submesh count after uniting merging submehes = " +SubMeshCount(0) 
      
  
  FinishMesh(#True)
      
  CreateEntity(0, MeshID(0), MaterialID(2), 0,0,0)
  
  ;CreateEntityBody(0, #PB_Entity_StaticBody, 1, 0.2, 1)

EndProcedure
User avatar
Psychophanta
Addict
Addict
Posts: 4977
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Add Mesh to Mesh to make a compound Mesh

Post by Psychophanta »

One of my firsts approachings to the PB 3D native when i am migrating to it from MP3D engine, which is an INCREDIBLE PAIN IN BRAIN!
AddMesh() function with no submeshes, please test and improve:

Code: Select all

Procedure AddMesh(Mesho.i,Mesh.i)
  Protected Dim MeshoDataV.PB_MeshVertex(0),Dim MeshoDataF.PB_MeshFace(0),Dim MeshDataV.PB_MeshVertex(0),Dim MeshDataF.PB_MeshFace(0),i.i,maxindex.i
  GetMeshData(Mesho,0, MeshoDataV(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate| #PB_Mesh_Normal|#PB_Mesh_Color, 0, MeshVertexCount(Mesho, 0)-1)
  GetMeshData(Mesho,0, MeshoDataF(), #PB_Mesh_Face, 0, MeshIndexCount(Mesho, 0)-1)
  GetMeshData(Mesh,0, MeshDataV(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate| #PB_Mesh_Normal|#PB_Mesh_Color, 0, MeshVertexCount(Mesh, 0)-1)
  GetMeshData(Mesh,0, MeshDataF(), #PB_Mesh_Face, 0, MeshIndexCount(Mesh, 0)-1)
  FreeMesh(Mesh)
  Mesh.i=CreateMesh(#PB_Any,#PB_Mesh_TriangleList,#PB_Mesh_Static)
  For i=0 To ArraySize(MeshDataV())
    With MeshDataV(i)
    MeshVertexPosition(\x,\y,\z)
    MeshVertexNormal(\NormalX,\NormalY,\NormalZ)
    MeshVertexTextureCoordinate(\u,\v) 
    MeshVertexColor(\Color)
    EndWith
  Next
  For i=0 To ArraySize(MeshoDataV())
    With MeshoDataV(i)
    MeshVertexPosition(\x,\y,\z)
    MeshVertexNormal(\NormalX,\NormalY,\NormalZ)
    MeshVertexTextureCoordinate(\u,\v) 
    MeshVertexColor(\Color)
    EndWith
  Next
  For i=0 To ArraySize(MeshDataF()) Step 3
    MeshFace(MeshDataF(i)\Index,MeshDataF(i+1)\Index,MeshDataF(i+2)\Index)
    If MeshDataF(i)\Index>maxindex:maxindex=MeshDataF(i)\Index:EndIf
    If MeshDataF(i+1)\Index>maxindex:maxindex=MeshDataF(i+1)\Index:EndIf
    If MeshDataF(i+2)\Index>maxindex:maxindex=MeshDataF(i+2)\Index:EndIf
  Next
  maxindex+1
  For i=0 To ArraySize(MeshoDataF()) Step 3
    MeshFace(MeshoDataF(i)\Index+maxindex,MeshoDataF(i+1)\Index+maxindex,MeshoDataF(i+2)\Index+maxindex)
  Next
  FinishMesh(1)
  NormalizeMesh(Mesh)
  UpdateMeshBoundingBox(Mesh)
EndProcedure
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Add Mesh to Mesh to make a compound Mesh

Post by applePi »

Thank you Psychophanta , this is nice, here is the sphere inside the cube, and when we save the mesh with savemesh and checking the mesh file with Ogre Meshy tool we see it contains only submesh 0 ie one submesh which is the main
also Debug SubMeshCount(Mesh) will output 1

Code: Select all

Declare AddMesh(Mesho.i,Mesh.i)
InitEngine3D()
InitSprite()
InitKeyboard()

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/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()
 
OpenWindow(0, 0, 0, 800, 600, "merge 2 meshes", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)

CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 0, 3, 7)
CameraLookAt(0, 0,0,0)

;CreateLight(#Light, Color [, x, y, z [, Flags]])
CreateLight(1, RGB(220,220,220), 50,100,20)

CreateMaterial(0, LoadTexture(0, "White.jpg"))
MaterialCullingMode(0,#PB_Material_NoCulling)


Mesho = CreateCube(#PB_Any, 2)
Mesh = CreateSphere(#PB_Any, 1)
;uncomment the following line to position the sphere over the cube
;TransformMesh(Mesh, 0,2,0,1,1,1,0,0,0)

AddMesh(Mesho, Mesh)
CreateEntity(1, MeshID(Mesh), MaterialID(0), 0, 0, 0)

CameraRenderMode(0, #PB_Camera_Wireframe)

;SaveMesh(Mesh, "test.mesh")
;Debug SubMeshCount(Mesh)
Repeat
    Repeat
    event = WindowEvent()
  Until event = 0
  
  RotateEntity(1, 0,1,0, #PB_Relative)
  
  RenderWorld()
  
  FlipBuffers()
  
  ExamineKeyboard()
  
Until KeyboardPushed(#PB_Key_Escape)

Procedure AddMesh(Mesho.i,Mesh.i)
  Protected Dim MeshoDataV.PB_MeshVertex(0),Dim MeshoDataF.PB_MeshFace(0),Dim MeshDataV.PB_MeshVertex(0),Dim MeshDataF.PB_MeshFace(0),i.i,maxindex.i
  GetMeshData(Mesho,0, MeshoDataV(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate| #PB_Mesh_Normal|#PB_Mesh_Color, 0, MeshVertexCount(Mesho, 0)-1)
  GetMeshData(Mesho,0, MeshoDataF(), #PB_Mesh_Face, 0, MeshIndexCount(Mesho, 0)-1)
  GetMeshData(Mesh,0, MeshDataV(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate| #PB_Mesh_Normal|#PB_Mesh_Color, 0, MeshVertexCount(Mesh, 0)-1)
  GetMeshData(Mesh,0, MeshDataF(), #PB_Mesh_Face, 0, MeshIndexCount(Mesh, 0)-1)
  FreeMesh(Mesh)
  Mesh.i=CreateMesh(#PB_Any,#PB_Mesh_TriangleList,#PB_Mesh_Static)
  For i=0 To ArraySize(MeshDataV())
    With MeshDataV(i)
    MeshVertexPosition(\x,\y,\z)
    MeshVertexNormal(\NormalX,\NormalY,\NormalZ)
    MeshVertexTextureCoordinate(\u,\v) 
    MeshVertexColor(\Color)
    EndWith
  Next
  For i=0 To ArraySize(MeshoDataV())
    With MeshoDataV(i)
    MeshVertexPosition(\x,\y,\z)
    MeshVertexNormal(\NormalX,\NormalY,\NormalZ)
    MeshVertexTextureCoordinate(\u,\v) 
    MeshVertexColor(\Color)
    EndWith
  Next
  For i=0 To ArraySize(MeshDataF()) Step 3
    MeshFace(MeshDataF(i)\Index,MeshDataF(i+1)\Index,MeshDataF(i+2)\Index)
    If MeshDataF(i)\Index>maxindex:maxindex=MeshDataF(i)\Index:EndIf
    If MeshDataF(i+1)\Index>maxindex:maxindex=MeshDataF(i+1)\Index:EndIf
    If MeshDataF(i+2)\Index>maxindex:maxindex=MeshDataF(i+2)\Index:EndIf
  Next
  maxindex+1
  For i=0 To ArraySize(MeshoDataF()) Step 3
    MeshFace(MeshoDataF(i)\Index+maxindex,MeshoDataF(i+1)\Index+maxindex,MeshoDataF(i+2)\Index+maxindex)
  Next
  FinishMesh(1)
  NormalizeMesh(Mesh)
  UpdateMeshBoundingBox(Mesh)
EndProcedure
User avatar
Psychophanta
Addict
Addict
Posts: 4977
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Add Mesh to Mesh to make a compound Mesh

Post by Psychophanta »

Indeed applePi.
I wanted a function without submeshes, but just a single mesh. And that's it.
Thank you!

I must learn more about PB 3D...
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
Post Reply