i like the idea of tracing a terrain with a ray like the satellite tracing the earth surface, but now i have done this exprement with RayCast.pb example as suggested by Comtois by tracing the sphere by moving the x-ray device 0.2 down every trace line, and i got a sphere front approximation like this:
it is great that PickX() and sisters returns the 'x'/'y'/'z' positions of the picked object in world coordinates.
Code: Select all
;
; ------------------------------------------------------------
;
; PureBasic - RayCast
;
; (c) 2012 - Fantaisie Software
;
; ------------------------------------------------------------
;
IncludeFile "Screen3DRequester.pb"
#CameraSpeed = 0.4
#N = 2
Enumeration
#MainWindow
#Editor
EndEnumeration
Define.f KeyX, KeyY, MouseX, MouseY, RatioX, RatioY, SpeedRotate
If InitEngine3D()
Add3DArchive("Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive("Data/Models", #PB_3DArchive_FileSystem)
Add3DArchive("Data/Scripts", #PB_3DArchive_FileSystem)
Add3DArchive("Data/GUI", #PB_3DArchive_FileSystem)
Parse3DScripts()
InitSprite()
InitKeyboard()
InitMouse()
If Screen3DRequester()
KeyboardMode(#PB_Keyboard_International)
;WorldDebug(#PB_World_DebugEntity)
;-Materials
;
GetScriptMaterial(0, "Color/Blue")
GetScriptMaterial(1, "Color/Green")
GetScriptMaterial(2, "Color/Red")
GetScriptMaterial(3, "Color/Yellow")
CreateMaterial(4, LoadTexture(0, "Dirt.jpg"))
;-Meshes
CreateSphere(1, 2)
;-Entities
CreateEntity(1, MeshID(1), MaterialID(1), -4, 1, 0)
CreateNode(0, 3, 2.8, 0)
CreateNode(1, -1, 0, -1)
AttachNodeObject(0, NodeID(1))
;-Camera
CreateCamera(0, 0, 0, 100, 100)
CameraBackColor(0, RGB(40, 30, 60))
MoveCamera(0, -1, 8, 15, #PB_Absolute)
CameraLookAt(0, -1, 0, 0)
;-Light
CreateLight(0, RGB(255, 255, 255), 1560, 900, 500)
AmbientColor(RGB(50, 50, 50))
;-GUI
RatioX = CameraViewWidth(0) / 640
RatioY = CameraViewHeight(0) / 480
OpenWindow3D(#MainWindow, 0, 0, 360 * RatioX, 110 * RatioY, "RayCast")
StringGadget3D(#Editor, 10 * RatioX, 20 * RatioY, 300 * RatioX, 40 * RatioY, "", #PB_String3D_ReadOnly)
ShowGUI(155, 0)
CreateMesh(3, #PB_Mesh_PointList, #True)
SkyDome("snow_1024.jpg", 100) ;for blue color background
Global traces
Repeat
Screen3DEvents()
Repeat
Event3D = WindowEvent3D()
Until Event3D = 0
If ExamineMouse()
MouseX = -MouseDeltaX() * #CameraSpeed * 0.5
MouseY = -MouseDeltaY() * #CameraSpeed * 0.5
EndIf
If KeyboardPushed(#PB_Key_Space)
Stop = 0
Else
Stop = 1
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
If fin=0
Entity = RayCast(NodeX(0), NodeY(0), NodeZ(0), NodeX(1)-NodeX(0), 0, NodeZ(1)-NodeZ(0), -1)
CreateLine3D(10, NodeX(0), NodeY(0), NodeZ(0), RGB(0, 0, 255), NodeX(1), NodeY(1), NodeZ(1), RGB(0, 0, 255))
If entity = 1
SpeedRotate = 0.1
Else
SpeedRotate = 1
EndIf
If Entity>=0
CreateLine3D(10, NodeX(0), NodeY(0), NodeZ(0), RGB(0, 255, 255), PickX(), PickY(), PickZ(), RGB(0, 255, 255))
x.f=PickX():y.f=PickY():z.f=PickZ()
SetGadgetText3D(#Editor, "Z= " + StrF(PickZ())+ " traces = "+Str(traces))
MeshVertexPosition(x+8, y, z);
MeshVertexColor(RGB(255,0,0))
MeshVertexNormal(x+8, y, z)
Else
;SetGadgetText3D(#Editor, "I'm looking...")
EndIf
rot.f+0.5
If rot=80 :rot=0:pos.f+0.2:traces+1:MoveNode(0, 3, 2.8-pos, 0,#PB_Absolute):EndIf
RotateNode(0, 0, rot, 0)
EndIf
If KeyboardReleased(#PB_Key_Z) Or traces = 15
traces = 0
fin=1
FinishMesh(#True)
CreateEntity(3, MeshID(3), MaterialID(2))
MoveEntity(3,0,0,0)
EndIf
RotateNode(0, 0, SpeedRotate * Stop, 0, #PB_Relative)
RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
MoveCamera (0, KeyX, 0, KeyY)
RenderWorld()
Screen3DStats()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
EndIf
Else
MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf
End