;; ----------------------------------------------------------------------------
;; 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 33 : Collision - using a Level made with Wings and lightmapped with Deled
;; This example demonstrates the collision animator And how To make one
;; node collide against another. 
;; This Example also shows how to apply a Lightmap using a texture layer. 
;; ----------------------------------------------------------------------------

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


; irrlicht objects
*BSPMesh.irr_mesh
*BSPNode.irr_node
*camera.irr_camera
*CameraNode.irr_node
*MapCollision.irr_selector
*ret.irr_selector


;; ////////////////////////////////////////////////////////////////////////////
;; 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 33: CollisionVsLevel" )
  
  ; first we load the example map into the scene for details on this please
  ; read example 5
  *BSPMesh = IrrGetMesh( "media\testroom1.dmf" )
  *BSPNode = IrrAddMeshToSceneAsOcttree( *BSPMesh )
  
  ; rescale our mesh so its big enough to walk in .. ;)
  IrrSetNodeScale(*BSPNode, 5.0, 5.0, 5.0)
  
  ; here we load our lightmap texture ..
  *Texture1 = IrrGetTexture( "media\LightMaps\testroom1\testroom1_LightMap0.bmp" )
  
  ; and apply it to our Mapnode with Material Index 1 ( the first layer is 0 ) . 
  ; Note: You can have up to 4 Texture layers per Mesh.
  IrrSetNodeMaterialTexture( *BSPNode, *Texture1, 1 )
  
  ; as we have no dynamic lights in the scene we switch all dynamic light effects on the material off,
  ; otherwise the scene would be dark.
  IrrSetNodeMaterialFlag( *BSPNode, #IRR_EMF_LIGHTING, #IRR_OFF )

  ; next we add a first person perspective camera to the scene
  *camera = IrrAddFPSCamera()
  
  ; and move it into the middle of the map
  *CameraNode = *camera
  
  ; Set our Camera Position and Rotation inside our Level
  IrrSetNodePosition( *CameraNode, 0, 80, 0 )
  IrrSetNodeRotation( *CameraNode, 4, -461.63, 0 )
  
  ; Add Skybox
  Dim *textures.irr_texture(6)
  *textures(0) = IrrGetTexture("media\irrlicht2_up.jpg")
  *textures(1) = IrrGetTexture("media\irrlicht2_dn.jpg")
  *textures(2) = IrrGetTexture("media\irrlicht2_rt.jpg")
  *textures(3) = IrrGetTexture("media\irrlicht2_lf.jpg")
  *textures(4) = IrrGetTexture("media\irrlicht2_ft.jpg")
  *textures(5) = IrrGetTexture("media\irrlicht2_bk.jpg")
  
  *SkyBox = IrrAddSkyBoxToScene( *textures(0),*textures(1),*textures(2),*textures(3),*textures(4),*textures(5))
  
  
  ; the first thing we need to do with collision is to create an object called a
  ; selector that contains a selection of triangle to be used in the collision
  ; calculations there are a number of different ways of doing this depending on
  ; the type of mesh you are working with. in this example we are using the
  ; complex BSP map and therefore should use the following command
  *MapCollision = IrrGetCollisionGroupFromComplexMesh( *BSPMesh, *BSPNode )
  
  
  ; now we can add the sixth and final animator to our camera object the collision
  ; animator. This takes a long list of parameters that define the following :-
  ; 1) the collision object created from the map
  ; 2) the node that is going to be collided against the map
  ; 3) 3 vaues defining the radius of the node (the camera in this case). if you
  ;    make this value too small you wont be able to climb steps, if its too big
  ;    you might get stuck in a doorway or be able to jump over a wall. the best
  ;    thing to do is to have some fun experimenting
  ; 4) 3 values defining the pull of gravity, in this case a weak downward force
  ; 5) finally 3 values defining the offset of the node from the collision point
  ;    this will enable you to bring an object to the surface
  *CameraNode = *camera
  *ret = IrrAddCollisionAnimator( *MapCollision, *CameraNode, 30.0,30.0,30.0, 0.0,-3.0,0.0, 0.0,50.0,0.0 )
  
  ; we also hide the mouse pointer to see the view 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 )
  
      ; draw the scene
      IrrDrawScene()
  
      ; end drawing the scene and render it
      IrrEndScene()
  Wend
  
  ; -----------------------------------------------------------------------------
  ; Stop the irrlicht engine and release resources
  IrrStop()
EndIf

End

; jaPBe Version=3.7.2.629
; Build=0
; FirstLine=48
; CursorPosition=68
; ExecutableFormat=Windows
; DontSaveDeclare
; IDE Options = PureBasic 4.10 Beta 3 (Windows - x86)
; CursorPosition = 71
; FirstLine = 42
; Folding = -