'' ----------------------------------------------------------------------------
'' Irrlicht Wrapper For Imperative Languages - Freebasic Examples
'' Frank Dodd (2006)
'' ----------------------------------------------------------------------------
'' Blitz-Irrlicht 1.8.3 Interface by Ed Musolino (AquaLung 2015)
'' ----------------------------------------------------------------------------
'' Example 100: XEffects Water
'' This example demonstrates the water shader supplied with the XEffects system
'' by Blindside. The effect occurs on the plane at Y = 0.0 And an effect is
'' generated both above And below the water. The animated effect is created by
'' changing the bitmap used To define the surface normal. While this set of
'' images are free To use, you will likely want To source a higher quality set
'' To improve the effect.
'' ----------------------------------------------------------------------------

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

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

' SPECIFIES the number of frames in the water animation
Const WATER_FRAMES = 18
' SPECIFIES that a New water frame will be used every two frames
Const NEW_IMAGE_EVERY = 2

' irrlicht objects
Global WaterTexture:Int[WATER_FRAMES]

Global MeshTexture:Int
Global SceneNode:Int
Global OurCamera:Int
Global filename:String


'' ////////////////////////////////////////////////////////////////////////////

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

' set the window caption
iAppTitle( "Example 100: XEffects Water" )

' add a camera into the scene, position And target it
OurCamera = iCreateFPSCamera( 0, 100.0, 0.1, -1,0,0,0,0.0)
iPositionNode( OurCamera, 150,80,100 )
iCameraTarget( OurCamera, 0,0,0 )

' start the XEffects system
iXFXStart( False, False, True )

' enable the depth render pass, this is required For the water shader
iXFXEnableDepthPass( True )

' Load a series of dump map images that define the bumpyness of the surface of
' the water. these are Then turned into normal maps For the shader To use
For i = 1 To WATER_FRAMES 
	If i > 9 Then fill$ = "00" Else fill$ = "000"
    filename = "./media/water/water_" + fill$ + i + ".png"
    WaterTexture(i) = iLoadTexture( filename )
    iNormalMapTexture( WaterTexture(i), 6.0 )
Next

' Load the shader associated with water rendering
iXFXAddPostProcessingFromFile( "./media/shaders/ScreenWater.hlsl",0 )

' Load a mesh And add it To the scene as a New node
SceneNode = iLoadMeshNode( "./media/temple.obj", False )

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

' scale the node up a little (this will Not effect this shaders lighting)
iScaleNode( SceneNode, 14.0, 14.0, 14.0 )

' Add the room To the depth pass test
iXFXAddNodeToDepthPass( SceneNode )

' apply a material To the node To give its surface color
iNodeTexture( SceneNode, MeshTexture, 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 )

' XEffects does Not use the background color defeined in bCameraCLSColor To
' display as empty space, instead it uses the color defined here.
iXFXSetClearColor( 255,128,255,255)

' hide the mouse from the display
iHideMouse()

' -----------------------------------------------------------------------------
' While the irrlicht environment is still running
i = NEW_IMAGE_EVERY
direction% = 1

While iRun() And (Not iKeyHit(KEY_ESCAPE))

        ' begin the scene
        iBeginScene()

        ' set the image used as the surface texture For this water frame
        iXFXSetPostProcessingUserTexture( WaterTexture(i/NEW_IMAGE_EVERY))
        
        ' keep on counting up images And reset back the begining If needed
        i = i + direction
        If i = (WATER_FRAMES + 1) * NEW_IMAGE_EVERY Then i = NEW_IMAGE_EVERY 

        ' draw the scene
        iDrawScene()
        
        ' End drawing the scene And render it
        iEndScene()

Wend 

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

End