addMesh suggested function

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

addMesh suggested function

Post by applePi »

addMesh : add meshes together, while preserving the physics, i prefer a native function than a procedure, since it is making the examples easier and shorter.
here 2000 cubes added together with a sphere trapped inside. with addMesh it is just addMesh(x,y) 2000 times.
just a show case for the benefits of addMesh.

Code: Select all

Enumeration
   
   #LIGHT
   #CAMERA
   #mainwin
   #Plane
   #sphere
  
EndEnumeration
#cameraSpeed = 4

Define.f KeyX, KeyY, MouseX, MouseY

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

Define.f KeyX, KeyY

Declare CreateMatrix()


InitEngine3D()
   InitMouse()
   InitKeyboard()
   InitSprite()
   ExamineDesktops()
   
OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "arrow keys+mouse: move/rotate camera.... X/Z rotate object", #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\GUI",#PB_3DArchive_FileSystem)
         Parse3DScripts()
         
         CreateCamera(0,0,0,100,100)
         CameraBackColor(0,RGB(0, 0, 0))
         MoveCamera(0,10,100,160)
         
         CreateLight(0,RGB(255, 255, 95),-50,50,100)
         ;;============================================
         OpenWindow3D(0,0,0,300,120,"Engine Stats")
         TextGadget3D(0,2,0,300,30,"")
         TextGadget3D(1,2,0,300,60,"")
         ShowGUI(255,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, 200, 250, #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, "ValetCoeur.jpg")) 
    MaterialCullingMode(1, #PB_Material_NoCulling)
    DisableMaterialLighting(1, #False)
    
    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()
    For i=0 To 2000
      SetEntityMaterial(0,MaterialID(Random(5)), i) 
      
    Next
  
    
    WorldGravity(-20)
        
    CreateSphere(#sphere, 10)
    CreateEntity(#sphere, MeshID(#sphere), MaterialID(5))
    
    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 = -#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 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
        RotateEntity(0, 0.0,0.5,0.0,#PB_Relative)
        RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(#Camera, KeyX, 0, KeyY)
      
      RenderWorld()
      FlipBuffers()
      ;;==============================================
            SetGadgetText3D(0,"FPS: "+StrF(Engine3DStatus(#PB_Engine3D_CurrentFPS )))
            SetGadgetText3D(1,"Tris: "+StrF(Engine3DStatus(#PB_Engine3D_NbRenderedTriangles))) 
      ;;================================================
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
 
End


Procedure CreateMatrix()
  CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
  
  CreateCube(30,5)
   
   While count <=2000
   
  AddSubMesh(#PB_Mesh_TriangleList)
  
  CopyMesh(30,31)
  TransformMesh(31,Random(200)-100,Random(200)-100,Random(200)-100, 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
   
 count+1  
 Wend
 
   
  FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(1))
    
  CreateEntity(0, MeshID(0), MaterialID(1), 0,-10,0)
  
  EntityPhysicBody(0, #PB_Entity_StaticBody, 1, 0.2, 1)
  
EndProcedure
Last edited by applePi on Tue Mar 24, 2015 1:26 pm, edited 2 times in total.
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: addMesh suggested function

Post by DK_PETER »

That would be cool.
+1
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.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: addMesh suggested function

Post by Psychophanta »

+1
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply