'' ----------------------------------------------------------------------------
'' Irrlicht Wrapper For Imperative Languages - Freebasic Examples
'' Frank Dodd (2006)
'' ----------------------------------------------------------------------------
'' Blitz-Irrlicht 1.8.3 Interface by Ed Musolino (AquaLung 2015)
'' ----------------------------------------------------------------------------
'' Example 107 : Render To texture with alpha
'' This example demonstrates rendering To a texture While using the alpha
'' channel To Create shaped And animated HUD elements
'' ----------------------------------------------------------------------------

'' ////////////////////////////////////////////////////////////////////////////
'' Includes For extension libraries
Framework usr.birrlicht

'' ////////////////////////////////////////////////////////////////////////////
'' Global variables

' irrlicht objects
Global MeshTexture:Int
Global SceneNode:Int
Global OurCamera:Int
Global TextureCamera:Int


'' ////////////////////////////////////////////////////////////////////////////
' start the irrlicht interface
iIrr3D( 1024, 768, 32, False )

' set the window caption
iAppTitle( "Example 107: Render to texture with alpha" )

' Load a mesh this acts as a blue Print For the model
SceneNode = iLoadAnimMesh( "./media/zumlin.md2", False )

' Load texture resources For texturing the scene nodes
MeshTexture = iLoadTexture( "./media/zumlin.pcx" )

' make sure that vertex color do Not affect any lighting
iNodeColorByVertex( SceneNode, ECM_NONE )

' set up some lighting colors
iNodeAmbientColor( SceneNode, iMakeARGB( 255,0,0,255 ))
iNodeDiffuseColor( SceneNode, iMakeARGB( 64,255,96,255 ))
iNodeSpecularColor( SceneNode, iMakeARGB( 255,255,0,255 ))
iNodeEmissiveColor( SceneNode, iMakeARGB( 0,0,0,255 ))

' apply a material To the node To give its surface color
iNodeTexture( SceneNode, MeshTexture, 0 )

' play an animation sequence embeded in the mesh. MD2 models have a group of
' preset animations For various poses And actions. this animation sequence is
' the character standing idle
iPlayMD2Animation( SceneNode, IRR_EMAT_STAND )

' add a camera into the scene, the first co-ordinates represents the 3D
' location of our view point into the scene the second co-ordinates specify the
' target point that the camera is looking at
TextureCamera = iCreateCamera( False )
iPositionNode( TextureCamera, 50, 0, 0 )
OurCamera = iCreateCamera( False )
iPositionNode( OurCamera, 50, 0, 0 )

' add a  light source
iCreateLight( False, 100,100,-100, 0.9,0.9,0.9, 600.0 )

' add some ambient lighting
iAmbientLight( 0.1, 0.1, 0.1 )

' Create a surface For rendering the image To
Texture = iCreateRenderTexture( 128, 128 )

' circle the camera around the Object
iFlyCircleAnimator( OurCamera, 0.0, 0.0, 0.0, 40.0, 0.001 )

iCameraCLSColor( 128, 128, 192 )

' -----------------------------------------------------------------------------
' While the irrlicht environment is still running
While iRun() And (Not iKeyHit( KEY_ESCAPE ))

        ' render To a texture
        iRenderTargetTexture( Texture, iMakeARGB( 128,0,0,0), True, True )

        ' switch To the texture camera
        iActiveCamera( TextureCamera )

        ' draw the scene To the texture
        iDrawScene()

        ' set the render target back To the screen
        iRenderTargetTexture( 0, 0, False, False )

        ' switch To the display camera
        iActiveCamera( OurCamera )

        ' begin the scene, erasing the canvas with sky-blue before rendering
        iBeginScene()

        ' draw the scene
        iDrawScene()

        ' draw a the texture with the alpha channel To the screen
        iDrawImageElement( Texture, 4, 4, 0, 0, 128, 128, True )

        ' End drawing the scene And render it
        iEndScene()

Wend 

' -----------------------------------------------------------------------------
' Stop the irrlicht engine And Release resources
iEndIrr3D()


End