//********************************************************************
// This source file is part of Ninfa3D Engine
// 
// Copyright (C) 2008-2009 Viz_fuerte. All rights reserved.
// 
// LIMITED WARRANTY
// This software is provided "as-is", without any express or implied warranty.
// In no event shall the author be held liable for any damages arising from the use of this software.
// This is a contract. By accepting the Ninfa3D Engine (the SOFTWARE), 
// you (either an individual or a single entity, the LICENSEE) accept the terms and conditions of this licensing agreement.
// 
// If the LICENSEE does not accept the terms and conditions, then LICENSEE must not accept the SOFTWARE.
// If at a later time the LICENSEE decides not to continue licensing the SOFTWARE under these terms and 
// conditions, then the LICENSEE must delete permanently all files constituting the SOFTWARE.
//
// File: Shaders.cpp
//********************************************************************

/*--------------------------------------------------------------------
Includos*/

	#include "..\..\Header Files\Ninfa3D.h"
	#include "..\..\Codes\CShaderSceneNode.h"

/*--------------------------------------------------------------------
Variables externas*/

	extern IrrlichtDevice* device;
	extern IVideoDriver* driver;
	extern IGPUProgrammingServices* gpu;

/*--------------------------------------------------------------------
Variables globales*/

	tShaderReply *shader_list = NULL;

/*--------------------------------------------------------------------
Declaracion global de funciones*/

extern "C"
{

	/*================================================================
	Carga los archivos de efectos graficos*/

	DLL_EXPORT void* DLL_APIENTRY LoadShader(const char* VFileName,const char* PFileName)
	{
		tShaderReply* shader_reply = NULL;

		if(gpu)
		{
			shader_reply = new tShaderReply;
			if(shader_reply)
			{
				shader_reply->ShaderCallBack = new CShaderCallBack;

				if((shader_reply->material_type = gpu->addHighLevelShaderMaterialFromFiles(VFileName,"main",EVST_VS_1_1,PFileName,"main",EPST_PS_1_1,shader_reply->ShaderCallBack,EMT_SOLID,0)) == -1)
				{
					delete shader_reply->ShaderCallBack;
					delete shader_reply;
					shader_reply = NULL;
				}
				else
				{
					if(shader_list == NULL)
					{
						shader_reply->next_shader = NULL;
						shader_list = shader_reply;
					}
					else
					{
						shader_reply->next_shader = shader_list;
						shader_list = shader_reply;
					}
				}
			}
		}
		return (void*)shader_reply;
	}

	DLL_EXPORT void DLL_APIENTRY VertexShaderConstant(tShaderReply*	shader_reply,const char* Name,const float* Floats,int Count)
	{
		if(shader_reply)
		{
			tConstant* ConstantElement = new tConstant;

			cout<<" "<<endl;
			cout<<"VertexShaderConstant debug"<<endl;
			cout<<"================================="<<endl;
			cout<<"name: "<<Name<<endl;

			for(int i=0;i<Count;++i)
			cout<<"floats: "<<Floats[i]<<endl;

			cout<<"count: "<<Count<<endl;
			cout<<"================================="<<endl;
			cout<<" "<<endl;

			ConstantElement->name = Name;
			ConstantElement->preset = 0;

			ConstantElement->next = NULL;

			ConstantElement->data = Floats;
			ConstantElement->count = Count;

			if(shader_reply->ShaderCallBack->VertexConstantList == NULL)
				shader_reply->ShaderCallBack->VertexConstantList = ConstantElement;
			else
			{
				ConstantElement->next = shader_reply->ShaderCallBack->VertexConstantList;
				shader_reply->ShaderCallBack->VertexConstantList = ConstantElement;
			}
		}
	}

	DLL_EXPORT void DLL_APIENTRY PixelShaderConstant(tShaderReply* shader_reply,const char* Name,const float* Floats,int Count)
	{
		if(shader_reply)
		{
			tConstant* ConstantElement = new tConstant;

			cout<<" "<<endl;
			cout<<"PixelShaderConstant debug"<<endl;
			cout<<"================================="<<endl;
			cout<<"name: "<<Name<<endl;

			for(int i=0;i<Count;++i)
			cout<<"floats: "<<Floats[i]<<endl;

			cout<<"count: "<<Count<<endl;
			cout<<"================================="<<endl;
			cout<<" "<<endl;

			ConstantElement->name = Name;
			ConstantElement->preset = 0;

			ConstantElement->next = NULL;

			ConstantElement->data = Floats;
			ConstantElement->count = Count;

			if(shader_reply->ShaderCallBack->PixelConstantList == NULL)
				shader_reply->ShaderCallBack->PixelConstantList = ConstantElement;
			else
			{
				ConstantElement->next = shader_reply->ShaderCallBack->PixelConstantList;
				shader_reply->ShaderCallBack->PixelConstantList = ConstantElement;
			}
		}
	}

	DLL_EXPORT void DLL_APIENTRY ApplyShadersEntity(tShaderReply* shader_reply,ISceneNode* Entity)
	{
		Entity->setMaterialType((E_MATERIAL_TYPE)shader_reply->material_type);
	}

	DLL_EXPORT void DLL_APIENTRY ApplyShadersMaterial(tShaderReply* shader_reply,ISceneNode* Entity,unsigned int Num)
	{
		Entity->getMaterial(Num).MaterialType = (E_MATERIAL_TYPE)shader_reply->material_type;
	}

}