Half Sphere , or less or more
Posted: Thu Mar 05, 2015 6:03 am
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

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

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