;; ----------------------------------------------------------------------------
;; 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 19 : Selecting Nodes through the camera
;; This example selects a node in the scene that is hit by a ray that is cast
;; out through the centre of the camera
;; ----------------------------------------------------------------------------

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


EnableExplicit

;; ////////////////////////////////////////////////////////////////////////////
;; Global variables

#TEST_NODES = 125

; irrlicht objects
Define *MeshTexture.irr_texture
Dim *TestNode.irr_node(#TEST_NODES)
Define *SelectedNode.irr_node
Define *OurCamera.irr_camera
Define *BitmapFont.irr_font
Define x.l
Define y.l
Define *MouseEvent.IRR_MOUSE_EVENT
Define selected_node.s

Define i.l
Define xx.l,yy.l,zz.l


;; ////////////////////////////////////////////////////////////////////////////
;; GDB debugger main() function

; -----------------------------------------------------------------------------
; 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 17: 2D Text label on a 3D model" )
  
  ; load the bitmap font as a texture
  *BitmapFont = IrrGetFont ( "media\bitmapfont.bmp" )
  *MeshTexture = IrrGetTexture( "media\texture.jpg" )
  
  ; itterate all of the test nodes
  For i = 1 To #TEST_NODES
      ; add a test node to the scene
      *TestNode(i) = IrrAddCubeSceneNode(10)
  
      ; move the node so that it is arranged into a large 3D cubed array of nodes
  
      xx.l = ((i-1)/25)*40
      yy.l = ((i-1)/5)*40
      zz.l = ((i-1) % 5)*40    
      IrrSetNodePosition( *TestNode(i), xx, yy,zz )    
      
      ; apply a material to the node
      IrrSetNodeMaterialTexture( *TestNode(i), *MeshTexture, 0 )
  Next i
  
  ; add an FPS camera into the scene and move it into position
  *OurCamera = IrrAddFPSCamera()
  IrrSetNodePosition( *OurCamera, 400, 200, 0 )
  IrrSetCameraTarget( *OurCamera, 0,0,0)
  
  ; prepare a message that indicates that there is no node selected in the scene
  selected_node = "NOTHING SELECTED"
  
  ; -----------------------------------------------------------------------------
  ; while the irrlicht environment is still running
  While IrrRunning()
      ; begin the scene, erasing the canvas with grey before rendering
      IrrBeginScene( 128,128,128 )
  
      ; draw the scene
      IrrDrawScene()
  
      ; get the node that collides with a ray cast out through the center of the
      ; camera
      *SelectedNode = IrrGetCollisionNodeFromCamera( *OurCamera , -1 )
  
      ; if the node value that was returned is 0 there is no node selected
      If *SelectedNode = 0
          ; Display a message informing the user how to move the cubes
          selected_node = "NOTHING SELECTED"
      Else
          ; some node was hit, scan through the array of nodes to find which one
          ; it was
          For i = 1 To #TEST_NODES
              ; if this was the collided node
              If  *SelectedNode = *TestNode(i)
                  ; display the node index
                  selected_node = "NODE "+Str(i)+" SELECTED"
                  ; switch lighting off on this node to indicate that it is
                  ; selected
                  IrrSetNodeMaterialFlag( *SelectedNode, #IRR_EMF_LIGHTING, #IRR_ON )
              Else
                  ; this node is not selected, switch lighting on for this node
                  ; to indicate this fact
                  IrrSetNodeMaterialFlag( *TestNode(i), #IRR_EMF_LIGHTING, #IRR_OFF )
              EndIf
          Next i
      EndIf
  
      ; draw the text indicating which node was selected
      Irr2DFontDraw ( *BitmapFont, selected_node, 0, 0, 100, 16 )
  
      ; 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 = 88
; FirstLine = 64
; Folding = -