;; ----------------------------------------------------------------------------
;; 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 28 : Render to Texture
;; This example loads an MD2 model and Render to a Texture on a Cube
;; ----------------------------------------------------------------------------

EnableExplicit 

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

;; ////////////////////////////////////////////////////////////////////////////
;; Global variables
Define *OurCamera.irr_camera
Define *FixedCamera.irr_camera
Define *FairyMesh.irr_mesh
Define *FairySceneNode.irr_node
Define *FairyTexture.irr_texture
Define *CubeNode.irr_node
Define *RenderTarget.irr_texture
Define *light.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()

  ; set the window caption
  IrrSetWindowCaption( "Example 28: Render to Target" )
  
  ; add credits for the model as a static text GUI object on the display
  IrrGuiAddStaticText( "Render to Target (the Cube) - Move with mouse and keys.", 4,0,200,16, #IRR_GUI_NO_BORDER, #IRR_GUI_NO_WRAP )
  
  
  ; load And display animated fairy mesh
  *FairyMesh = IrrGetMesh("media/faerie.md2")
  If *FairyMesh
    *FairySceneNode = IrrAddMeshToScene( *FairyMesh )  
    *FairyTexture   = IrrGetTexture("media/faerie2.bmp")
    IrrSetNodeMaterialTexture( *FairySceneNode, *FairyTexture, 0 )
    IrrSetNodeMaterialFlag( *FairySceneNode, #IRR_EMF_LIGHTING, #True)
;    IrrSetNodeMaterialShininess( *FairySceneNode, 0, 20.0)
    IrrSetNodePosition( *FairySceneNode,-10,0,-100 )
  EndIf
  
  *light = IrrAddLight(-15,5,-105,255,255,255,100)
  IrrSetAmbientLight(60,60,60)
  
  
  ; add fps camera
  *OurCamera = IrrAddFPSCamera()
  IrrSetNodePosition(*OurCamera, -50,50,-150)
  
  ;disable mouse cursor
  IrrHideMouse()
  
  ;create test cube
  	*CubeNode = IrrAddCubeSceneNode(60);
  
  ; let the cube rotate And set some light settings
  IrrAddRotationAnimator(*CubeNode,0.3, 0.3,0)
  IrrSetNodePosition( *CubeNode,-100,0,-100 )
  IrrSetNodeMaterialFlag( *CubeNode, #IRR_EMF_LIGHTING, #False)
  
  
  	
  	;To test out the render To texture feature, we need a render target texture. These are Not 
  	;like standard textures, but need To be created first. To create one, we call 
  	;IVideoDriver::createRenderTargetTexture() And specify the size of the texture. Please
  	;don't use sizes bigger than the frame buffer for this, because the render target shares
  	;the zbuffer With the frame buffer. And because we want To render the scene Not from the
  	;user camera into the texture, we add another, fixed camera To the scene. But before we
  	;do all this, we check If the current running driver is able To render To textures. If 
  	;it is Not, we simply display a warning text.
  	
  	
    *RenderTarget = IrrCreateRenderTargetTexture(256,256)
    IrrSetNodeMaterialTexture( *CubeNode, *RenderTarget, 0 )
      
    ;add fixed camera
    *FixedCamera = IrrAddCamera(10,10,-80, -10,10,-100)
  
  
  ; -----------------------------------------------------------------------------
  ; 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 scene into render target
  			
  			;set render target texture
  			IrrSetRenderTarget(*RenderTarget, 80,80,80, #True, #True);     
  
  			; make cube invisible And set fixed camera As active camera
  			IrrSetNodeVisibility(*CubeNode,#False);
  			IrrSetActiveCamera(*FixedCamera);
  
        ; draw the scene to the render buffer
        IrrDrawScene()
      
  			; set back old render target
  			IrrSetRenderTarget(0,   0,0,255, #True, #True);     );      
  
  			; make the cube visible And set the user controlled camera As active one
  			IrrSetNodeVisibility(*CubeNode,#True);
  			IrrSetActiveCamera(*OurCamera);
  
      IrrDrawScene()
      ; draw the GUI
      IrrDrawGUI()
  
      ; end drawing the scene and render it
      IrrEndScene()
  Wend
  
  ; -----------------------------------------------------------------------------
  ; Stop the irrlicht engine and release resources
  IrrStop()
EndIf


End

; IDE Options = PureBasic v4.01 (Windows - x86)
; CursorPosition = 34
; FirstLine = 4
; jaPBe Version=3.7.2.629
; Build=0
; FirstLine=75
; CursorPosition=107
; ExecutableFormat=Windows
; DontSaveDeclare
; IDE Options = PureBasic 4.10 Beta 3 (Windows - x86)
; CursorPosition = 66
; FirstLine = 39
; Folding = -