' ------------------------------------------------------------
'   n3xt-D exemples
'
'   Sample  011   as   render on texture.


'   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

 
' set ambient light
 _AmbientLight(&hffaaaaaa)
 
 
' Create mesh
Dim cubemesh as Mesh = _CreateCubeMesh()


'-----------------------------------------
' Create a cube And set position
Dim cube as EntityNode = _CreateEntityNode(cubemesh)
_PositionNode(cube, 0,0,0) 
' first Method To texturing
 Dim  material as NMaterial = _NodeMaterial(cube)
' Load texture
 Dim  tex as NTexture = _LoadTexture( "media/glass.bmp") 
' set texture in material 
  _TextureMaterial(material,  0, tex) 



'-----------------------------------------
' Create cube For receive texturerendered
 Dim target as EntityNode = _CreateEntityNode(cubemesh)
 _ScaleNode(target, 3,3,3)
 _PositionNode(target, -6,0,0) 
_MaterialFlagNode(target, EMF_LIGHTING, False)

' assign target texture on the *target cube
Dim textureTarget as NTexture = _CreateRenderTargetTexture( 256, 256)
_MaterialTextureNode(target, textureTarget, 0 )



' Create first  camera
Dim cam as CameraNode = _CreateCamera( )
_PositionNode(cam, -5,3,-10)

' Create second  camera For render target
dim cam2 as CameraNode = _CreateCamera( )
_PositionNode(cam2, 0,0,-3)


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

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

  _TurnNode(cube, 0,1,0)
  
  	' ---------------
  	'      Render
  	' ---------------
  _BeginScene()

    ' render traget texture with camera 2
    _ActiveCamera(cam2)
    _RenderTarget(textureTarget , True,  True,  &hff666600)
    ' this Method
      _RenderNode(cam2)
      _RenderNode(cube)
    ' <<Or>>
    '_VisibleNode(*target, #False)
    '_DrawScene()


    ' render with Main camera
    _ActiveCamera(cam)
    _RenderTarget(Null , True,  True,  0)
    _VisibleNode(target, True)
    _DrawScene()

  _EndScene()


Wend 
' end
_FreeEngine()