// ----------------------------------------------------------------------------
// Blitz-Irrlicht 1.8.3 Wrapper by Ed Musolino 2015
// Based off of Frank Dodd's Irrlicht 1.7 Freebasic Wrapper (2006-2010)
// ----------------------------------------------------------------------------

//  This software is provided 'as-is', without any express Or implied
//  warranty.  In no event will the authors be held liable For any damages
//  arising from the use of this software.

//  Permission is granted To anyone To use this software For any purpose,
//  including commercial applications, And To alter it And redistribute it
//  freely, subject To the following restrictions:

//  1. The Origin of this software must Not be misrepresented; you must not
//     claim that you wrote the original software. If you use this software
//     in a product, an acknowledgement in the product documentation would be
//     appreciated but is Not required.
//  2. Altered source versions must be clearly marked as such, And must Not be
//     misrepresented as being the original software.
//  3. This notice may Not be removed Or altered from any source distribution.
//
// /////////////////////////////////////////////////////////////////////////////

#include "bIrrlicht.h"


extern IrrlichtDevice*		device;
extern IVideoDriver*		driver;
extern ISceneManager*		smgr;
extern IGUIEnvironment*		guienv;


extern "C"
{

DLL_EXPORT int * BBCALL iCreateFPSCamera( ISceneNode *  parent , float rotateSpeed , float moveSpeed , int id , SKeyMap *  keyMapArray , int keyMapSize , bool noVerticalMovement , float jumpSpeed  )
{
	return (int *)smgr->addCameraSceneNodeFPS( parent, rotateSpeed, moveSpeed, id, keyMapArray, keyMapSize, noVerticalMovement, jumpSpeed);
}


DLL_EXPORT int * BBCALL iCreateCamera( ISceneNode* parent )
{
	/*
	To look at the mesh, we place a camera into 3d space at the position
	(0, 30, -40). The camera looks from there to (0,5,0).
	*/
	return (int *)smgr->addCameraSceneNode(parent,
            vector3df( 60, 5, 0 ),
            vector3df( 0, 5, 0 )
    );
}


DLL_EXPORT int * BBCALL iCreateMayaCamera( ISceneNode *  parent , float rotateSpeed , float zoomSpeed, float moveSpeed   )
{
    return (int *)smgr->addCameraSceneNodeMaya( parent, rotateSpeed, zoomSpeed, moveSpeed, -1 );
}

/* ----------------------------------------------------------------------------
reposition the target location of a camera
*/
DLL_EXPORT void BBCALL iCameraTarget ( ICameraSceneNode* camera, float X, float Y,  float Z )
{
	camera->setTarget(core::vector3df(X,Y,Z));
}

/* ----------------------------------------------------------------------------
get the current target location of a camera
*/
DLL_EXPORT float BBCALL iCameraTargetX ( ICameraSceneNode* camera )
{
	vector3df vect = camera->getTarget();
	return (float)vect.X;
}
DLL_EXPORT float BBCALL iCameraTargetY ( ICameraSceneNode* camera )
{
	vector3df vect = camera->getTarget();
	return (float)vect.Y;
}
DLL_EXPORT float BBCALL iCameraTargetZ ( ICameraSceneNode* camera )
{
	vector3df vect = camera->getTarget();
	return (float)vect.Z;
}

/* ----------------------------------------------------------------------------
Get the up vector of the camera
*/
DLL_EXPORT float BBCALL iCameraUpX ( ICameraSceneNode* camera )
{
    vector3df curVector = camera->getUpVector();
    return (float)curVector.X;
}
DLL_EXPORT float BBCALL iCameraUpY ( ICameraSceneNode* camera )
{
    vector3df curVector = camera->getUpVector();
    return (float)curVector.Y;
}
DLL_EXPORT float BBCALL iCameraUpZ ( ICameraSceneNode* camera )
{
    vector3df curVector = camera->getUpVector();
    return (float)curVector.Z;
}

/* ----------------------------------------------------------------------------
Get the forward vector of the camera
*/
DLL_EXPORT float BBCALL iCameraForwardX ( ICameraSceneNode* camera )
{
    vector3df cameraForward = (camera->getTarget() - camera->getPosition()).normalize();
    return (float)cameraForward.X;
}
DLL_EXPORT float BBCALL iCameraForwardY ( ICameraSceneNode* camera )
{
    vector3df cameraForward = (camera->getTarget() - camera->getPosition()).normalize();
    return (float)cameraForward.Y;
}
DLL_EXPORT float BBCALL iCameraForwardZ ( ICameraSceneNode* camera )
{
    vector3df cameraForward = (camera->getTarget() - camera->getPosition()).normalize();
    return (float)cameraForward.Z;
}

/* ----------------------------------------------------------------------------
Get the forward vector of the camera
*/
DLL_EXPORT float BBCALL iCameraRightX ( ICameraSceneNode* camera )
{
    vector3df cameraUp = camera->getUpVector();
    vector3df cameraForward = (camera->getTarget() - camera->getPosition()).normalize();
    vector3df cameraRight = cameraForward.crossProduct(cameraUp);

    return (float)cameraRight.X;
}
DLL_EXPORT float BBCALL iCameraRightY ( ICameraSceneNode* camera )
{
    vector3df cameraUp = camera->getUpVector();
    vector3df cameraForward = (camera->getTarget() - camera->getPosition()).normalize();
    vector3df cameraRight = cameraForward.crossProduct(cameraUp);

    return (float)cameraRight.Y;
}
DLL_EXPORT float BBCALL iCameraRightZ ( ICameraSceneNode* camera )
{
    vector3df cameraUp = camera->getUpVector();
    vector3df cameraForward = (camera->getTarget() - camera->getPosition()).normalize();
    vector3df cameraRight = cameraForward.crossProduct(cameraUp);

    return (float)cameraRight.Z;
}

/* ----------------------------------------------------------------------------
set the up vector of the camera allowing the camera to be rolled
*/
DLL_EXPORT void BBCALL iCameraUp ( ICameraSceneNode* camera, float X, float Y,  float Z )
{
    camera->setUpVector(core::vector3df(X,Y,Z));
}

/* ----------------------------------------------------------------------------
set the distance at which the camera starts to clip polys
*/
DLL_EXPORT void BBCALL iCameraRange ( ICameraSceneNode* camera, float farDistance, float nearDistance )
{
	camera->setFarValue(farDistance);
	camera->setNearValue(nearDistance);
}

/* ----------------------------------------------------------------------------
set the active camera in the scene
*/
DLL_EXPORT void BBCALL iActiveCamera ( ICameraSceneNode* camera )
{
    smgr->setActiveCamera(camera);
}

/* ----------------------------------------------------------------------------
sets the field of view (Default: PI / 2.5f).
*/
DLL_EXPORT void BBCALL iCameraFOV ( ICameraSceneNode* camera, float fov )
{
	camera->setFOV(fov);
}

/* ----------------------------------------------------------------------------
set the aspect ratio of a camera
*/
DLL_EXPORT void BBCALL iCameraAspectRatio ( ICameraSceneNode* camera, float aspectRatio )
{
	camera->setAspectRatio( aspectRatio );
}

}