' ------------------------------------------------------------
'   n3xt-D exemples
'
'   Sample  019  : Parallax mapping scene with bumpmap earth
'   Historique  :
'     29/03/09  19:16    TMyke
'
' ------------------------------------------------------------
' Includes libraries
#include "N3xtD.bi"

' Dimes
Dim app As any Ptr
Dim Quit As Integer


'----------------------------------------------------------
' open n3xt-D screen
app = _CreateGraphics3D(800,600)
If app=NULL Then
  End
End If
 

'----------------------------------------
' fog definition
_Fog(&h00908050, EFT_FOG_LINEAR,  250.0, 1000.0,  0.003,  True,  FALSE )


'----------------------------------------
' Load 3D objects 
 Dim obj as Mesh = _LoadMesh("media/room.3ds")
' change planar texture mapping on the mesh
_MakePlanarTextureMappingMesh(obj, 0.003)
Dim obj2 as Mesh = _CreateMeshWithTangents(obj)
_FreeLoadedMesh(obj) 



'----------------------------------------
' Create room
Dim room as EntityNode =  _CreateEntityNode(obj2)
' Load two textures
Dim colormap as NTexture = _LoadTexture( "media/rockwall.bmp") 
Dim normalmap as NTexture = _LoadTexture( "media/rockwall_height.bmp") 
' Creates a normal map from a height map texture. 
_NormalMapTexture(normalmap, 10.0) 

' set texture in the New room (room2)
_MaterialTextureNode(room, colormap , 0)
_MaterialTextureNode(room, normalmap , 1)

' set material For room parallax mapping 
Dim material as NMaterial = _NodeMaterial(room, 0)
_SpecularColorMaterial(material, 0)
_MaterialTypeNode(room,  EMT_PARALLAX_MAP_SOLID )
_MaterialFlagNode(room,  EMF_FOG_ENABLE,  True)
_TypeParamMaterial(material, 1.0/64.0)
'----------------------------------------






'----------------------------------------
Dim obj1 as Mesh = _LoadMesh("media/earth.x")
Dim obj0 as Mesh = _CreateMeshWithTangents(obj1)
_FreeLoadedMesh(obj1) 
' set the alpha value of all vertices To 200
_VertexColorAlphaMesh(obj0,  190)
' scale And position mesh
_ScaleMesh(obj0, 65,65,65)


'----------------------------------------
' Create EARTH
Dim sphere as EntityNode = _CreateEntityNode(obj0)
' Create New sphere with tangent composants
_PositionNode(sphere, -70,130,45)
' Load heightmap, Create normal map from it And set it
Dim earthNormalMap as NTexture = _LoadTexture( "media/earthbump.bmp") 
' Creates a normal map from a height map texture. 
_NormalMapTexture(earthNormalMap, 9.0) 
_MaterialTextureNode(sphere, earthNormalMap , 1)
' adjust material settings
_MaterialTypeNode(sphere,  EMT_PARALLAX_MAP_TRANSPARENT_VERTEX_ALPHA )
_MaterialFlagNode(sphere,  EMF_FOG_ENABLE,  True)


'----------------------------------------
' Create light And set position
  Dim light as LightNode = _CreateLight(&h5588ff, 400)
  _PositionNode(light, 0,0,0)
  _CreateFlyCircleAnimator(light, 0,150,0, 200.0, 0.001)

'----------------------------------------
' create light and set position
  Dim light1 As LightNode = _CreateLight(&hff8800, 400)
  _CreateFlyCircleAnimator(light1, 50,300,0, 190.0, -0.003)


'----------------------------------------
'----------------------------------------
' Create PARTICLE SYSTEM
Dim part as ParticleSystemNode = _CreateParticleSystem(FALSE, light)
' Create a box particle emitter
Dim aabbox(6) as Single
  aabbox(0)=-3 : aabbox(1)=0 : aabbox(2)=-3 : aabbox(3)=3 : aabbox(4)=1 : aabbox(5)=3
  Dim box_emitter as ParticleEmitter = _CreateParticleBoxEmitter(part, 0, 0.03, 0, 80, 100, &h0Affffff, &h0Affffff, 400, 1000,0,5,5,5,5, Varptr( aabbox(0)))

' change SizeOf each particle inside the particle emitter
  _MinStartSizeParticle(box_emitter,  30.0, 40.0  )
  _MaxStartSizeParticle(box_emitter,  30.0, 40.0  )
' add New emitter inside the particlesystem
  _AddEmitterParticleSystem(part, box_emitter ) 
' fadeout element
  _FadeOutParticleSystem( part )

' adjust some material settings
  _LoadTextureNode(part, "media/fireball.bmp")
   
  Dim mat As NMaterial = _NodeMaterial(part, 0)
  _TypeMaterial(mat,  EMT_TRANSPARENT_ADD_COLOR)  
'----------------------------------------





'----------------------------------------
' Create a camera
Dim cam as CameraNode = _CreateCameraFPS( )
_PositionNode(cam, -145,215,-263)
_TargetCamera(cam, -70,130,45)

' simple text inside the scene area 
_LoadFont("media/courriernew.png")
Dim font as IGUIFont = _GetFont()


'---------------------------------------
'           Main loop
' ---------------------------------------
While Quit=0
 

' If Escape Key, Exit	
  If _GetKeyDown(KEY_ESCAPE) then
    Quit=1
  EndIf

 _TurnNode(sphere, 0,0.5,0)
 


  	' ---------------
  	'      Render
  	' ---------------
  _BeginScene(128,128,128)
    _DrawScene()
  _EndScene()

Wend 
' end
_FreeEngine()

