;; ----------------------------------------------------------------------------
;; Irrlicht Wrapper For Imperative Languages - Purebasic Examples
;; IrrlichtWrapper and FreeBasic-Example by Frank Dodd (2006)
;; Improved IrrlichtWrapper - Michael Taupitz (2007)
;; PureBasic-Examples by Michael Taupitz (neotoma) & Marius Eckardt (Thalius) 
;; ----------------------------------------------------------------------------
;; Example 10 : Terrain And Fog
;; This example creates a terrain from a bitmap heightfield And displays it in
;; a foggy scene
;; ----------------------------------------------------------------------------
;;

;; ////////////////////////////////////////////////////////////////////////////
;; Include the Irr3D-Requester
XIncludeFile "Irr3DRequester.pb"

;; ////////////////////////////////////////////////////////////////////////////
;; Global variables

; irrlicht objects
*terrain.irr_terrain
*TerrainNode.irr_node
*TerrainTexture0.irr_texture
*TerrainTexture1.irr_texture
*camera.irr_camera
*CameraNode.irr_node



; -----------------------------------------------------------------------------
; To Start the Irrlicht-Device we use the Irr3DRequester, to make it more comfortable
; for the Users, and also easier to test different resolutions and devices.
If Irr3DRequester()

  ; send the window caption
  IrrSetWindowCaption( "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 = IrrAddTerrain( "media\terrain-heightmap.bmp" )
  
  ; 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
  IrrSetNodeScale( *TerrainNode, 40.0, 4.4, 40.0 )
  
  ; we load two textures in to apply to the terrain node
  *TerrainTexture0 = IrrGetTexture( "media\terrain-texture.jpg" )
  *TerrainTexture1 = IrrGetTexture( "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
  IrrSetNodeMaterialTexture( *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
  IrrSetNodeMaterialTexture( *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
  IrrScaleTexture( *terrain, 1.0, 20.0 )
  
  ; finally we apply some material texuring effects to the node
  ; the terrain is self illuminating
  IrrSetNodeMaterialFlag( *TerrainNode, #IRR_EMF_LIGHTING, #IRR_OFF )
  ; 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
  IrrSetNodeMaterialType ( *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
  IrrSetNodeMaterialFlag( *TerrainNode, #IRR_EMF_FOG_ENABLE, #IRR_ON )
  
  ; 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
  IrrSetFog ( 128,240,255,255, #IRR_EXPONENTIAL_FOG, 0.0,8000.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 = IrrAddFPSCamera()
  *CameraNode = *camera
  IrrSetNodePosition( *CameraNode, 3942.8, 1102.7, 5113.9 )
  IrrSetNodeRotation( *CameraNode, 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
  IrrSetCameraClipDistance( *camera, 12000 )
  
  ; we also hide the mouse pointer to see the view better
  IrrHideMouse()
  
  ; -----------------------------------------------------------------------------
  ; while the irrlicht environment is still running
  While IrrRunning()
      ; begin the scene, erasing the canvas with sky-blue before rendering
      IrrBeginScene( 240, 255, 255 )
  
      ; draw the scene
      IrrDrawScene()
  
;       IrrGetNodePosition(*camera,@x.f,@y.f,@z.f)
;       IrrTerrainHeight(*terrain,20,20,@Height.f)
;       Debug(StrF(*Height))
      
      ; end drawing the scene and render it
      IrrEndScene()
  Wend
  
  ; -----------------------------------------------------------------------------
  ; Stop the irrlicht engine and release resources
  IrrStop()
  
EndIf 


End

; IDE Options = PureBasic 4.10 Beta 2 (Windows - x86)
; CursorPosition = 87
; FirstLine = 69
; Folding = -