/* -----------------------------------------------------------------------------
	GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */


		#include "binding_helpers.h"
		#include "script_squirrel/cobject/matrix_decl.h"
		#include "math/matrix4.h"
		#include "math/matrix3.h"

		using namespace GS;


//------------------------------------------------------------------------------
SQInteger			PushMatrix4(HSQUIRRELVM vm, const Matrix4 &mtx)
{
	push_Matrix4(vm, mtx);
/*
	if	(!CreateClassInstance(vm, "Matrix4", true))
		return sq_suspendvm(vm);

	SetTableKey("m00", sq_pushfloat, -1, mtx.m[0][0]);
	SetTableKey("m10", sq_pushfloat, -1, mtx.m[1][0]);
	SetTableKey("m20", sq_pushfloat, -1, mtx.m[2][0]);
	SetTableKey("m30", sq_pushfloat, -1, mtx.m[3][0]);
	SetTableKey("m01", sq_pushfloat, -1, mtx.m[0][1]);
	SetTableKey("m11", sq_pushfloat, -1, mtx.m[1][1]);
	SetTableKey("m21", sq_pushfloat, -1, mtx.m[2][1]);
	SetTableKey("m31", sq_pushfloat, -1, mtx.m[3][1]);
	SetTableKey("m02", sq_pushfloat, -1, mtx.m[0][2]);
	SetTableKey("m12", sq_pushfloat, -1, mtx.m[1][2]);
	SetTableKey("m22", sq_pushfloat, -1, mtx.m[2][2]);
	SetTableKey("m32", sq_pushfloat, -1, mtx.m[3][2]);
	SetTableKey("m03", sq_pushfloat, -1, mtx.m[0][3]);
	SetTableKey("m13", sq_pushfloat, -1, mtx.m[1][3]);
	SetTableKey("m23", sq_pushfloat, -1, mtx.m[2][3]);
	SetTableKey("m33", sq_pushfloat, -1, mtx.m[3][3]);
*/
	return 1;
}
SQInteger			GetMatrix4(HSQUIRRELVM vm, int idx, Matrix4 &mtx)
{
	GetTableKey("m00", sq_getfloat, idx, mtx.m[0][0]);
	GetTableKey("m10", sq_getfloat, idx, mtx.m[1][0]);
	GetTableKey("m20", sq_getfloat, idx, mtx.m[2][0]);
	GetTableKey("m30", sq_getfloat, idx, mtx.m[3][0]);
	GetTableKey("m01", sq_getfloat, idx, mtx.m[0][1]);
	GetTableKey("m11", sq_getfloat, idx, mtx.m[1][1]);
	GetTableKey("m21", sq_getfloat, idx, mtx.m[2][1]);
	GetTableKey("m31", sq_getfloat, idx, mtx.m[3][1]);
	GetTableKey("m02", sq_getfloat, idx, mtx.m[0][2]);
	GetTableKey("m12", sq_getfloat, idx, mtx.m[1][2]);
	GetTableKey("m22", sq_getfloat, idx, mtx.m[2][2]);
	GetTableKey("m32", sq_getfloat, idx, mtx.m[3][2]);
	GetTableKey("m03", sq_getfloat, idx, mtx.m[0][3]);
	GetTableKey("m13", sq_getfloat, idx, mtx.m[1][3]);
	GetTableKey("m23", sq_getfloat, idx, mtx.m[2][3]);
	GetTableKey("m33", sq_getfloat, idx, mtx.m[3][3]);
	return 0;
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
SQInteger			PushMatrix3(HSQUIRRELVM vm, const Matrix3 &mtx)
{
	push_Matrix3(vm, mtx);
/*
	if	(!CreateClassInstance(vm, "Matrix3", true))
		return sq_suspendvm(vm);

	SetTableKey("m00", sq_pushfloat, -1, mtx.m[0][0]);
	SetTableKey("m10", sq_pushfloat, -1, mtx.m[1][0]);
	SetTableKey("m20", sq_pushfloat, -1, mtx.m[2][0]);
	SetTableKey("m01", sq_pushfloat, -1, mtx.m[0][1]);
	SetTableKey("m11", sq_pushfloat, -1, mtx.m[1][1]);
	SetTableKey("m21", sq_pushfloat, -1, mtx.m[2][1]);
	SetTableKey("m02", sq_pushfloat, -1, mtx.m[0][2]);
	SetTableKey("m12", sq_pushfloat, -1, mtx.m[1][2]);
	SetTableKey("m22", sq_pushfloat, -1, mtx.m[2][2]);
*/
	return 1;
}
SQInteger			GetMatrix3(HSQUIRRELVM vm, int idx, Matrix3 &mtx)
{
	GetTableKey("m00", sq_getfloat, idx, mtx.m[0][0]);
	GetTableKey("m10", sq_getfloat, idx, mtx.m[1][0]);
	GetTableKey("m20", sq_getfloat, idx, mtx.m[2][0]);
	GetTableKey("m01", sq_getfloat, idx, mtx.m[0][1]);
	GetTableKey("m11", sq_getfloat, idx, mtx.m[1][1]);
	GetTableKey("m21", sq_getfloat, idx, mtx.m[2][1]);
	GetTableKey("m02", sq_getfloat, idx, mtx.m[0][2]);
	GetTableKey("m12", sq_getfloat, idx, mtx.m[1][2]);
	GetTableKey("m22", sq_getfloat, idx, mtx.m[2][2]);
	return 0;
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
SQInteger			RotationMatrixFromDirection(HSQUIRRELVM vm)
{
	__SQ_GETSINGLE(__SQ_GETVECTOR(direction))
	__SQ_RETURNMATRIX3(Matrix3::FromOrthonormalBasis(direction))
}
SQInteger			RotationMatrixFromDirectionAndUp(HSQUIRRELVM vm)
{
	__SQ_GETSTART(2)
	__SQ_GETVECTOR(direction)
	__SQ_GETVECTOR(up)
	__SQ_GETEND
	__SQ_RETURNMATRIX3(Matrix3::FromOrthonormalBasis(direction, &up))
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
SQInteger			EulerFromDirection(HSQUIRRELVM vm)
{
	__SQ_GETSINGLE(__SQ_GETVECTOR(direction))
	__SQ_RETURNVECTOR(Matrix3::FromOrthonormalBasis(direction).AsEuler())
}
SQInteger			EulerFromDirectionAndUp(HSQUIRRELVM vm)
{
	__SQ_GETSTART(2)
	__SQ_GETVECTOR(direction)
	__SQ_GETVECTOR(up)
	__SQ_GETEND
	__SQ_RETURNVECTOR(Matrix3::FromOrthonormalBasis(direction, &up).AsEuler())
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
SQInteger			MatrixToEuler(HSQUIRRELVM vm)
{
	__SQ_GETSTART(2)
	__SQ_GETMATRIX3(matrix)
	__SQ_GETINT(rorder)
	__SQ_GETEND
	__SQ_RETURNVECTOR(matrix.AsEuler((Math::rOrder)rorder))
}
SQInteger			TransformationMatrix(HSQUIRRELVM vm)
{
	__SQ_GETSTART(4)
	__SQ_GETVECTOR(p)
	__SQ_GETVECTOR(r)
	__SQ_GETVECTOR(s)
	__SQ_GETVECTOR(t)
	__SQ_GETEND
	__SQ_RETURNMATRIX4(Matrix4::TransformationMatrix(p, r, s, &t))
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
void		RegisterMatrixBinding(HSQUIRRELVM vm)
{
	using namespace GS::Script;

	sq_register(vm, MatrixToEuler, "MatrixToEuler", _SC(".xi"));
	sq_register(vm, TransformationMatrix, "TransformationMatrix", _SC(".xxxx"));

	sq_register(vm, RotationMatrixFromDirection, "RotationMatrixFromDirection", _SC(".x"));
	sq_register(vm, RotationMatrixFromDirectionAndUp, "RotationMatrixFromDirectionAndUp", _SC(".xx"));
	sq_register(vm, EulerFromDirection, "EulerFromDirection", _SC(".x"));
	sq_register(vm, EulerFromDirectionAndUp, "EulerFromDirectionAndUp", _SC(".xx"));
}
//------------------------------------------------------------------------------
