#include "ai.h"

AI_SHADER_NODE_EXPORT_METHODS(apHairBSDF)

enum apHairBSDFParams
{
	p_absorption,
	p_r_weight,
	p_tt_weight,
	p_trt_weight,
	p_tangent,
	p_roughness,
	p_eta,
	p_tilt,
	p_opacity
};


struct ShaderData
{
	float roughness;
	float eta;
	float tilt;
};

node_parameters
{
	AiParameterRGB("absorption", 0.0, 0.0, 0.0);
	AiParameterRGB("R", 0.0, 0.0, 0.0);
	AiParameterRGB("TT", 0.0, 0.0, 0.0);
	AiParameterRGB("TRT", 0.0, 0.0, 0.0);
	AiParameterFlt("roughness", 0);
	AiParameterFlt("eta", 0);
	AiParameterFlt("tilt", 0);
	AiParameterRGB("opacity", 1, 1, 1);
}

node_initialize
{
	ShaderData *data = (ShaderData *)AiMalloc(sizeof(ShaderData));
	AiNodeSetLocalData(node, data);
}

node_update
{
	ShaderData *data = (ShaderData *)AiNodeGetLocalData(node);
	data->roughness = AiNodeGetFlt(node, "roughness");
	data->eta = AiNodeGetFlt(node, "eta");
	data->tilt = AiNodeGetFlt(node, "tilt");
	
}

node_finish
{
	ShaderData *data = (ShaderData *)AiNodeGetLocalData(node);
AiFree(data);
}

shader_evaluate
{
	ShaderData *data = (ShaderData *)AiNodeGetLocalData(node);
	AtRGB absorption = AiShaderEvalParamRGB(p_absorption);
	AtRGB R_weight = AiShaderEvalParamRGB(p_r_weight);
	AtRGB TT_weight = AiShaderEvalParamRGB(p_tt_weight);
	AtRGB TRT_weight = AiShaderEvalParamRGB(p_trt_weight);
	AtRGB opacity = AiShaderEvalParamRGB(p_opacity);
	AtRGB weights[3] = {R_weight, TT_weight, TRT_weight};
	
	AtBSDF *bsdf = AidEonBSDF(sg, absorption, weights, sg->dPdv, data->roughness, data->eta, data->tilt);

	AtClosureList closures;
	closures.add(bsdf);
	closures.add(AiClosureTransparent(sg, 1.0 - opacity));
	sg->out.CLOSURE() = closures;
}

node_loader
{
	if (i > 0)
	return false;

	node->methods = apHairBSDF;
	node->output_type = AI_TYPE_CLOSURE;
	node->name = "apHairBSDF";
	node->node_type = AI_NODE_SHADER;
	strcpy(node->version, AI_VERSION);
	return true;
}
