' ------------------------------------------------------------
'   n3xt-D exemples
'
'   Sample  203 as   FIRST POSTPROCESSING TEST.
'   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 EffectRender()

' 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


dim shaderparameters(7) as Single
dim video as Integer = EDT_DIRECT3D9


_ChangeWorkingDirectory("media")

  
 '----------------------------------- 
' add in memory management system a zip file 
 _AddZipFileArchive( "map-20kdm2.pk3" )


'-----------------------------------
' Load Quake3 scene Object
  dim obj as Mesh = _LoadMesh("20kdm2.bsp")

'-----------------------------------
' Create a mesh with one of the 3D Object loaded
  dim scene as EntityNode = _CreateEntityNode(obj)




'-----------------------------------------
' Load And Create shader
dim shad as Integer
_VersionShader(EVST_VS_1_1, EPST_PS_2_0)

If video = EDT_DIRECT3D9 Then
  shad = _CreateShaderHighLevel("Shaders/PP_DX_Vertex.fx","main", "Shaders/PP_DX_Invert.fx", "main", EMT_SOLID, @CallbackShader, Null)

Else
  shad = _CreateShaderHighLevel("Shaders/PP_GL_Vertex.fx","main", "Shaders/PP_GL_Invert.fx", "main", EMT_SOLID, @CallbackShader, Null)

  'shad = _CreateShaderHighLevel("Shaders/PP_GL_Vertex.fx","main", "Shaders/PP_GL_Bloom1.fx", "main", EMT_SOLID, @CallbackShader, Null)
  'shaderparameters(0)=0.10

  'shad = _CreateShaderHighLevel("Shaders/PP_GL_Vertex.fx","main", "Shaders/PP_GL_Bloom2.fx", "main", EMT_SOLID, @CallbackShader, Null)
  'shaderparameters(0)=0.00185

  'shad = _CreateShaderHighLevel("Shaders/PP_GL_Vertex.fx","main", "Shaders/PP_GL_Bloom3.fx", "main", EMT_SOLID, @CallbackShader, Null)
  'shaderparameters(0)=0.00185
  
  'shad = _CreateShaderHighLevel("Shaders/PP_GL_Vertex.fx","main", "Shaders/PP_GL_Bloom4.fx", "main", EMT_SOLID, @CallbackShader, Null)
  'shaderparameters(0)=0.98
EndIf


'---------------------------
' quad plane definition
' define 4 vertices 
dim shared id(6) as Short
dim shared vvv(9*4) as single

  vvv(0) = -1  : vvv(1) = -1  : vvv(2) = 0 : vvv(3) = 0  : vvv(4) = 0 : vvv(5) = 0 : vvv(6) = &hff00ffff : vvv(7) = 0  : vvv(8) = 1
  vvv(9) = -1  : vvv(10) =  1  : vvv(11) = 0  : vvv(12) = 0  : vvv(13) = 0 : vvv(14) = 0: vvv(15) = &hff00ffff : vvv(16) = 0  : vvv(17) = 0
  vvv(18) =  1  : vvv(19) =  1  : vvv(20) = 0  : vvv(21) = 0  : vvv(22) = 0 : vvv(23) = 0: vvv(24) = &hff00ffff : vvv(25) = 1  : vvv(26) = 0
  vvv(27) =  1  : vvv(28) = -1  : vvv(29) = 0  : vvv(30) = 0  : vvv(31) = 0 : vvv(32) = 0: vvv(33) = &hff00ffff : vvv(34) = 1  : vvv(35) = 1
' define 6 indices, For two triangles  
  id(0) = 0 : id(1) = 1 : id(2) = 2
  id(3) = 0 : id(4) = 2 : id(5) = 3


' texture render definition
dim firstmap as NTexture = _CreateRenderTargetTexture( 512, 512)
'dim secondmap as NTexture = Null
' New material
dim shared material as NMaterial 
material = _CreateMaterial()
_FlagMaterial( material, EMF_WIREFRAME, False)
_FlagMaterial( material, EMF_LIGHTING, False)
_TextureMaterial(material, 0, firstmap)
'_TextureMaterial(material, 1, secondmap)
_FlagMaterial(material, EMF_TEXTURE_WRAP, ETC_CLAMP) 
_TypeMaterial(material, shad) 



'-----------------------------------
' Create And position  camera
  dim cam as CameraNode = _CreateCameraFPS()
  _PositionNode(cam, 726, 724, 330)
  _TargetCamera(cam, 800,730,400)




' ---------------------------------------
'           Main loop
' ---------------------------------------
While Quit=0
 
' If Escape Key, Exit	
  If _GetKeyDown(KEY_ESCAPE) then
    Quit=1
  EndIf




  ' ---------------
  '      Render
  ' ---------------
  _BeginScene(100,100,100)
     ' element of scene render 
     _RenderTarget( firstmap , True,  True,  &hff660000)
        _DrawScene()
 
     ' effect render
     _RenderTarget( Null , True,  True,  0)
        ' render effect
        EffectRender()
    
  _EndScene()


Wend 
' end
_FreeEngine()



'--------------------------------
' effect render procedure
sub EffectRender()
  _SetMaterial(material) 
  '_DrawIndexedTriangleList(Varptr(vvv(0)), 4, Varptr(id(0)),  2)
  _DrawIndexedTriangleList(@vvv(0), 4, @id(0),  2)
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 text1 as Integer, text2 as Integer	
  
  ' with Bloom process as 
  '_PixelShaderConstantNMaterialServices(services,	"vecValues" ,  varptr(shaderparameters(0)),   8)

  If(userData = 1) Then
    text1 = 0
    _PixelShaderConstantNMaterialServices(services,	"texture1" ,  Varptr(text1),   1)
    text2 = 1
    '_PixelShaderConstantNMaterialServices(services,	"texture2" ,  Varptr(text2),   1)
  EndIf

End sub
