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


#ifndef	__COBJECT_GEOMETRY_TEMPLATE_IMPL__
#define __COBJECT_GEOMETRY_TEMPLATE_IMPL__


		#include "script_squirrel/cobject/geometry_template_decl.h"
		#include "script_squirrel/cobject/cobject_decl.h"
		#include "script_squirrel/cobject/vector_decl.h"
		#include "script_squirrel/cobject/uv_decl.h"
		#include "script_squirrel/cobject/cobject.h"
		#include "core/geometry_template.h"
		#include "core/resource_factories.h"
		#include "core/render_resource_factory.h"
		#include "core/render_data.h"
		#include "core/renderer.h"
		#include "core/engine.h"

		using namespace GS;
		using namespace GS::Core;
		using namespace GS::Script;


//------------------------------------------------------------------------------
#define	_GetGeometryTemplate(_VAR, _IDX)	_GetTypedParam(_VAR, _IDX, GeometryTemplate, GeometryTemplate)
//------------------------------------------------------------------------------

_IMPL_NATIVE_CONSTRUCTION(GeometryTemplate, GeometryTemplate);

//------------------------------------------------------------------------------
_MEMBER_FUNCTION_IMPL(GeometryTemplate, constructor)
	return construct_GeometryTemplate(v, new GeometryTemplate);
_END_IMPL
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
_MEMBER_FUNCTION_IMPL(GeometryTemplate, clearMaterial)
	_GetGeometryTemplate(t, 1)
	t->ClearMaterials();
	return 0;
_END_IMPL

_MEMBER_FUNCTION_IMPL(GeometryTemplate, pushMaterial)
	_GetGeometryTemplate(t, 1)
	const SQChar *name = sa.GetString(2);
	t->PushMaterial(name);
	return 0;
_END_IMPL
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
_MEMBER_FUNCTION_IMPL(GeometryTemplate, clear)
	_GetGeometryTemplate(t, 1)
	t->Clear();
	return 0;
_END_IMPL

_MEMBER_FUNCTION_IMPL(GeometryTemplate, beginPolygon)
	_GetGeometryTemplate(t, 1)
	t->BeginPolygon();
	return 0;
_END_IMPL

_MEMBER_FUNCTION_IMPL(GeometryTemplate, pushVertex)
	_GetGeometryTemplate(t, 1)
	_GetTypedParam(_v, 2, Vector4, Vector)
	t->PushVertex(*_v);
	return 0;
_END_IMPL
_MEMBER_FUNCTION_IMPL(GeometryTemplate, pushNormal)
	_GetGeometryTemplate(t, 1)
	_GetTypedParam(n, 2, Vector4, Vector)
	t->PushNormal(*n);
	return 0;
_END_IMPL
_MEMBER_FUNCTION_IMPL(GeometryTemplate, pushColor)
	_GetGeometryTemplate(t, 1)
	_GetTypedParam(c, 2, Vector4, Vector)
	t->PushColor(Color(*c));
	return 0;
_END_IMPL
_MEMBER_FUNCTION_IMPL(GeometryTemplate, pushUV)
	_GetGeometryTemplate(t, 1)
	_GetTypedParam(uv, 3, Vector2, UV)
	t->PushUV(sa.GetInt(2), *uv);
	return 0;
_END_IMPL

_MEMBER_FUNCTION_IMPL(GeometryTemplate, endPolygon)
	_GetGeometryTemplate(t, 1)
	t->EndPolygon((ushort)sa.GetInt(2));
	return 0;
_END_IMPL
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
_MEMBER_FUNCTION_IMPL(GeometryTemplate, setVertexMergeThreshold)
	_GetGeometryTemplate(t, 1)
	t->SetVertexMergeThreshold(sa.GetFloat(2));
	return 0;
_END_IMPL
_MEMBER_FUNCTION_IMPL(GeometryTemplate, instantiate)
	_GetGeometryTemplate(t, 1)
	_GetCObject(ResourceFactories, typetag_ResourceFactories, f, 2)
	AutoPtr <Geometry> g(t->Instantiate(sa.GetString(3)));
	Render::Geometry *r = f->render->NewGeometry();
	r->Create(*f->render, *g);
	_ReturnCObject(r, typetag_Geometry)
_END_IMPL
//------------------------------------------------------------------------------


//# Class: GeometryTemplate
_BEGIN_CLASS(GeometryTemplate)

_MEMBER_FUNCTION(GeometryTemplate, constructor, 1, _SC("."))

//------------------------------------------------------------------------------
//# Topic: Polygon creation
//------------------------------------------------------------------------------

/*#
	Func: beginPolygon
	Proto: void:
	Desc: Begin declaration of a new polygon.
#*/
_MEMBER_FUNCTION(GeometryTemplate, beginPolygon, 1, _SC("."))
/*#
	Func: pushVertex
	Proto: void:Vector
	Desc: Push a vertex on the current polygon declaration.
#*/
_MEMBER_FUNCTION(GeometryTemplate, pushVertex, 2, _SC(".x"))
/*#
	Func: pushNormal
	Proto: void:Vector
	Desc: Push a normal on the current polygon declaration.
#*/
_MEMBER_FUNCTION(GeometryTemplate, pushNormal, 2, _SC(".x"))
/*#
	Func: pushColor
	Proto: void:Vector
	Desc: Push a color on the current polygon declaration.
#*/
_MEMBER_FUNCTION(GeometryTemplate, pushColor, 2, _SC(".x"))
/*#
	Func: pushUV
	Proto: void:int channel, UV
	Desc: Push an UV on a channel of the current polygon declaration.
#*/
_MEMBER_FUNCTION(GeometryTemplate, pushUV, 3, _SC(".ix"))
/*#
	Func: endPolygon
	Proto: void:int material
	Desc: End declaration of the current polygon and specify its material index.
#*/
_MEMBER_FUNCTION(GeometryTemplate, endPolygon, 2, _SC(".i"))

//------------------------------------------------------------------------------
//# Topic: Geometry creation
//------------------------------------------------------------------------------

/*#
	Func: clear
	Proto: void:
	Desc: Clear current template definition, keep the current material table.
#*/
_MEMBER_FUNCTION(GeometryTemplate, clear, 1, _SC("."))

/*#
	Func: instantiate
	Proto: Geometry:Engine engine, string name
	Desc: Instantiate the current template as a named geometry.
	Note: If the geometry name is already found in the current engine cache, a
	cached copy will be returned instead of a new instantiation.
#*/
_MEMBER_FUNCTION(GeometryTemplate, instantiate, 3, _SC(".xs"))

/*#
	Func: clearMaterial
	Proto: void:
	Desc: Push a material on the template material table.
#*/
_MEMBER_FUNCTION(GeometryTemplate, clearMaterial, 1, _SC("."))
/*#
	Func: pushMaterial
	Proto: void:String path
	Desc: Push a material path on the template material table.
#*/
_MEMBER_FUNCTION(GeometryTemplate, pushMaterial, 2, _SC(".s"))

/*#
	Func: setVertexMergeThreshold
	Proto: void:float threshold
	Desc: Set the vertex merging algorithm threshold.
#*/
_MEMBER_FUNCTION(GeometryTemplate, setVertexMergeThreshold, 2, _SC(".n"))

_END_CLASS(GeometryTemplate)


#endif	// __COBJECT_GEOMETRY_TEMPLATE_IMPL__
