Half Sphere , or less or more

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

Half Sphere , or less or more

Post by applePi »

we may needs part of a sphere, here we use the sphere mesh made from PB CreateSphere and we divide it to parts (sphereParts) and we display the upper part.
the pyramid CreateSpherePlus(4, 5, 4, 2, 1, 2) are made from 4 segments 2 Rings, 2 parts, and we display the upper part
note that one of the spheres have 2 holes at north and south poles.
try to go inside the spheres
Image

Code: Select all

Enumeration
   
   #LIGHT
   #CAMERA
   #WorkSphereMesh = 555
   
EndEnumeration

#CameraSpeed = 0.5
Define.f KeyX, KeyY, MouseX, MouseY

Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
Global mesh
ExamineDesktops()
If OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), " ... W.... to WireFrame/ SolidFrame ......use mouse + Arrows to rotate + move Camera ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

Define.f KeyX, KeyY

Declare CreateSpherePlus(mesh, radius.f, Segments, Rings, start.f, sphereParts.f)


If InitEngine3D()
  
  Add3DArchive(".", #PB_3DArchive_FileSystem)
  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, 0, 10, 18, #PB_Absolute)
    CameraFOV(#camera, 70)
    CameraBackColor(#camera, RGB(255,200,200))
    CameraLookAt(#camera,0,0,0)
        
    CreateLight(0, RGB(255,255,255), 20, 100, 0)
    AmbientColor(RGB(200, 200, 200))
         
    ;CreateMaterial(5, LoadTexture(5, "Geebee2.bmp"))
    ;CreateMaterial(5, LoadTexture(5, "DosCarte.png")) 
    CreateMaterial(5, LoadTexture(5, "fw12b.jpg"))
    MaterialCullingMode(5, #PB_Material_NoCulling)
    MaterialShadingMode(5, #PB_Material_Wireframe)
    
    ;CreateSpherePlus(mesh, radius.f, Segments, Rings, start, sphereParts)
    CreateSpherePlus(0, 6, 32,32, 1,1) ; Full sphere
    CreateEntity(0, MeshID(#WorkSphereMesh), MaterialID(5) , 8,6,0)
        
    CreateSpherePlus(1, 5, 32,32, 1,2) ; half sphere
    CreateEntity(1, MeshID(#WorkSphereMesh), MaterialID(5) , -7,5,0)
    
    CreateSpherePlus(2, 5, 32,32, 1,4) ; several rings from the sphere
    CreateEntity(2, MeshID(#WorkSphereMesh), MaterialID(5) , -11,-8,0)
    
    CreateSpherePlus(3, 5, 32,32, 1,32) ; sphere north pole only
    CreateEntity(3, MeshID(#WorkSphereMesh), MaterialID(5) , 0,-46,0)
    ScaleEntity(3, 7,7,7)
    
    CreateSpherePlus(4, 5, 4, 2, 1, 2) ; pyramid
    CreateEntity(4, MeshID(#WorkSphereMesh), MaterialID(5) , 0,-2,0)
    
    CreateSpherePlus(5, 5, 32,32, 400, 1.104) ; with two holes
    CreateEntity(5, MeshID(#WorkSphereMesh), MaterialID(5) , 10,-9,0) 
    
      
    wireFrame = 0
  
    Repeat
      Event = WindowEvent()
        
      If ExamineMouse()
        MouseX = -MouseDeltaX()/20 
        MouseY = -MouseDeltaY()/20
      EndIf
                
      If ExamineKeyboard()
        
        If KeyboardReleased(#PB_Key_W)
          If wireFrame
          MaterialShadingMode(5, #PB_Material_Wireframe)
          wireFrame ! 1
            Else 
          MaterialShadingMode(5, #PB_Material_Solid)
          wireFrame ! 1
        EndIf
        EndIf
        
        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
        
        RotateEntity(0,0,0.4,0, #PB_Relative)
        RotateEntity(1,0,0.4,0, #PB_Relative)
        RotateEntity(2,0,0.4,0, #PB_Relative)
        RotateEntity(3,0,0.4,0, #PB_Relative)
        RotateEntity(4,0,0.4,0, #PB_Relative)
        RotateEntity(5,0,0.4,0, #PB_Relative)
        
        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 CreateSpherePlus(mesh, radius.f, Segments, Rings, start.f, sphereParts.f)
  CreateMesh(#WorkSphereMesh, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
  
  CreateSphere(mesh,radius,Segments, Rings)

  ;TransformMesh(mesh,0,0,0, 1,1,1,0,0,0)
  GetMeshData(mesh,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate , 0, MeshVertexCount(mesh)-1)
  GetMeshData(mesh,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(mesh, 0)-1)
  ArrSize = ArraySize(MeshData())
  ;Debug ArrSize
  
  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()) 
  ;Debug ArrSizeInd
  ArrSizeInd = ArrSizeInd / sphereParts ; sphereParts is the number of sphere divisions we want
    
  For i=start To ArrSizeInd Step 3
     indx = i-1
     MeshFace(MeshDataInd(indx)\Index, MeshDataInd(indx+1)\Index,MeshDataInd(indx+2)\Index)
   Next
   
  FinishMesh(#True)
  
  
EndProcedure
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: Half Sphere , or less or more

Post by Crusiatus Black »

That's cool, well done!
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Half Sphere , or less or more

Post by davido »

@applePi,
Thank you for another impressive example. :D
DE AA EB
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Half Sphere , or less or more

Post by IdeasVacuum »

That's very interesting ApplePi, stepping into the realm of CAD. 8)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5524
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Half Sphere , or less or more

Post by Kwai chang caine »

Splendid !!!
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
User avatar
falsam
Enthusiast
Enthusiast
Posts: 635
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Half Sphere , or less or more

Post by falsam »

Kwai chang caine wrote:Splendid !!!
Thanks for sharing 8)
+1 Thank :)

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

Sorry for my bad english and the Dunning–Kruger effect 🤪
Post Reply