;; ----------------------------------------------------------------------------
;; Irrlicht Wrapper For Imperative Languages - Purebasic Examples
;; IrrlichtWrapper and FreeBasic-Example by Frank Dodd (2006)
;; Improved IrrlichtWrapper and PureBasicExample by Michael Taupitz 
;; ----------------------------------------------------------------------------
;; Example 36 : LensFlare
;; This Example is converted from the Acki's Extensions LensFlare-Demo.
;; ----------------------------------------------------------------------------

EnableExplicit 

;; ////////////////////////////////////////////////////////////////////////////
;; Include the Irr3D-Requester
;XIncludeFile "..\Irr3DRequester.pb"
XIncludeFile #PB_Compiler_Home + "Includes\IrrlichtWrapper_Include.pbi"

;; ////////////////////////////////////////////////////////////////////////////
;; Global variables
Define *camera.irr_camera
Define *light.irr_node
Define *noda.irr_node
Define *lensflareNode.irr_node
Define *mTexture.irr_texture
Define *mSphereTexture.irr_texture
Define *dynamicCubeNode.irr_node
Define *dynamicSphereNode.irr_node
Define *dc.irr_node
Define *mesh.irr_mesh, *mc.irr_mesh
Define mx.f,my.f,mz.f
Define *BSPNode.l,*BSPMesh.l,*entity.l,*rec.l
Define fps.l=0, lastFPS.l = 0

Define *coneMesh.l,*coneNode.l, *coneTexture.l
; We need a EventReceiver to react on Mouse-Buttons.....
ProcedureCDLL.l eventCallback(*event.SEvent)
  Shared *dynamicCubeNode,*mTexture,*camera,*dc,*dynamicSphereNode,*mSphereTexture
  Define Size.f = 10.0 
  Define mass.f = 10.0
  Define velocity.f = 5.0
  Define.f cam_x,cam_y,cam_z
  Define.f target_x,target_y,target_z
  Define.f velo_x,velo_y,velo_z
