#include "ai.h"

AI_SHADER_NODE_EXPORT_METHODS(apOrenNayerBSDF)

enum apClosureParams
{
	p_weight,
	p_roughness,
	p_transmission,
	p_opacity
};


struct ShaderData
{
	float roughness;
	bool transmission;
};

node_parameters
{
	AiParameterRGB("weight", 0.5, 0.5, 0.5);
	AiParameterFlt("roughness", 0);
	AiParameterBool("transmission", 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->transmission = AiNodeGetBool(node, "transmission");
}

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

shader_evaluate
{
	ShaderData *data = (ShaderData *)AiNodeGetLocalData(node);
	AtRGB weight = AiShaderEvalParamRGB(p_weight);
	AtRGB opacity = AiShaderEvalParamRGB(p_opacity);
	AtBSDF *bsdf = AiOrenNayarBSDF(sg, weight * opacity, sg->N, data->roughness, data->transmission);
	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 = apOrenNayerBSDF;
	node->output_type = AI_TYPE_CLOSURE;
	node->name = "apOrenNayerBSDF";
	node->node_type = AI_NODE_SHADER;
	strcpy(node->version, AI_VERSION);
	return true;
}