' ------------------------------------------------------------
'   n3xt-D exemples
'
'   Sample  017   as   alpha blending test mesh
'   Historique   as 
'     29/03/09  19 as 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
 
  
  
'----------------------------------------
' Create light And set position
  Dim light as LightNode = _CreateLight(&hffffffff, 100)
  _PositionNode(light, 0,15,-8)
  
  
  
Dim cubemesh as Mesh = _CreateCubeMesh(5.0)
Dim hillmesh as Mesh = _CreateHillPlaneMesh()


'----------------------------------------
' Create a first cube And set position
Dim cube as EntityNode = _CreateEntityNode(cubemesh)
_PositionNode(cube, 5,5,6) 
_LoadTextureNode(cube, "media/glass.bmp") 
Dim material as NMaterial = _NodeMaterial(cube, 0)
_TypeMaterial(material,  EMT_TRANSPARENT_ADD_COLOR )



'----------------------------------------
' Create a second cube And set position
 cube = _CreateEntityNode(cubemesh)
_PositionNode(cube, 0,5,0) 
_LoadTextureNode(cube, "media/glass.bmp")
' work on material now
material  = _NodeMaterial(cube)
_ColorMaterial(material)
_TypeMaterial(material,  EMT_ONETEXTURE_BLEND )
_TypeBlendMaterial(material, EBF_SRC_ALPHA, EBF_ONE_MINUS_SRC_ALPHA,	EMFN_MODULATE_1X,  EAS_TEXTURE OR EAS_VERTEX_COLOR) 




'----------------------------------------
' quick ground 
Dim hillplane as EntityNode = _CreateEntityNode(hillmesh)
_LoadTextureNode(hillplane, "media/wall.jpg") 



' Create a camera
dim cam as CameraNode = _CreateCameraFPS( )
_PositionNode(cam, 0,10,-18)


'-----------------------------------
' Load font png
_LoadFont("media/courriernew.png")
Dim font as IGUIFont = _GetFont()

' ---------------------------------------
'           Main loop
' ---------------------------------------
Dim alpha as Integer
Dim src as Integer=6, dest as Integer=7
dim cColor as integer
While Quit=0
 

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

 _TurnNode(cube, 0,0.5,0)

  
 If _GetKeyUp(KEY_KEY_Y) then
    src = src +1
      _TypeBlendMaterial(material, src, dest,	EMFN_MODULATE_1X, EAS_TEXTURE or EAS_VERTEX_COLOR) 
  EndIf
 If _GetKeyUp(KEY_KEY_H) Then
    src = src -1
      _TypeBlendMaterial(material, src, dest,	EMFN_MODULATE_1X,  EAS_TEXTURE Or EAS_VERTEX_COLOR) 
  EndIf
 If _GetKeyUp(KEY_KEY_U) Then
    dest = dest +1
      _TypeBlendMaterial(material, src, dest,	EMFN_MODULATE_1X,  EAS_TEXTURE Or EAS_VERTEX_COLOR) 
  EndIf
 If _GetKeyUp(KEY_KEY_J) Then
    dest = dest -1
      _TypeBlendMaterial(material, src, dest,	EMFN_MODULATE_1X,  EAS_TEXTURE Or EAS_VERTEX_COLOR) 
  EndIf
 

 
 
  If _GetKeyDown(KEY_KEY_T) Then
    alpha = alpha + 1
    If(alpha>254) Then alpha = 0
    cColor = &h01000000 * alpha + &h010000*alpha + &h0100 * alpha + alpha
    _DiffuseColorNode(cube,  ccolor)
  EndIf
  If _GetKeyDown(KEY_KEY_G) Then
     alpha = alpha - 1
    If(alpha<1) Then alpha = 255
    cColor  = &h01000000 * alpha + &h010000*alpha + &h0100 * alpha + alpha
    _DiffuseColorNode(cube,  ccolor)
  EndIf
  



  	' ---------------
  	'      Render
  	' ---------------
  _BeginScene()
     _DrawScene()
     _DrawText(font, "Key T/G to change alpha as "+ str(alpha),  10,10,0,0)
     _DrawText(font, "Key Y/H to change src  as "+ str(src),  10,30,0,0)
     _DrawText(font, "Key U/J to changedest as "+ str(dest),  10,45,0,0)
   _EndScene()


Wend 
' end
_FreeEngine()

