;; ----------------------------------------------------------------------------
;; 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 (and very modified ) from the 
;; Acki's Extensions LensFlare-Demo.
;; ----------------------------------------------------------------------------
;;The used SkyBox was found at http://www.OctaneDigitalStudios.com


EnableExplicit 


;number of Meteoriods, flying around.
#STONES = 10

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

;; ////////////////////////////////////////////////////////////////////////////
;; Global variables
Define *camera.irr_camera
Define *light.irr_node
Define *lensflareNode
Define *lensflareTexture
Define *SkyBox.irr_node

Define *stone_mesh.irr_mesh
Define *stone_Node.irr_node
Define *stone_texture.irr_texture


Define fps.l=0, lastFPS.l = 0,i.l
Define.f rx,ry,rz

;Place for the Stones, maybe usefull for later usage.
Dim stones.l(#STONES)

; -----------------------------------------------------------------------------
; 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() 
  ; set the window caption
  IrrSetWindowCaption( "Example 36 : LensFlare" )
  
  
  ;Creating a light as Parent for the LensFlare
  ;create light 
  *light  = IrrAddLight(0,0,0, 0.6,0.7,1.0,400.0) 
  ;Then add a FlyCircle-Animator to light, so the light will fly araond
  IrrAddFlyCircleAnimator(*light,0,150,0, 250,0.001) 
  
  ;Create the LensFlare-SceneNode
  *lensflareNode = IrrAddLensFlareSceneNode()
  
  ;Check the Pointer !!
  If *lensflareNode 
    ;Load the LensFlare-mage
    *lensflareTexture   = IrrGetTexture("media\flares.jpg") 
    IrrSetNodePosition(*lensflareNode,0,0,0)
    
    ;make the LensFalre to the Child of the Light.
     IrrAddChildToParent(*lensflareNode,*light)
               
Debug("*lensflareTexture="+Str(*lensflareTexture))
Debug("*lensflareNode="+Str(*lensflareNode))

    IrrSetNodeMaterialTexture(*lensflareNode, *lensflareTexture,0) 

               
  EndIf
  
  ;add some Rocks in the Scene....
  ;this is to make this little exmaple a little bit nicer. Also you can see how easy it is
  ;to add Objects with autmatic movement.
  ;Load the Mesh and Texture for the meteorites
  *stone_mesh = IrrGetMesh("media\stone1.ms3d")
  *stone_texture = IrrGetTexture("media\stone1.jpg")
  
  ;Now we place our meteors randomly to the Scene, and attach 2 Animators to each of them
  ; so the fly around and rotate.
  For i = 0 To #STONES-1
    rx.f = 500-Random(1000)
    ry.f = 500-Random(1000)
    rz.f = 500-Random(1000)
    stones(i) = IrrAddMeshToScene(*stone_mesh)
    IrrSetNodePosition(stones(i),rx,ry,rz)
    IrrSetNodeScale(stones(i),0.5,0.5,0.5)
    IrrSetNodeMaterialTexture(stones(i),*stone_texture,0)
    IrrSetNodeMaterialType(stones(i), #IRR_EMT_SOLID)
    IrrAddFlyCircleAnimator(stones(i),rx,ry,rz,Random(500)-250, 0.001)
    IrrAddRotationAnimator( stones(i),1.0/Random(30),1.0/Random(30),1.0/Random(30))
    ;Colour the stones ab bit
    IrrSetNodeAllMaterialEmissiveColor(stones(i),255,Random(255),Random(255),Random(255))
  Next

  ;Staff our Skybox with textures, so it looks like be in space..... 
  Dim *textures.irr_texture(6)
  *textures(0) = IrrGetTexture("media\up_256x256.jpg")
  *textures(1) = IrrGetTexture("media\down_256x256.jpg")
  *textures(2) = IrrGetTexture("media\left_256x256.jpg")
  *textures(3) = IrrGetTexture("media\right_256x256.jpg")
  *textures(4) = IrrGetTexture("media\front_256x256.jpg")
  *textures(5) = IrrGetTexture("media\back_256x256.jpg")
  
  *SkyBox = IrrAddSkyBoxToScene( *textures(0),*textures(1),*textures(2),*textures(3),*textures(4),*textures(5))  
  
  ;A simple FPS-Cam is enough for this example.
;  *camera = IrrAddFPSCamera()
  
  ;Next nice thing ist the Camera,  we attach here also animators, so its moving around.....
  ;if you not want to move automatic, use the FPS-Camera above
  *camera = IrrAddCamera(0,0,200, 0,0,0)
  IrrAddFlyCircleAnimator(*camera,0,0,0,125, 0.0001)
  IrrAddRotationAnimator( *camera,1.0/Random(30),1.0/Random(30),1.0/Random(30))


  
  ; 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()
    
    ; 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()
EndIf


End

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