;; ----------------------------------------------------------------------------
;; 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 21 : Carrying Objects
;; This example demonstrates how you can attach a child model To an animated
;; Direct X model containing joints
;; ----------------------------------------------------------------------------

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


EnableExplicit


; irrlicht objects
Define *DirectXMesh.irr_mesh
Define *MeshTexture.irr_texture
Define *BoxTexture.irr_texture
Define *SceneNode.irr_node
Define *OurCamera.irr_camera
Define *JointNode.irr_node
Define *TestNode.irr_node

; -----------------------------------------------------------------------------
; 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 21: Carrying Objects" )
  
  ; load a mesh
  *DirectXMesh = IrrGetMesh( "media\dwarf.x" )
  
  ; load texture resources for texturing models
  *MeshTexture = IrrGetTexture( "media\dwarf.jpg" )
  *BoxTexture = IrrGetTexture( "media\texture.jpg" )
  
  ; add the mesh to the scene
  *SceneNode = IrrAddMeshToScene( *DirectXMesh )
  
  ; apply a material to the object
  IrrSetNodeMaterialTexture( *SceneNode, *MeshTexture, 0 )
  
  ; switch off lighting effects on this model
  IrrSetNodeMaterialFlag( *SceneNode, #IRR_EMF_LIGHTING, #IRR_OFF )
  
  ; set the speed of playback for the animated Direct X model
  ;IrrSetNodeAnimationSpeed( *SceneNode, 4000 )
  
  ; add a camera into the scene pointing at the model
  *OurCamera = IrrAddCamera( 75,40,-50, 0,40,0 )
  
  ; create a test node to represent the object that is being carried
  *TestNode = IrrAddCubeSceneNode(10)
  
  ; assign a texture to the carried object
  IrrSetNodeMaterialTexture( *TestNode, *BoxTexture, 0 )
  
  ; offset the position of the node so that it appears to be attached to the
  ; characters hand
  IrrSetNodePosition( *TestNode, 20,-15,-10 )
  
  ; get an invisible node that is attached to the specified joint on the animated
  ; node
  *JointNode = IrrGetDirectXJointNode ( *SceneNode, "Joint26" )
  
  ; attach the carried test node to this invisible joint node from now on the
  ; carried object will move along with the joint without any more intervention
  ; from us
  IrrAddChildToParent ( *TestNode, *JointNode )
  
  
  ; -----------------------------------------------------------------------------
  ; while the irrlicht environment is still running
  While IrrRunning()
      ; begin the scene, erasing the canvas with sky-blue before rendering
      IrrBeginScene( 240, 255, 255 )
  
      ; 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 = 59
; FirstLine = 45
; Folding = -