'' ----------------------------------------------------------------------------
'' Irrlicht Wrapper For Imperative Languages - Freebasic Examples
'' Frank Dodd (2006-2010)
'' ----------------------------------------------------------------------------
'' Blitz-Irrlicht 1.8.3 Interface by Ed Musolino (AquaLung 2015)
'' ----------------------------------------------------------------------------
'' Example 33 : Rendering To a texture
'' This example renders a 3D model And a cube To the scene And uses the 3D model
'' To texture the cube.
'' ----------------------------------------------------------------------------

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

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

' irrlicht objects
Global MD2Mesh:Int
Global MeshTexture:Int
Global RenderTexture:Int
Global TextureA:Int
Global TextureB:Int
Global SceneNode:Int
Global CubeNode:Int
Global StaticCamera:Int
Global FPSCamera:Int
Global pixels:Int

'' ////////////////////////////////////////////////////////////////////////////
' start the irrlicht interface
iIrr3D( 800, 600, 32, False )

' set the window caption
iAppTitle( "Example 33: Rendering to a texture" )

' add credits For the model as a static Text GUI Object on the display
iStaticText( "Zumlin model by Rowan 'Sumaleth' Crawford", 4,0,200,16, False, False, False )

' 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" )

' Create a texture surface that is suitable For rendering a display onto
' the renderview must be the same size Or larger than this texture
RenderTexture = iCreateRenderTexture ( 128, 128 )
TextureA = iLoadTexture( "./media/Diagonal.bmp" )
TextureB = iCreateTexture( "merged", 128, 128, ECF_A8R8G8B8 )

' add a simple cube To the scene that will be textured with the rendered surface
CubeNode = iCreateCube( 30.0, False )
iPositionNode( CubeNode, 0, 0, 100 )

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

' switch off lighting effects on this model, as there are no lights in this
' scene the model would otherwise render pule black
iNodeMaterialFlag( SceneNode, IRR_EMF_LIGHTING, False )
iNodeMaterialFlag( CubeNode, IRR_EMF_LIGHTING, False )

' 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 static camera that is used To render the scene To a texture
StaticCamera = iCreateCamera( False )
iPositionNode( StaticCamera,50,0,0 )

' add a first person camera that is used For the display view
FPSCamera = iCreateFPSCamera( False,100.0,0.5,-1,0,0,0,0.0)
iPositionNode( FPSCamera, 40, 0, 110 )
iCameraTarget( FPSCamera, 0,0,80 )
'iRotateNode( FPSCamera, 160, -400.0, 0 )

' we also hide the mouse pointer To see the view better
iHideMouse()

' erase the canvas with sky-blue before rendering
iCameraCLSColor( 140, 255, 255 )

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

    ' switch To the static camera And render the scene To the texture
    iActiveCamera ( StaticCamera )


    pixels = iLockTexture( RenderTexture )
    iDrawSceneToTexture( RenderTexture )
    iUnlockTexture( RenderTexture )


    ' begin the scene
    iBeginScene()

    ' switch To the FPS camera draw the scene
    iActiveCamera ( FPSCamera )
    iDrawScene()
    
    ' draw the GUI
    iDrawGUI()

    ' End drawing the scene And render it
    iEndScene()
Wend 

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


End