'' ----------------------------------------------------------------------------
'' Irrlicht Wrapper For Imperative Languages - Freebasic Examples
'' Frank Dodd (2006-2010)
'' ----------------------------------------------------------------------------
'' Blitz-Irrlicht 1.8.3 Interface by Ed Musolino (AquaLung 2015)
'' ----------------------------------------------------------------------------
'' Example 65 : Spherical Terrain
'' This example creates a spherical terrain by attaching six terrain objects
'' together in a cube And Then applying a spherical distortion To the terrains.
'' the six textures applied To the terrain sphere need To be carefully
'' constructed so that they are properly applied.
'' ----------------------------------------------------------------------------

'' ////////////////////////////////////////////////////////////////////////////
'' 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 BitmapFont:Int
Global metrics:String
Global Light:Int
Global SkyBox:Int
Global Billboard:Int
Global BillboardTexture:Int

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

' send the window caption
iAppTitle( "Example 65: Spherical Terrain" )

BitmapFont = iLoadFont ( "./media/bitmapfont.bmp" )
TerrainTexture0 = iLoadTexture( "./media/terrain-texture.jpg" )
TerrainTexture1 = iLoadTexture( "./media/detailmap-dim.jpg" )

' add a spherical terrain To the scene
AddPlanet( 0,0,0, 0,0,0, 255 )

' add a simple skybox To the scene To represent space
SB1Tex = iLoadTexture("./media/stars.jpg")


SkyBox = iCreateSkybox( SB1Tex,SB1Tex,SB1Tex,SB1Tex,SB1Tex,SB1Tex)

iCameraFog ( 255,128,0, IRR_EXPONENTIAL_FOG, 0.0,500.0, 0.5 )

Light = iCreateLight( False, 6400,16000,0, 1,1,1, 192200.0 )
iAmbientLight( 0.1, 0.1, 0.1 )


' 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, 0, 2000, 0 )

' Load the texture resource For the billboard
BillboardTexture = iLoadTexture( "./media/sun.tga" )

' add the billboard To the scene, the first two parameters are the size of the
' billboard in this instance they match the pixel size of the bitmap To give
' the correct aspect ratio. the last three parameters are the position of the
' billboard Object
Billboard = iCreateBillBoard( 600.0,600.0,  3000.0, 1500.0, 0.0 )

' now we apply the loaded texture To the billboard using node material index 0
iNodeTexture( Billboard, BillboardTexture, 0 )

' rather than have the billboard lit by light sources in the scene we can
' switch off lighting effects on the model And have it render as If it were
' Self illuminating
iNodeMaterialFlag( Billboard, IRR_EMF_LIGHTING, False )
iNodeMaterialType( Billboard, IRR_EMT_TRANSPARENT_ALPHA_CHANNEL )
iMaterialTypeParam( iGetMaterial( Billboard, 0 ), 0.0001 )

' 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, 500000, 1.0 )

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

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

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

    ' begin the scene
    iBeginScene()

    ' draw the scene
    iDrawScene()

    metrics = "Example 65: Spherical Terrain ("+String(iGetFPS())+") fps ("+String(iTrisDrawn())+" Polygons)"
    iAppTitle( metrics )

    'metrics = "PRIMS: "+ Str(iTrisDrawn())
    'iText ( BitmapFont, metrics, 4, 4, 250, 24 )

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

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


End


Function AddPlanet( px#,py#,pz#, rx#,ry#,rz#, col% )

' add the spherical terrain. this is supplied with six terrain highmaps one For
' each of the faces on the cube
Terrain = iCreateSphericalTerrain("./media/simplesphere.bmp", "./media/simplesphere.bmp", "./media/simplesphere.bmp", "./media/simplesphere.bmp", "./media/simplesphere.bmp", "./media/simplesphere.bmp", px,py,pz, rx,ry,rz, 6.4,6.4,6.4, 255, 255, 255, 255,  -30, 0, 4, ETPS_17 )
    TerrainNode = Terrain
    iNodeTexture( TerrainNode, TerrainTexture0, 0 )
    iNodeTexture( TerrainNode, TerrainTexture1, 1 )
    iScaleSphericalTexture( Terrain, 1.0, 60.0 )
    iNodeMaterialType ( TerrainNode, IRR_EMT_DETAIL_MAP )

End Function