triangular Flag, + Cone to fill with balls

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

triangular Flag, + Cone to fill with balls

Post by applePi »

the same like the PB flag example but using a scaled cone and applying formula to change its vertices z coordinates so it behaves like a flag in real time
note that:

Code: Select all

For i=961 To 992
      MeshData(i)\x = 0: MeshData(i)\y = -20: MeshData(i)\z = 0
    Next 
will squeeze the base vertices to one unnoticed point, since the base does not contribute in waves, comment the above lines and rotate around the flag to see the distorted cone Base

Image

Code: Select all

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

#CameraSpeed = 1
#NbX=30
#NbZ=30

#Camera   = 0
#Entity   = 0
#Light    = 0
#Material = 0
#Mesh     = 0

Define.f KeyX, KeyY, MouseX, MouseY

Global Dim MeshData.PB_MeshVertex(0) 

Declare UpdateMatrix()

If InitEngine3D()
 
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/skybox.zip", #PB_3DArchive_Zip)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
  Parse3DScripts()
 
  InitSprite()
  InitKeyboard()
  InitMouse()
 
  If Screen3DRequester()
   
    ;-Material 
    GetScriptMaterial(#Material, "Scene/GroundBlend")
    ;MaterialCullingMode(#Material, #True) 
    MaterialShadingMode(#Material, #PB_Material_Wireframe)
   
    ;-Mesh
 
    CreateCone(#Mesh, 10, 20, 30,30)
    
    CreateEntity(#Mesh, MeshID(#Mesh), MaterialID(#Material))
    RotateEntity(#Mesh,180,0,90,#PB_Absolute)
    ScaleEntity(#Mesh, 3, 3, 0.2)
    GetMeshData(#Mesh,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(#Mesh)-1)
    
    ;-Camera
    CreateCamera(#Camera, 0, 0, 100, 100)
    MoveCamera(#Camera, 0, 50, 100, #PB_Absolute)
    CameraLookAt(#Camera, 0, 0, 0)
    CameraBackColor(#Camera, RGB(90, 0, 0))
   
    ;-Light
    CreateLight(#Light, RGB(255, 255, 255), 20, 150, 120)
    AmbientColor(RGB(90, 90, 90))
   
    ;- Skybox
    SkyBox("stevecube.jpg")
    
    ;sqeezing the base vertices to one unnoticed point
    For i=961 To 992
      MeshData(i)\x = 0: MeshData(i)\y = -20: MeshData(i)\z = 0
    Next  
      
    SetMeshData(#Mesh, 0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(#Mesh)-1)
    UpdateMeshBoundingBox(#Mesh)
    
       
    Repeat
      Screen3DEvents()
     
      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
      
       
      EndIf
     
      If ExamineMouse()
        MouseX = -(MouseDeltaX()/10)
        MouseY = -(MouseDeltaY()/10)
      EndIf
      MoveCamera  (#Camera, KeyX, 0, KeyY)
      RotateCamera(#Camera,  MouseY, MouseX, 0, #PB_Relative) 
     
      ; Waves
      UpdateMatrix()
           
      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 UpdateMatrix()
  Protected.l a, b, c
  dtime.f=ElapsedMilliseconds()/600 ; reduce 600 to get speedier animation
  
  For b=0 To #Nbz
    For a=0 To #NbX
      xa.f=MeshData(c)\x
      zb.f=MeshData(c)\y
      
      MeshData(c)\z=Cos(dtime + (Sqr(Pow(0.5*xa,2)+Pow(0.5*zb,2))))*30
      c + 1
    Next a
  Next b
  
  SetMeshData(#Mesh, 0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(#Mesh)-1)
EndProcedure
second cone example is to move the base center vertex up to the cone tip, so we can use it as a container.
note that here we need only DisableEntityBody(101, 0) to let the first sphere alive, look closely and you see it vibrating so it affects the other spheres and keeping it alive.
Image

Code: Select all

Enumeration
   #camera
   #plane
   #cone
   #sphere
EndEnumeration

Define.f KeyX, KeyY, MouseX, MouseY

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

ExamineDesktops()
If OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), " ... Z/X rotate the cone , ..... use arrow keys and mouse to move/rotate the camera", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts",#PB_3DArchive_FileSystem)
    
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)
                  
    CreateCamera(#camera, 0, 0, 100, 100)
    MoveCamera(#camera, -80, 40, 20, #PB_Absolute)
    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(4, LoadTexture(4, "ground_diffuse.png"))
    ;MaterialCullingMode(4, #PB_Material_NoCulling)
    ;MaterialShadingMode(4, #PB_Material_Wireframe)
    CreateMaterial(5, LoadTexture(5, "Geebee2.bmp"))
    MaterialCullingMode(5, #PB_Material_NoCulling)
    ;MaterialShadingMode(5, #PB_Material_Wireframe)
    CreateMaterial(6, LoadTexture(6, "snow_1024.jpg"))
    MaterialCullingMode(6, #PB_Material_NoCulling)
            
    CreatePlane(#plane, 200, 200, 5, 5, 2, 2) ; the ground
    CreateEntity(#plane, MeshID(#plane), MaterialID(6), 0,-15,0)
    CreateEntityBody(#plane, #PB_Entity_StaticBody, 1, 0.5, 1)
    
    ;CreateCone(#Mesh, Radius, Height [, NbBaseSegments, NbHeightSegments])
    CreateCone(#cone, 10, 20, 20,20)
    CreateEntity(#cone, MeshID(#cone), MaterialID(4), 0,1,0)
    RotateEntity(#cone, 0,0,180)
    
    GetMeshData(#cone,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(#cone)-1)
    ;GetMeshData(#cone,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(#cone, 0)-1)
    ;Debug MeshVertexCount(#cone)-1 ;462
    ;Debug MeshIndexCount(#cone)-1  ; 2459
    
    ;reposition of cone base center vertex so all base triangles stretch with it making the hollow cone
    MeshData(441)\x = 0: MeshData(441)\y = 10: MeshData(441)\z = 0
     
    SetMeshData(#cone, 0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(#cone)-1)
    UpdateMeshBoundingBox(#cone)
    CreateEntityBody(#cone, #PB_Entity_StaticBody  ,1,1,1)
        
    CreateSphere(#sphere, 1)
    For i=101 To 300 ; 200 spheres to fill the cone
      CreateEntity(i, MeshID(#sphere), MaterialID(5) , 0, 20+r,0)
      CreateEntityBody(i, #PB_Entity_SphereBody, 1, 0.5, 1)
      r+5
    Next
  
    
    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)
          
          RotateEntity(#cone, 0,0,0.5, #PB_Relative)
          ElseIf KeyboardPushed(#PB_Key_X)
          RotateEntity(#cone, 0,0, -0.5, #PB_Relative)
          
        EndIf
        
       EndIf
       
      RotateCamera(#camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(#camera, KeyX, 0, KeyY)
      
      ;enough to DisableEntityBody(101, 0) since its tiny vibration will affect other spheres and keep them alive (while inside the cone)
      ;For i=101 To 300
         ;DisableEntityBody(i, 0)
          DisableEntityBody(101, 0)
      ;Next
    
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End
the last example is the tool i used to know the number of some vertex, pressing V or C will move a sphere to vertex 0, 1, 2, 3, etc. from it i know that the base center vertex number is 441

Code: Select all

Enumeration
   #camera
   #Plane
      
EndEnumeration


Define.f KeyX, KeyY, MouseX, MouseY

Global Dim MeshData.PB_MeshVertex(0) 

ExamineDesktops()
If OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "press V or C to maove the sphere over vertices .use arrow keys and mouse to move the camera", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

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(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)
    
              
    CreateCamera(#camera, 0, 0, 100, 100)
    MoveCamera(#camera, 0, 10, 60, #PB_Absolute)
    CameraBackColor(#camera, RGB(255,200,200))
    CameraLookAt(#camera,-10,0,0)
        
    CreateLight(0, RGB(255,255,255), 0, 20, 30)
    AmbientColor(RGB(200, 200, 200))
    
    CreateMaterial(4, LoadTexture(4, "ground_diffuse.png"))
    MaterialCullingMode(4, #PB_Material_NoCulling)
    CreateMaterial(5, LoadTexture(5, "Geebee2.bmp"))
    MaterialCullingMode(5, #PB_Material_NoCulling)
    MaterialShadingMode(5, #PB_Material_Wireframe)
    CreateMaterial(6, LoadTexture(6, "MRAMOR6X6.jpg"))
    MaterialCullingMode(6, #PB_Material_NoCulling)
    MaterialShadingMode(4, #PB_Material_Wireframe)
    
    ;CreateCone(#Mesh, Radius, Height [, NbBaseSegments, NbHeightSegments])
    CreateCone(5, 10, 20, 20,20)
    TransformMesh(5, 0,0,0, 2,2,2,0,0,0)
    CreateEntity(5, MeshID(5), MaterialID(5), 0,0,0)
       
    GetMeshData(5,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(5)-1)
       
    CreateSphere(2000, 0.5)
    CreateEntity(2000, MeshID(2000), #PB_Material_None )
   
    vert = 0
    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_V) 
          Debug vert
          MoveEntity(2000, MeshData(vert)\x, MeshData(vert)\y, MeshData(vert)\z, #PB_Absolute)
          vert + 1
          If vert=MeshVertexCount(5, 0):vert=0:EndIf
          
          
        EndIf
        
        If KeyboardReleased(#PB_Key_C) 
          Debug vert
          MoveEntity(2000, MeshData(vert)\x, MeshData(vert)\y, MeshData(vert)\z, #PB_Absolute)
          vert + 1
          If vert=MeshVertexCount(5, 0):vert=0:EndIf
          
          
        EndIf
       
       EndIf
       
      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
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: triangular Flag, + Cone to fill with balls

Post by IdeasVacuum »

You should publish an eBook applePi, you are excellent at this stuff.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: triangular Flag, + Cone to fill with balls

Post by applePi »

Thanks IdeasVacuum , in fact we don't need to scale the cone entity, since all the cone vertices subjected to the same waves formula, and the opposite vertices in 3D have the same x,y which the waves formula are using to calculate the z value.
when we change the mesh, the entity follow it because it is an instance of the mesh . and whatever happened to the mesh will affect the entity.
look at these which results in visually equal animated Flag
ScaleEntity(#Mesh, 3, 3, 3)
MeshData(c)\z=Cos(dtime + (Sqr(Pow(0.5*xa,2)+Pow(0.5*zb,2))))* 2


ScaleEntity(#Mesh, 3, 3, 0.2)
MeshData(c)\z=Cos(dtime + (Sqr(Pow(0.5*xa,2)+Pow(0.5*zb,2))))* 30

but ScaleEntity(#Mesh, 3, 3, 0.0)
will show static Flag

also i hide the other side of the Cone by commenting this:
MaterialCullingMode(#Material, #True)

there are many other notes .
if someone can animate the plane made from CreatePlane please post it.
i will use the plane from CreatePlane to plot the math functions easily since we can texture the plane, and we can show the grid easily.
in the Docs:
#PB_Mesh_Static : once created, the mesh can't be modified anymore with the mesh update functions (default).
#PB_Mesh_Dynamic: once created, the mesh can be modified with the mesh update functions.
Post Reply