/*
 * 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"
#include "node_color.h"

color image_texture_lookup(string filename, string color_space, float u, float v, output float Alpha)
{
	color rgb = (color)texture(filename, u, 1.0 - v, "wrap", "periodic", "alpha", Alpha);

	if (color_space == "sRGB")
		rgb = color_srgb_to_scene_linear(rgb);

	return rgb;
}

shader node_image_texture(
	int use_mapping = 0,
	matrix mapping = matrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
	point Vector = P,
	string filename = "",
	string color_space = "sRGB",
	string projection = "Flat",
	float projection_blend = 0.0,
	output color Color = color(0.0, 0.0, 0.0),
	output float Alpha = 1.0)
{
	point p = Vector;

	if (use_mapping)
		p = transform(mapping, p);

	if (projection == "Flat") {
		Color = image_texture_lookup(filename, color_space, p[0], p[1], Alpha);
	}
	else if (projection == "Box") {
		/* object space normal */
		vector Nob = transform("world", "object", N);

		/* project from direction vector to barycentric coordinates in triangles */
		Nob = vector(fabs(Nob[0]), fabs(Nob[1]), fabs(Nob[2]));
		Nob /= (Nob[0] + Nob[1] + Nob[2]);

		/* basic idea is to think of this as a triangle, each corner representing
		 * one of the 3 faces of the cube. in the corners we have single textures,
		 * in between we blend between two textures, and in the middle we a blend
		 * between three textures.
		 *
		 * the Nxyz values are the barycentric coordinates in an equilateral
		 * triangle, which in case of blending, in the middle has a smaller
		 * equilateral triangle where 3 textures blend. this divides things into
		 * 7 zones, with an if () test for each zone */

		vector weight = vector(0.0, 0.0, 0.0);
		float blend = projection_blend;
		float limit = 0.5*(1.0 + blend);

		/* first test for corners with single texture */
		if (Nob[0] > limit*(Nob[0] + Nob[1]) && Nob[0] > limit*(Nob[0] + Nob[2])) {
			weight[0] = 1.0;
		}
		else if (Nob[1] > limit*(Nob[0] + Nob[1]) && Nob[1] > limit*(Nob[1] + Nob[2])) {
			weight[1] = 1.0;
		}
		else if (Nob[2] > limit*(Nob[0] + Nob[2]) && Nob[2] > limit*(Nob[1] + Nob[2])) {
			weight[2] = 1.0;
		}
		else if (blend > 0.0) {
			/* in case of blending, test for mixes between two textures */
			if (Nob[2] < (1.0 - limit)*(Nob[1] + Nob[0])) {
				weight[0] = Nob[0] / (Nob[0] + Nob[1]);
				weight[0] = clamp((weight[0] - 0.5 * (1.0 - blend)) / blend, 0.0, 1.0);
				weight[1] = 1.0 - weight[0];
			}
			else if (Nob[0] < (1.0 - limit)*(Nob[1] + Nob[2])) {
				weight[1] = Nob[1] / (Nob[1] + Nob[2]);
				weight[1] = clamp((weight[1] - 0.5 * (1.0 - blend)) / blend, 0.0, 1.0);
				weight[2] = 1.0 - weight[1];
			}
			else if (Nob[1] < (1.0 - limit) * (Nob[0] + Nob[2])) {
				weight[0] = Nob[0] / (Nob[0] + Nob[2]);
				weight[0] = clamp((weight[0] - 0.5 * (1.0 - blend)) / blend, 0.0, 1.0);
				weight[2] = 1.0 - weight[0];
			}
			else {
				/* last case, we have a mix between three */
				weight[0] = ((2.0 - limit) * Nob[0] + (limit - 1.0)) / (2.0 * limit - 1.0);
				weight[1] = ((2.0 - limit) * Nob[1] + (limit - 1.0)) / (2.0 * limit - 1.0);
				weight[2] = ((2.0 - limit) * Nob[2] + (limit - 1.0)) / (2.0 * limit - 1.0);
			}
		}

		Color = color(0.0, 0.0, 0.0);
		Alpha = 0.0;

		float tmp_alpha;

		if (weight[0] > 0.0) {
			Color += weight[0]*image_texture_lookup(filename, color_space, p[1], p[2], tmp_alpha);
			Alpha += weight[0]*tmp_alpha;
		}
		if (weight[1] > 0.0) {
			Color += weight[1]*image_texture_lookup(filename, color_space, p[0], p[2], tmp_alpha);
			Alpha += weight[1]*tmp_alpha;
		}
		if (weight[2] > 0.0) {
			Color += weight[2]*image_texture_lookup(filename, color_space, p[1], p[0], tmp_alpha);
			Alpha += weight[2]*tmp_alpha;
		}
	}
}

