' ------------------------------------------------------------
'   n3xt-D exemples
'
'   Sample  201   as   Test HighLevelShaders.
'   Historique   as 
'     29/03/09  19 as 16    TMyke
'
' ------------------------------------------------------------

' Includes libraries
#include "N3xtD.bi"

declare Sub CallbackShader Cdecl(byval services as NMaterialServices,byval  userData as Integer)
Declare Sub CallbackShaderMaterial Cdecl(mat as NMaterial)


' 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

Dim video as Integer = EDT_DIRECT3D9

 
' Load an 3D Object 
Dim obj2 as Mesh = _CreateCubeMesh(1.0)


  
 ' Create a camera
Dim shared cam as CameraNode 
cam = _CreateCamera( )
_PositionNode(cam, 0,0,-5)
 
    
'-----------------------------------------
' Load And Create shader
Dim shad as Integer
Dim shad2 as Integer

_VersionShader(EVST_VS_1_1, EPST_PS_2_0)

If video = EDT_DIRECT3D9 Then
  shad = _CreateShaderHighLevel("media/d3d9.hlsl", "vertexMain", "media/d3d9.hlsl", "pixelMain", EMT_SOLID, @CallbackShader, @CallbackShaderMaterial)
  shad2 = _CreateShaderHighLevel("media/d3d9.hlsl", "vertexMain", "media/d3d9.hlsl", "pixelMain", EMT_TRANSPARENT_ADD_COLOR, @CallbackShader, @CallbackShaderMaterial)
Else
  shad = _CreateShaderHighLevel("media/opengl.vert", "vertexMain", "media/opengl.frag", "pixelMain", EMT_SOLID, @CallbackShader, @CallbackShaderMaterial)
  shad2 = _CreateShaderHighLevel("media/opengl.vert", "vertexMain", "media/opengl.frag", "pixelMain", EMT_TRANSPARENT_ADD_COLOR, @CallbackShader, @CallbackShaderMaterial)
EndIf


' Create Skybox
dim sky as EntityNode = _CreateSkybox( _LoadTexture("media/up.jpg"),_LoadTexture("media/dn.jpg"),_LoadTexture("media/lf.jpg"),_LoadTexture("media/rt.jpg"),_LoadTexture("media/ft.jpg"),_LoadTexture("media/bk.jpg"))



'-----------------------------------------
' add cube
Dim cube0 as EntityNode = _CreateEntityNode(obj2)
 _PositionNode(cube0, -1,-1,1)
 _LoadTextureNode(cube0, "media/wall.jpg")
 _MaterialFlagNode(cube0, EMF_LIGHTING, False)


'-----------------------------------------
' add second cube
Dim cube as EntityNode = _CreateEntityNode(obj2)
 _PositionNode(cube, 0,-1.3,0)
 _LoadTextureNode(cube, "media/wall.jpg")
 _MaterialFlagNode(cube, EMF_LIGHTING, False)
 _MaterialTypeNode(cube, shad)

'-----------------------------------------
' add third cube
Dim cube2 as EntityNode = _CreateEntityNode(obj2)
 _PositionNode(cube2, 1,-1.3,1)
 _LoadTextureNode(cube2, "media/wall.jpg")
 _MaterialFlagNode(cube2, EMF_LIGHTING, False)
 _MaterialTypeNode(cube2, shad)







'-----------------------------------
' 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

  
  _TurnNode(cube0, 0,0.5,0) 
  _TurnNode(cube, 0,0.5,0) 
  _TurnNode(cube2, 0,0.5,0) 
  


  	' ---------------
  	'      Render
  	' ---------------
  _BeginScene()
     _DrawScene()
     _DrawText(font, "FPS  as  "+ str(_FPS())  ,  10,25,0,0, &hffffffff)
     _DrawText(font, "Primitives as  "+ str(_PrimitiveCountDrawn())  ,  10,45,0,0, &hffffffff)
  _EndScene()


Wend 
' end
_FreeEngine()



Sub CallbackShaderMaterial Cdecl(mat as NMaterial)
End Sub

' ShaderCallback as 
' this procedure call at the each render pass.
' it is this area which will provide the data necessary
' For the shader run.
Sub CallbackShader Cdecl(ByVal services as NMaterialServices,  ByVal userData as Integer)
	
	Dim mvp(17) As Single
	Dim invWorld(17) as Single
	Dim worldViewProj(17) as Single
	Dim ppos(4) as Single
	Dim col(4) as Single
	dim world(17) As Single
	
	
  _GetWorldTransform(Varptr(invWorld(0)))
  _Matrix_Inverse(Varptr(invWorld(0)), Varptr(invWorld(0)))  	
  _VertexShaderConstantNMaterialServices(services, "mInvWorld",  Varptr(invWorld(0)),  16)
 
 	'set clip matrix
  _GetProjectionTransform(Varptr(worldViewProj(0)))
  _GetViewTransform(Varptr(mvp(0)))
  _Matrix_Mul(Varptr(worldViewProj(0)), Varptr(worldViewProj(0)), Varptr(mvp(0)))
  _GetWorldTransform(Varptr(mvp(0)))
  _Matrix_Mul(Varptr(worldViewProj(0)), Varptr(worldViewProj(0)), Varptr(mvp(0)))
  _VertexShaderConstantNMaterialServices(services, "mWorldViewProj",  Varptr(worldViewProj(0)),  16)

  ' set camera position
	_NodePosition(cam, Varptr(ppos(0)) )
  _VertexShaderConstantNMaterialServices(services, "mLightPos", Varptr(ppos(0)),  3)

  ' set light color
	col(0)=0.0
	col(1)=1.0
	col(2)=1.0
	col(3)=0.0
  _VertexShaderConstantNMaterialServices(services, "mLightColor", Varptr(col(0)),  4)
	' set transposed world matrix
  _GetWorldTransform(Varptr(world(0)))
  _Matrix_Transposed(Varptr(world(0)), Varptr(world(0))) 
  _VertexShaderConstantNMaterialServices(services, "mTransWorld",  Varptr(world(0)),  16)

End Sub

