;; ----------------------------------------------------------------------------
;; Irrlicht Wrapper For Imperative Languages - Purebasic Examples
;; IrrlichtWrapper and FreeBasic-Example by Frank Dodd (2006)
;; Improved IrrlichtWrapper - Michael Taupitz (2007)
;; PureBasic-Examples by Michael Taupitz (neotoma) & Marius Eckardt (Thalius) 
;; ----------------------------------------------------------------------------
;; Example 20 : Getting the point of collision
;; This example gets the point of collision between a selector group And a ray
;; we cast out into the scene through the center of the camera
;; ----------------------------------------------------------------------------

;; ////////////////////////////////////////////////////////////////////////////
;; Include the Irr3D-Requester
XIncludeFile "Irr3DRequester.pb"


EnableExplicit

; irrlicht objects
Define *MD2Mesh.irr_mesh
Define *MeshTexture.irr_texture
Define *TestTexture.irr_texture
Define *SceneNode.irr_node
Define *TestNode.irr_node
Define *OurCamera.irr_camera
Define *CollisionGroup.irr_selector
Define StartVector.IRR_VECTOR
Define EndVector.IRR_VECTOR
Define CollideAt.IRR_VECTOR

; -----------------------------------------------------------------------------
; To Start the Irrlicht-Device we use the Irr3DRequester, to make it more comfortable
; for the Users, and also easier to test different resolutions and devices.
If Irr3DRequester()

  ; send the window caption
  IrrSetWindowCaption( "Example 20: Getting the point of collision" )
  
  ; load a mesh
  *MD2Mesh = IrrGetMesh( "media\sydney.md2" )
  
  ; load texture resources for texturing models
  *MeshTexture = IrrGetTexture( "media\sydney.bmp" )
  *TestTexture = IrrGetTexture( "media\particlered.bmp" )
  
  ; add the mesh to the scene
  *SceneNode = IrrAddMeshToScene( *MD2Mesh )
  
  ; add a test node to the scene, we are going to use this test node as the
  ; marker for the point that is detected by the collision
  *TestNode = IrrAddCubeSceneNode(10)
  ; rescale the node so that it is an unobtrusive size
  IrrSetNodeScale( *TestNode, 0.2, 0.2, 0.2 )
  
  ; apply materials to the objects
  IrrSetNodeMaterialTexture( *SceneNode, *MeshTexture, 0 )
  IrrSetNodeMaterialTexture( *TestNode, *TestTexture, 0 )
  
  ; switch off lighting effects on the model and our TestNode ( Cube )
  IrrSetNodeMaterialFlag( *SceneNode, #IRR_EMF_LIGHTING, #IRR_OFF )
  IrrSetNodeMaterialFlag( *TestNode, #IRR_EMF_LIGHTING, #IRR_OFF )
  
  ; Make our Box Transparent where our Texture is Black / ADDITIVE Blending.. ;)
  IrrSetNodeMaterialType ( *TestNode, #IRR_EMT_TRANSPARENT_ADD_COLOR  )
  
  ; stop the animation playing
  IrrSetNodeAnimationRange( *SceneNode, 0,0)
  
  ; add a first person camera into the scene and point it at the model
  *OurCamera = IrrAddFPSCamera()
  IrrSetNodePosition( *OurCamera, 50,0,0 )
  IrrSetCameraTarget( *OurCamera, 0,0,0 )
  
  ; create a simple collision group from the triangles containing within the
  ; bounding box of the model
  *CollisionGroup = IrrGetCollisionGroupFromBox( *SceneNode )
  
  ; hide the mouse from the display so we can see things better
  IrrHideMouse()
  
  ; -----------------------------------------------------------------------------
  ; while the irrlicht environment is still running
  While IrrRunning()
      ; begin the scene, erasing the canvas with sky-blue before rendering
      IrrBeginScene( 240, 255, 255 )
  
      IrrGetNodePosition( *OurCamera, @StartVector\x, @StartVector\y, @StartVector\z )
      IrrGetCameraTarget( *OurCamera, @EndVector\x, @EndVector\y, @EndVector\z )
      
      ; extend the line through the target point for 5000 times its origonal length
      EndVector\x + ((EndVector\x - StartVector\x) * 5000)
      EndVector\y + ((EndVector\y - StartVector\y) * 5000)
      EndVector\z + ((EndVector\z - StartVector\z) * 5000)
  
      ; reset the collision point so that if the 
      CollideAt\x = 0 : CollideAt\y = 0 : CollideAt\z = 0
      
      ; using the line we have defined and the collision group created from the
      ; object get the point at which the two collide
      IrrGetCollisionPoint( @StartVector, @EndVector, *CollisionGroup, @CollideAt )
  
      ; move the test object to this location to indicate the point of collision
      IrrSetNodePosition( *TestNode, CollideAt\x, CollideAt\y, CollideAt\z )
  
      ; draw the scene
      IrrDrawScene()
      
      ; end drawing the scene and render it
      IrrEndScene()
  Wend
  
  ; -----------------------------------------------------------------------------
  ; Stop the irrlicht engine and release resources
  IrrStop()
EndIf

End

; IDE Options = PureBasic 4.10 Beta 2 (Windows - x86)
; CursorPosition = 50
; FirstLine = 48
; Folding = -