'' ----------------------------------------------------------------------------
'' Irrlicht Wrapper For Imperative Languages - Freebasic Examples
'' Frank Dodd (2006-2010)
'' ----------------------------------------------------------------------------
'' Blitz-Irrlicht 1.8.3 Interface by Ed Musolino (AquaLung 2015)
'' ----------------------------------------------------------------------------
'' Example 53 : Grass
'' An example of a grass Object that is layered over a terrain To Create extra
'' detail To a terrain increasing the realism
'' ----------------------------------------------------------------------------

'' ////////////////////////////////////////////////////////////////////////////
'' 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
Global CameraNode:Int
Global TerrainHeight:Int
Global terrainColor:Int
Global grassMap:Int
Global grassTexture:Int
Global grassNode:Int
Global x:Float
Global y:Float
Global z:Float


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

' send the window caption
iAppTitle( "Example 53: Grass by G Davidson" )

' allow transparency To write To the z buffer, this is often nessecary To make
' many transparent objects appear in the correct order especially transparent
' objects is opaque elements like grass And leaves, the only consideration is
' that there will be an impact on performance
iTransparentZWrite( False )

' here we Create the terrain from a greyscale bitmap where bright pixels are
' high points And black pixels are low points. this 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,0,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, 2.4, 40.0 )

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

' set up the terrain node with textures lighting
iNodeTexture( TerrainNode, TerrainTexture0, 0 )
iNodeTexture( TerrainNode, TerrainTexture1, 1 )
iScaleTerrainTexture( Terrain, 1.0, 60.0 )
iNodeMaterialFlag( TerrainNode, IRR_EMF_LIGHTING, False )
iNodeMaterialType ( TerrainNode, IRR_EMT_DETAIL_MAP )
iNodeMaterialFlag( TerrainNode, IRR_EMF_FOG_ENABLE, True )

' Load the textures And images For that node
TerrainHeight = iLoadImage( "./media/iheight.jpg" )
terrainColor = iLoadImage( "./media/island.jpg" )
grassMap = iLoadImage( "./media/island.jpg" )
grassTexture = iLoadTexture( "./media/grass.png" )

' we add the grass in as 100 seperate patches, these could even be grouped
' under a set of zone managers To make them more efficient
For x = 0 To 3
    For y = 0 To 3
        ' here we add the grass Object, it has the following parameters: -
        ' a terrain onto which the grass layered,
        ' an x And y tile coordinate For the patch (multiplied by the patch size)
        ' a size For the patch
        ' the distance in patches upto which all blades of grass are drawn
        ' whether pairs of grass are aligned in a cross IRR_ON To enable
        ' a scale For the grass height
        ' the height map associated with the terrain used For setting grass height
        ' the texture map associated with the terrain used For coloring the grass
        ' a grass map defining the types of grass placed onto the terrain
        ' a texture map containing the images of the grass
        grassNode = iCreateGrass( Terrain, x, y, 1100*3, 2.0, False, 0.5, 250, 0, 0, TerrainHeight, terrainColor, grassMap, grassTexture )
                                
        ' here we set how much grass is visible firstly the number of grass
        ' particles that can be seen And secondly the distance upto which they
        ' are drawn
        iGrassDensity( grassNode, 600, 4000 )
        
        ' here we set the wind effect on the grass, first parameter sets the
        ' strength of the wind And the second the resoloution
        iGrassWind ( grassNode, 3.0, 1.0 )
        
        iNodeMaterialFlag( grassNode, IRR_EMF_LIGHTING, False )
        iNodeMaterialFlag( grassNode, IRR_EMF_FOG_ENABLE, True )
    Next
Next
        
' now we need To add the fog To the scene
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)
CameraNode = Camera
iPositionNode( CameraNode, 3942.0, 650.0, 5113.0 )
iRotateNode( CameraNode, 19, -185.5, 0 )

' the clipping distance of a camera is a distance beyond which no triangles are
' rendered
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()

	x = iNodeX( CameraNode, False )
	z = iNodeZ( CameraNode, False )

    y = iTerrainHeight( Terrain, x, z )
    iPositionNode( CameraNode, x, y+160, z )

    ' draw the scene
    iDrawScene()

    ' End drawing the scene And render it
    iEndScene()

Wend

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


End