' ------------------------------------------------------------
'   n3xt-D exemples
'
'   Sample 036   as   Ray Collide Test And
'                  used Octtree system For large model 
'   Historique   as 
'     29/03/09  19 as 16    TMyke
'
' ------------------------------------------------------------

' Includes libraries
#include "N3xtD.bi"

declare Sub DrawTriangle()

' 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

 



'----------------------------------- 
' add in memory management system a zip file 
 _AddZipFileArchive( "media/chiropteradm.pk3" )

'-----------------------------------
' Load Quake3 scene Object
  Dim obj as Mesh = _LoadMesh("chiropteradm.bsp")


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

 


'-----------------------------------------
' Create first  camera
Dim cam as CameraNode = _CreateCameraFPS( )
_PositionNode(cam, 0,20,-50)


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

'-----------------------------------
' define 3D Line ray test
Dim Lstart as iVECTOR3
dim Lend as iVECTOR3
Lstart.x=0  : Lstart.y=30 : Lstart.z=0
Lend.x=0  : Lend.y=-70  : Lend.z=0


' ---------------------------------------
'           Main loop
' ---------------------------------------
dim a as String
While Quit=0
 

' If Escape Key, Exit	
  If _GetKeyDown(KEY_ESCAPE) then
    Quit=1
  EndIf
  
      
  If _GetKeyUp(KEY_SPACE) Then
   Dim res as Integer = _CollisionPoint(Lstart.x,  Lstart.y,  Lstart.z,  Lend.x,  Lend.y,  Lend.z, quake)
   a = "collision result = " + str(res)
  EndIf
  
 
 	' move ray
  If _GetKeyDown(KEY_KEY_H) Then
    Lstart.x = Lstart.x+1
    Lend.x = Lend.x+1
  EndIf
  If _GetKeyDown(KEY_KEY_G) Then
    Lstart.x = Lstart.x-1
    Lend.x = Lend.x-1
  EndIf
  If _GetKeyDown(KEY_KEY_Y) Then
    Lstart.z = Lstart.z+1
    Lend.z = Lend.z+1
  EndIf
  If _GetKeyDown(KEY_KEY_B) Then
    Lstart.z = Lstart.z-1
    Lend.z = Lend.z-1
  EndIf





  	' ---------------
  	'      Render
  	' ---------------
  _BeginScene(250,250,250)
    _DrawScene()
       DrawTriangle()
       _DrawLine3D(Lstart.x,  Lstart.y,  Lstart.z,  Lend.x,  Lend.y,  Lend.z, &hff0000ff)
     _DrawText(font, "FPS  as  "+ str(_FPS())  ,  10,10,0,0, &hff00ffff)
     _DrawText(font, "Dir key and mous/Left-click for move camera",  10,25,0,0, &hffff00ff)
     _DrawText(font, "H/G/Y/B for move Ray and SpaceKey for test collide",  10,40,0,0, &hffff00ff)
     _DrawText(font, "Primitives as  "+ str(_PrimitiveCountDrawn())  ,  10,55,0,0, &hffffffff)
  _EndScene()


Wend 
' end
_FreeEngine()


Sub DrawTriangle()
  dim tri(8) As Single

  _Pickedtriangle (Varptr(tri(0)))
  _DrawTriangle3D( Varptr(tri(0)),  &hffff0000)
End sub 