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


		#include "binding_helpers.h"
		#include "raytracer/raytracer_core.h"
		#include "core/graphic_resource_factory.h"
		#include "core/resource_factories.h"
		#include "core/geometry.h"
		#include "picture/pict_io.h"

		using namespace GS;
		using namespace GS::Script;
		using namespace GS::Raytrace;


//------------------------------------------------------------------------------
SQInteger			NewRaytracer(HSQUIRRELVM vm)
{
	__SQ_GETSINGLESAFEPTR(f, Core::ResourceFactories, typetag_ResourceFactories)
	__SQ_RETURNSAFEPTR(new Raytracer(f->graphic), typetag_Raytracer)
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
SQInteger			RaytracerSetScene(HSQUIRRELVM vm)
{
	__SQ_GETSTART(2)
	__SQ_GETSAFEPTR(ray, Raytracer, typetag_Raytracer)
	__SQ_GETSAFEPTR(scn, S3D::Scene, typetag_Scene3d)
	__SQ_GETEND
	__SQ_RETURNBOOL((ray && scn) ? ray->SetScene(scn) : false)
}
SQInteger			RaytracerTrace(HSQUIRRELVM vm)
{
	__SQ_GETSTART(4)
	__SQ_GETSAFEPTR(ray, Raytracer, typetag_Raytracer)
	__SQ_GETSTRING(file)
	__SQ_GETINT(width)
	__SQ_GETINT(height)

	bool r = false;
	Picture output(width, height);
	if	(ray->Render(output, width, height))
		r = output.isValid() ? PictureIO::Get().TgaSave(output, file) : false;

	__SQ_GETEND
	__SQ_RETURNBOOL(r)
}
SQInteger			RaytracerSetInterlace(HSQUIRRELVM vm)
{
	__SQ_GETSTART(3)
	__SQ_GETSAFEPTR(ray, Raytracer, typetag_Raytracer)
	__SQ_GETBOOL(interlaced)
	__SQ_GETBOOL(interlace_even)
	__SQ_GETEND

	Configuration config = ray->GetConfiguration();
	config.interlaced = interlaced ? true : false;
	config.interlace_even = interlace_even ? true : false;
	ray->SetConfiguration(config);

	__SQ_RETURN
}
SQInteger			RaytracerSetAntialias(HSQUIRRELVM vm)
{
	__SQ_GETSTART(5)
	__SQ_GETSAFEPTR(ray, Raytracer, typetag_Raytracer)
	__SQ_GETBOOL(trace_aa)
	__SQ_GETINT(aa_sample)
	__SQ_GETFLOAT(aa_threshold)
	__SQ_GETBOOL(aa_jitter)
	__SQ_GETEND

	Configuration config = ray->GetConfiguration();
	config.trace_aa = trace_aa ? true : false;
	config.aa_sample = aa_sample;
	config.aa_threshold = aa_threshold;
	config.aa_jitter = aa_jitter ? true : false;
	ray->SetConfiguration(config);

	__SQ_RETURN
}
SQInteger			RaytracerSetGlobalIllum(HSQUIRRELVM vm)
{
	__SQ_GETSTART(4)
	__SQ_GETSAFEPTR(ray, Raytracer, typetag_Raytracer)
	__SQ_GETBOOL(trace_gi)
	__SQ_GETINT(gi_sample)
	__SQ_GETINT(indirect_gi_bounce)
	__SQ_GETEND

	Configuration config = ray->GetConfiguration();
	config.trace_gi = trace_gi ? true : false;
	config.gi_sample = gi_sample;
	config.indirect_gi_bounce = indirect_gi_bounce;
	ray->SetConfiguration(config);

	__SQ_RETURN
}
SQInteger			RaytracerStartInterlacedSequence(HSQUIRRELVM vm)
{
	__SQ_GETSINGLESAFEPTR(ray, Raytracer, typetag_Raytracer)
	ray->StartInterlacedSequence();
	__SQ_RETURN
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
void		RegisterRaytracerBinding(HSQUIRRELVM vm)
{
/*#
	Topic: Raytracer
#*/

/*#
	Section: RaytracerGeneric
	Desc: Generic functions
#*/
	/*#
		Func: NewRaytracer
		Proto: Raytracer:ResourceFactory
		Desc: Create a new raytracer instance.
	#*/
	sq_register(vm, NewRaytracer, "NewRaytracer", _SC(".x"));
	/*#
		Func: RaytracerSetAntialias
		Proto: void:raytracer,bool enable,int sample_count,float threshold,bool jitter
		Desc: Set the raytracer antialias output. The number of AA sample can be set and jitered AA selected. Default threshold: 0.015.
	#*/
	sq_register(vm, RaytracerSetAntialias, "RaytracerSetAntialias", _SC(".xbinb"));
	/*#
		Func: RaytracerSetGlobalIllum
		Proto: void:raytracer,bool enable,int sample_count,int max_indirect_bounce
		Desc: Enable the raytracer global illumination. The number of GI sample can be set as well as a maximum number of indirect bounce.
	#*/
	sq_register(vm, RaytracerSetGlobalIllum, "RaytracerSetGlobalIllum", _SC(".xbii"));
	/*#
		Func: RaytracerSetInterlace
		Proto: void:raytracer,bool interlaced,bool even_frame
		Desc: Set the raytracer interlace output. The frame parity is specified as the second parameter.
	#*/
	sq_register(vm, RaytracerSetInterlace, "RaytracerSetInterlace", _SC(".xbb"));
	/*#
		Func: RaytracerStartInterlacedSequence
		Proto: void:raytracer
		Desc: Start an interlaced sequence. This function should be called when beginning a new sequence in order to ensure correct frame parity.
	#*/
	sq_register(vm, RaytracerStartInterlacedSequence, "RaytracerStartInterlacedSequence", _SC(".x"));
	/*#
		Func: RaytracerSetScene
		Proto: void:raytracer,scene
		Desc: Set raytracer scene, this function might be a little slow as it setups a lot data to work with.
	#*/
	sq_register(vm, RaytracerSetScene, "RaytracerSetScene", _SC(".xx"));
	/*#
		Func: RaytracerTrace
		Proto: void:raytracer,string out_picture,int width,int height
		Desc: Raytrace current scene to a Targa picture file. Note that when rendering interlaced sequences one of two calls to this function will return false to notify you that the current frame was only buffered and not saved.
	#*/
	sq_register(vm, RaytracerTrace, "RaytracerTrace", _SC(".xsii"));
}
//------------------------------------------------------------------------------
