' ------------------------------------------------------------
'   n3xt-D exemples
'
'   Sample  015  : primitives with a spot light and material.
'   Historique  :
'     29/03/09  19:16    TMyke
'
' ------------------------------------------------------------

' Includes libraries
#include "N3xtD.bi"

' Globales
Dim app As any Ptr
Dim Quit As Integer


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


 
'----------------------------------------
' Create light And set position
Dim light as LightNode = _CreateLight()
  _PositionNode(light, 0,15,-8)
' change light Type -> SPOTLIGHT 
 _TypeLight( light, ELT_SPOT) ' Or  iTypeLight( light, ELT_DIRECTIONAL)
' redefine internal parameters
 _RadiusLight( light,  30)
 _RotateNode( light, 60,0,0)
 _OuterConeLight( light, 30)
 _InnerConeLight( light,  25 )
' color light set element 
'_AmbientColorLight( light, &hff999999)
'_DiffuseColorLight( light, &hff555555)



'----------------------------------------
' Create mesh
Dim cubemesh as Mesh = _CreateCubeMesh()
Dim spheremesh as Mesh = _CreateSphereMesh()
Dim cylindermesh as Mesh= _CreateCylinderMesh(1.0, 3.0)
 
 
' Create a cube And set position
Dim cube as EntityNode = _CreateEntityNode(cubemesh)
_PositionNode(cube, -6,0,0) 
' first Method set
Dim mat as NMaterial = _NodeMaterial(cube)
_ColorMaterial(mat)
_DiffuseColorMaterial(mat, &hff000044)
_ShadowVolumeEntity(cube)


' Create a sphere And set position
Dim sphere as EntityNode = _CreateEntityNode(spheremesh)
_PositionNode(sphere, -2,0,0) 
' second metohd set
_MaterialColorNode(sphere)
_DiffuseColorNode(sphere, &hff004400) 
_ShadowVolumeEntity(sphere)

' Create a cylinder And set position
Dim cylinder as EntityNode = _CreateEntityNode(cylindermesh)
_PositionNode(cylinder, 2,2,0) 
_RotateNode(cylinder, 0,0,90) 
' second metohd set
_MaterialColorNode(cylinder)
_DiffuseColorNode(cylinder, &hff440000) 
_ShadowVolumeEntity(cylinder)




'----------------------------------------
' quick ground For light spot test
Dim hillplanemesh as Mesh = _CreateHillPlaneMesh(50.0,50.0, 80,80)

Dim hillplane as EntityNode = _CreateEntityNode(hillplanemesh)
_PositionNode(hillplane, 0,-2,0) 
_ScaleMesh(hillplanemesh, 0.01, 0.01, 0.01)
' second metohd set
_AmbientColorNode(hillplane, &hff444444) 
_DiffuseColorNode(hillplane, &hff888888) 



' Create a camera
Dim cam as CameraNode = _CreateCamera( )
_PositionNode(cam, 0,10,-13)
_RotateNode(cam, 20,0,0)


'-----------------------------------
' Load font png
_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
    

  ' move light with dir key And mouse (Left click)
  If _GetKeyDown(KEY_ARROW_RIGHT) Then _MoveNode(light, 0.5,0,0)
  If _GetKeyDown(KEY_ARROW_LEFT) Then _MoveNode(light, -0.5,0,0) 
  If _GetKeyDown(KEY_ARROW_UP) Then _MoveNode(light, 0,0,0.5)
  If _GetKeyDown(KEY_ARROW_DOWN) Then _MoveNode(light, 0,0,-0.5) 
  If _GetMouseEvent(MOUSE_BUTTON_LEFT) then
    Dim angley as single =  _GetDeltaMouseX() / 4.0
    Dim anglex as Single =  _GetDeltaMouseY() / 4.0
    _TurnNode(light, anglex, angley,0)
  EndIf




  	'; ---------------
  	';      Render
  	'; ---------------
  _BeginScene()
     _DrawScene()
     _DrawText(font, "for move spot light: dir key / mouse with leftclick",  10,10,0,0)
  _EndScene()


Wend 
' end
_FreeEngine()
