'' ----------------------------------------------------------------------------
'' Irrlicht Wrapper For Imperative Languages - Freebasic Examples
'' Frank Dodd (2006-2010)
'' ----------------------------------------------------------------------------
'' Blitz-Irrlicht 1.8.3 Interface by Ed Musolino (AquaLung 2015)
'' ----------------------------------------------------------------------------
'' Example 10 : Terrain And Fog
'' This example creates a terrain from a bitmap heightfield And displays it in
'' a foggy scene
'' ----------------------------------------------------------------------------

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

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

' irrlicht objects
Global Terrain:Int
Global TerrainNode:Int
Global TerrainTexture0:Int
Global TerrainTexture1:Int
Global Camera:Int

'' ////////////////////////////////////////////////////////////////////////////
' start the irrlicht interface
iVSync( True )
iIrr3D( 1280, 720, 32, False )

' send the window caption
iAppTitle( "Example 10: Terrain and Fog" )

' here we Create the terrain from a greyscale bitmap where bright pixels are
' high points And black pixels are low points. the command generates the mesh
' And automatically adds it as a node To the scene
Terrain = iLoadTerrain( "./media/iheight.jpg",0,0,0,0,0,0,1,1,1,255,255,255,255,2,5,ETPS_17 )

' the node is too small To be a proper terrain so we get the node Object of the
' terrain And scale its size up 40 times along the X And Z axis And just 4 times
' along the Y axis
TerrainNode = Terrain
iScaleNode( TerrainNode, 40.0, 4.4, 40.0 )

' we Load two textures in To apply To the terrain node
TerrainTexture0 = iLoadTexture( "./media/island.jpg" )

TerrainTexture1 = iLoadTexture( "./media/detailmap3.jpg" )

' the first texture is a color texture that is applied across the entire
' surface of the map. this needs To be a fairly high resoloution as the map is
' very large now
iNodeTexture( TerrainNode, TerrainTexture0, 0 )

' the second texture is tiled many times across the map this adds fine detail
' And structure To the first enlarged terain color
iNodeTexture( TerrainNode, TerrainTexture1, 1 )

' we set the scale of the detail map so its repeated 20 times across the map in
' the x And y axis
iScaleTerrainTexture( Terrain, 1.0, 40.0 )

' finally we apply some material texuring effects To the node
' the terrain is Self illuminating
iNodeMaterialFlag( TerrainNode, IRR_EMF_LIGHTING, False )
' the material Type is detail Type. this Type applies the first texture across
' the entire node And Then scales And applies the second texture across the node
' there are many different material types you can set, these can be found in the
' .bi definition file, experiment with them
iNodeMaterialType ( TerrainNode, IRR_EMT_DETAIL_MAP )

' finally we switch on fog For this material so that this terrain will fade out
' into a fog in the distance
iNodeMaterialFlag( TerrainNode, IRR_EMF_FOG_ENABLE, True )

' now we need To add the fog To the scene. the first three parameters are the
' fog color, we set this To the same color as our sky so the scene fogs out
' nicely into nothing, the Next parameter specifies whether you want the fog
' To increase in a linear mannar Or exponentially - exponential fog usually
' looks more atmospheric While linear looks more like a dense sea fog, the Next
' two parameters specify the distance at which the fog starts And the distance
' at which the fog reaches its maximum density And finally the fog density -
' this is only used with exponential fog And determines how quickly the
' exponential change takes place
iCameraFog ( 128,255,255, IRR_EXPONENTIAL_FOG, 0.0,4000.0, 0.5 )


' we add a first person perspective camera To the scene so you can look about
' And move it into the center of the map
Camera = iCreateFPSCamera( 0,100.0,0.5,-1,0,0,0,0.0 )
iPositionNode( Camera, 3942.8, 1202.7, 5113.9 )
iRotateNode( Camera, 19, -185.5, 0 )

' the clipping distance of a camera is a distance beyond which no triangles are
' rendered, this speeds the scene up by Not showing geometry that is in the
' distance And too small To see however our terrain is so huge we need To
' extend this distance out
iCameraRange( Camera, 12000, 1.0)

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

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

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

    ' begin the scene
    iBeginScene()

    ' draw the scene
    iDrawScene()

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

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

End