;  Debug("EventCB")
  If *event\EventType = #IRR_EET_MOUSE_INPUT_EVENT 
    If *event\MouseEvent\event = #IRR_EMIE_LMOUSE_PRESSED_DOWN
      ;thow a Test-Cube from cameras's position
      *dynamicCubeNode = IrrAddCubeSceneNode(Size*2)
      IrrSetNodeMaterialFlag(*dynamicCubeNode,#IRR_EMF_LIGHTING,#False)
      IrrSetNodeMaterialTexture(*dynamicCubeNode,*mTexture,0)
      *dc = IrrPhysicsAddDynamicCube(*dynamicCubeNode,Size*2,Size*2,Size*2,mass)
      ;;IrrPhysicsSetVelocity() 
      
      IrrGetNodePosition(*camera,@cam_x,@cam_y,@cam_z)
      IrrGetCameraTarget(*camera,@target_x,@target_y,@target_z)
      IrrPhysicsSetEntityPos(*dc,cam_x,cam_y,cam_z)
      
      cam_x = target_x-cam_x
      cam_y = target_y-cam_y
      cam_z = target_z-cam_z
      
      Debug("vorher("+StrF(cam_x)+","+StrF(cam_y)+","+StrF(cam_z))
      IrrVectorNormalize(@cam_x,@cam_y,@cam_z) 
      Debug("nachher("+StrF(cam_x)+","+StrF(cam_y)+","+StrF(cam_z))
      
      velo_x = cam_x * velocity
      velo_y = cam_y * velocity
      velo_z = cam_z * velocity
      
      IrrPhysicsSetEntityVelocity(*dc,velo_x,velo_y,velo_z)
      
    EndIf
    If *event\MouseEvent\event = #IRR_EMIE_RMOUSE_PRESSED_DOWN
      *dynamicSphereNode = IrrAddSphereSceneNode(Size,16) 
      IrrSetNodeMaterialFlag(*dynamicSphereNode,#IRR_EMF_LIGHTING,#False)
      IrrSetNodeMaterialTexture(*dynamicSphereNode,*mTexture,0)
      *dc = IrrPhysicsAddDynamicSphere(*dynamicSphereNode,Size,Size,Size,mass) 
      
      IrrGetNodePosition(*camera,@cam_x,@cam_y,@cam_z)
      IrrGetCameraTarget(*camera,@target_x,@target_y,@target_z)
      IrrPhysicsSetEntityPos(*dc,cam_x,cam_y,cam_z)
      
      cam_x = target_x-cam_x
      cam_y = target_y-cam_y
      cam_z = target_z-cam_z
      
      Debug("vorher("+StrF(cam_x)+","+StrF(cam_y)+","+StrF(cam_z))
      IrrVectorNormalize(@cam_x,@cam_y,@cam_z) 
      Debug("nachher("+StrF(cam_x)+","+StrF(cam_y)+","+StrF(cam_z))
      
      velo_x = cam_x * velocity
      velo_y = cam_y * velocity
      velo_z = cam_z * velocity
      
      IrrPhysicsSetEntityVelocity(*dc,velo_x,velo_y,velo_z)
      
    EndIf
  EndIf
  
  EndProcedure
  
        

; -----------------------------------------------------------------------------
; 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() 
If  IrrStart( #IRR_EDT_DIRECT3D9, 640, 480, #IRR_WINDOWED, #IRR_NO_SHADOWS, #False)
  ; set the window caption
  IrrSetWindowCaption( "Example 36 : LensFlare" )
  
  
  IrrPhysicsInit() 
  
  
  *rec = IrrCreateEventReceiver(@eventCallback())
  IrrSetEventReceiver(*rec)
  ;Creating a light as Parent for the LensFlare
  ;*light = IrrAddLight(0,0,0,0.6,0.7,1.0,600.0)
  
  *mTexture   = IrrGetTexture("../media/wall.bmp") 
  
  
  ; first we add the pk3 archive to our filing system. once we have done this
  ; we can open any of the files in the archive as if they were in the current
  ; working directory
  IrrAddZipFile( "../media\map-20kdm2.pk3", #IRR_IGNORE_CASE, #IRR_IGNORE_PATHS )
  
  ; load the BSP map from the archive as a mesh object. any polygons in the mesh
  ; that do not have textures will be removed from the scene!
  *BSPMesh = IrrGetMesh( "20kdm2.bsp" )
  
  ; add the map to the scene as a node. when adding the mesh this call uses a 
  ; mechanism called an octtree that if very efficient at rendering large amounts
  ; of complext geometry most of which cant be seen, using this call for maps
  ; will greatly improve your framerates
  *BSPNode = IrrAddMeshToSceneAsOcttree( *BSPMesh )
  
   *entity = IrrPhysicsAddStaticMesh(*BSPMesh,*BSPNode, 1,1,1, 10)
   IrrPhysicsSetEntityPos(*entity,-1300,-144,-1249)

  
  *coneMesh = IrrGetMesh("..\media\cone.3ds")
  *coneNode  = IrrAddMeshToScene(*coneMesh)
  *coneTexture = IrrGetTexture("..\media\crate.jpg")
  IrrSetNodeMaterialTexture(*coneNode, *coneTexture,0)
  IrrSetNodeMaterialFlag(	*coneNode,#IRR_EMF_LIGHTING,#False)

  IrrPhysicsAddMesh(*coneMesh,*coneNode,1,1,1,10)
  
  
	
  *camera = IrrAddFPSCamera()
  
  ; 
  ; 
  ; 
  ; *mesh =   IrrGetMesh("../media/mikesbox.b3d")
  ; *noda = IrrAddMeshToScene(*mesh) 
  ; ;IrrSetNodeScale( *noda,20,20,20)
  ; IrrSetNodeMaterialTexture(*noda,*mTexture,0)
  ; IrrSetNodeMaterialTexture(*noda,*mTexture,1)
  ; 
  ; IrrSetNodeMaterialType(*noda,#IRR_EMT_SOLID) 
  ; IrrSetNodeMaterialFlag(*noda,#IRR_EMF_LIGHTING,#False)
  ; *mc = IrrPhysicsAddMesh(*mesh,*noda,1,1,1)
  ; IrrPhysicsSetEntityMassMatrix(*mc,0.0001,0.0,0.0,0.0)  
  ; IrrAddRotationAnimator(*noda,0,0,0.1)
  ; 
  ; *dynamicCubeNode = IrrAddCubeSceneNode(100.0)
  ; IrrSetNodeMaterialTexture( *dynamicCubeNode, *mTexture, 0 )
  ; IrrSetNodeMaterialFlag(*dynamicCubeNode,#IRR_EMF_LIGHTING,#False)
  ; *dc = IrrPhysicsAddDynamicCube(*dynamicCubeNode,100.0,100.0,100.0,4.0)
  ; IrrPhysicsSetEntityPos(*dc,0,0,100)
  ; 
  ; *dynamicCubeNode = IrrAddCubeSceneNode(100.0)
  ; IrrSetNodeMaterialTexture( *dynamicCubeNode, *mTexture, 0 )
  ; IrrSetNodeMaterialFlag(*dynamicCubeNode,#IRR_EMF_LIGHTING,#False)
  ; *dc = IrrPhysicsAddDynamicCube(*dynamicCubeNode,100.0,100.0,100.0,4.0)
  ; IrrPhysicsSetEntityPos(*dc,30,150,100)
  ; 
  
  
  ;A simple FPS-Cam is enough for this example.
  ; *camera = IrrAddFPSCamera()
  ; IrrSetNodePosition(*camera,0,0,0)
  ;*camera = IrrAddCamera( 0,0,-750, 0,0,0 )
  ; disable mouse cursor
  IrrHideMouse() 
  
  ; -----------------------------------------------------------------------------
  ; while the irrlicht environment is still running
  While IrrRunning()
    ; begin the scene, erasing the canvas with sky-blue before rendering
    IrrBeginScene( 0,0,0)
    
    ; draw the scene to the render buffer
    IrrDrawScene()
    
    IrrPhysicsUpdate()
    
    ; end drawing the scene and render it
    IrrEndScene()
    
    fps = IrrGetFPS()
    If fps <> lastFPS 
      IrrSetWindowCaption("IrrlichtWrappper - LensFlare example FPS:" + Str(fps) )
      lastFPS = fps;
    EndIf
    
    
  Wend
  
  ; -----------------------------------------------------------------------------
  ; Stop the irrlicht engine and release resources
  IrrStop()
  IrrPhysicsClose()
EndIf


End

; jaPBe Version=3.7.2.629
; Build=0
; Language=0x0000 Language Neutral
; FirstLine=24
; CursorPosition=42
; ExecutableFormat=Console
; DontSaveDeclare
; IDE Options = PureBasic 4.10 Beta 3 (Windows - x86)
; CursorPosition = 216
; FirstLine = 171
; Folding = -