#Include "Ninfa3d.bi"

dim Shared camera As nCAMERA
Dim terrain As nTERRAIN
Dim font01 As nFONT
Dim As nTEXTURE tex01,tex02,TerrainTexture0,grassTexture
Dim wire As Integer
dim xterrainHeight as nimage
dim terrainColor as nimage
dim grassMap as nimage
dim grassNode as ngrass
dim x as single
dim y as single
'Enable vertical synch.
EnableVsync()

'Starts the Ninfa3D Engine
InitEngine(640,480,32,0)
	BackGroundColor(128,128,128)'Background Color
	AmbientLight(200,200,200)'Ambient color
	Fog(96,96,96,0.0003)' Fog

'Include the file "SampleFuntions.bi.
'It contains useful features, such as drawing the interface and create the test area.
#Include "SampleFunctions.bi"

terrain = LoadTerrain("./media/terrain-heightmap.jpg")
    ScaleMesh(terrain,40.0, 2.4, 40.0 )
    
    TerrainTexture0 = LoadTexture( "./media/terrain-texture.jpg" )
    EntityTexture(terrain,TerrainTexture0,0)


'Create a camera
camera = CreateCameraFPS(100,1)
	CameraRange(camera,1,42000)
	PositionEntity(camera,2400,256,4500)' Position


font01 = LoadFont("media\courier.xml")'Load the font, in format XML

grassTexture = LoadTexture( "./media/g2.png" )
'grassTexture = LoadTexture( "./media/grass.png" )


xterrainHeight=LoadImagefromFile("./media/terrain-heightmap.jpg")
terrainColor=LoadImagefromFile("./media/terrain-texture.jpg")
grassMap=LoadImagefromFile("./media/terrain-grassmap.bmp")
' 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 9
    for y = 0 to 9
        ' here we add the grass object, it has the following parameters: -
        ' a terrain onto which the grass layered,
        ' an x and y coordinate for the patch
        ' a size for the patch
        ' 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 = AddGrass (Terrain, x, y, 1024, 1.5, 2000, 0, 0, xterrainHeight, 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
        SetGrassDensity ( grassNode, 2000, 5000 )
        'grassrender(grassnode)
        ' here we set the wind effect on the grass, first parameter sets the
        ' strength of the wind and the second the resoloution
        SetGrassWind ( grassNode, 3.0, 1.0 )
        
        EntityFlag( grassNode, 3, 0 )
        EntityFlag( grassNode, 8, 1)
    next y
next x

While(EngineRun)'Returns 1 if the engine is running.

	'Begins scene
	BeginScene()
	
	'By pressing the "ESCAPE" closes the engine.
	If KeyHit(KEY_ESCAPE) Then CloseEngine()
	
	'Update the Engine.
	'Changing the value (only affects the physics) that will update the
	'Physical faster or slower.
	UpdateEngine(0.05)
	
	If KeyHit(KEY_E) Then wire = wire + 1
		
	If wire>1 Then wire = 0
	
	If Wire = 1 Then
		EntityFlag(terrain,0,1)
	Else
		EntityFlag(terrain,0,0)
	EndIf
    
    PositionEntity(camera,EntityX(camera),TerrainHeight(terrain,EntityX(camera),EntityZ(camera))+100,EntityZ(camera))
	Font(font01)'assign the font to use
	Text(15,75,"E - Wireframe",0,0)

	'Displays the interface (FPS and triangles)
	'Interface()

	'End the scene
	EndScene()
	
Wend

'Ends Ninfa3D Engine
EndEngine()