'' ----------------------------------------------------------------------------
'' Irrlicht Wrapper For Imperative Languages - Freebasic Examples
'' Frank Dodd (2006)
'' ----------------------------------------------------------------------------
'' Blitz-Irrlicht 1.8.3 Interface by Ed Musolino (AquaLung 2015)
'' ----------------------------------------------------------------------------
'' Example 93 : 3D Position from Screen Coordinates
'' This example demonstrates how To get a 3D position in space from a set of
'' screen coordinates And the definition of a 2D plane in 3D space. this can
'' be particularly useful in a scene management application For constraining
'' the movement of an Object To a specific axis
'' ----------------------------------------------------------------------------

'' ////////////////////////////////////////////////////////////////////////////
'' Includes For extension libraries
Framework usr.birrlicht

'' ////////////////////////////////////////////////////////////////////////////
'' Global variables

' irrlicht objects
Global MyTexture:Int
Global CenterNode:Int
Global IndicatorObject:Int
Global Camera:Int
Global XPOS:Float
Global YPOS:Float
Global ZPOS:Float
Global normalX:Float = 0.0
Global normalY:Float = 0.0
Global normalZ:float = 1.0


'' ////////////////////////////////////////////////////////////////////////////
' start the irrlicht interface
iIrr3D( 800, 600, 32, False )

' send the window caption
iAppTitle( "Example 93 : 3D Position from Screen Coordinates" )

' add a bright ambient light To the scene To brighten everything up
iAmbientLight( 1,1,1 )

' first Load a texture
MyTexture = iLoadTexture( "./media/texture.jpg" )

' add a simple test node so we can visualise the world origin
CenterNode = iCreateCube( 8, False )

' texture the node
iNodeTexture( CenterNode, MyTexture, 0 )


' add a sphere Object that will mark the point in 3D space
' the sphere we Create has a radius of 10.0 And is made from rings of 12
' verticies
IndicatorObject = iCreateSphere( 10.0, False )', 12)
iScaleNode( IndicatorObject, 5, 5, 5 )

' texture the sphere
iNodeTexture( IndicatorObject, MyTexture, 0 )

' add a static camera To the scene
Camera = iCreateCamera( False )
iPositionNode( Camera, 100, 100, 100 )

' Hide the mouse pointer
iHideMouse()

iCameraCLSColor( 255, 255, 255 )

' -----------------------------------------------------------------------------
' While the irrlicht environment is still running
While iRun() And (Not iKeyHit( KEY_ESCAPE ))

	' begin the scene
	iBeginScene()
    
	If iKeyHit( KEY_KEY_X )
		normalX  = 1.0
		normalY  = 0.0
		normalZ  = 0.0
	End If
	
	If iKeyHit( KEY_KEY_Y )
		normalX  = 0.0
		normalY  = 1.0
		normalZ  = 0.0
	End If
	
	If iKeyHit( KEY_KEY_Z )
		normalX  = 0.0
		normalY  = 0.0
		normalZ  = 1.0
	End If
	
	MX% = iMouseX()
	MY% = iMouseY()
	
	' get the position of the Object projected against the plane
	XPOS = i3DPositionXFromScreen( MX, MY, Camera, normalX, normalY, normalZ, 0.0 )
	YPOS = i3DPositionYFromScreen( MX, MY, Camera, normalX, normalY, normalZ, 0.0 )
	ZPOS = i3DPositionZFromScreen( MX, MY, Camera, normalX, normalY, normalZ, 0.0 )
	
	' update the position of the indicator Object
	iPositionNode( IndicatorObject, XPOS , YPOS, ZPOS )

	' draw the scene
	iDrawScene()

	' End drawing the scene And render it
	iEndScene()     

Wend

' -----------------------------------------------------------------------------
' Stop the irrlicht engine And Release resources
iEndIrr3D()


End