/*
 * Copyright 2011, Blender Foundation.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "stdosl.h"

shader node_ward_bsdf(
	color Color = color(0.8, 0.8, 0.8),
	float Roughness = 0.0,
	float Anisotropy = 0.0,
	float Rotation = 0.0,
	normal Normal = N,
	normal Tangent = normalize(dPdu),
	output closure color BSDF = diffuse(Normal))
{
	/* rotate tangent around normal */
	vector T = Tangent;

	if (Rotation != 0.0)
		T = rotate(T, Rotation*2.0*M_PI, point(0.0, 0.0, 0.0), Normal);

	/* compute roughness */
	float RoughnessU, RoughnessV;
	float aniso = clamp(Anisotropy, -0.99, 0.99);

	if (aniso < 0.0) {
		RoughnessU = Roughness / (1.0 + aniso);
		RoughnessV = Roughness * (1.0 + aniso);
	}
	else {
		RoughnessU = Roughness * (1.0 - aniso);
		RoughnessV = Roughness / (1.0 - aniso);
	}

	BSDF = Color * ward(Normal, T, RoughnessU, RoughnessV);
}

