/******************************************************************************
 * Copyright 1986-2002 by mental images GmbH & Co.KG, Fasanenstr. 81, D-10623
 * Berlin, Germany. All rights reserved.
 ******************************************************************************
 * Created:	24.10.97
 * Module:	baseshader
 * Purpose:	base shaders for Phenomenon writers
 *
 * Exports:
 *	mib_transparency
 *	mib_transparency_version
 *	mib_opacity
 *	mib_opacity_version
 *
 * History:
 *      24.10.97: initial version
 *	12.01.98: added mib_opacity
 *	22.01.98: transp and opacity are now of type miColor
 *
 * Description:
 *	Transparency node
 *****************************************************************************/

#include <shader.h>

struct mt {
	miColor input;
	miColor transp;
};

DLLEXPORT int mib_transparency_version(void) {return(2);}

DLLEXPORT miBoolean mib_transparency(
	miColor		*result,
	miState		*state,
	struct mt	*paras)
{
	register miColor *transp = mi_eval_color(&paras->transp);
	miColor		inp;

	if (transp->r == 0.0 && transp->g == 0.0 &&
	    transp->b == 0.0 && transp->a == 0.0)
		*result = *mi_eval_color(&paras->input);
	else {
		mi_trace_transparent(result, state);

		if (transp->r != 1.0 || transp->g != 1.0 ||
		    transp->b != 1.0 || transp->a != 1.0) {
			inp = *mi_eval_color(&paras->input);
			result->r = result->r * transp->r +
						inp.r * (1.0 - transp->r);
			result->g = result->g * transp->g +
						inp.g * (1.0 - transp->g);
			result->b = result->b * transp->b +
						inp.b * (1.0 - transp->b);
			result->a = result->a * transp->a +
						inp.a * (1.0 - transp->a);
		}
	}
	return(miTRUE);
}


/*-------------------------------------------------------------------------*/

struct mo {
	miColor		input;
	miColor		opacity;
};

DLLEXPORT int mib_opacity_version(void) {return(2);}

DLLEXPORT miBoolean mib_opacity(
	miColor		*result,
	miState		*state,
	struct mo	*paras)
{
	register miColor *opacity = mi_eval_color(&paras->opacity);
	miColor		inp;

	if (opacity->r == 1.0 && opacity->g == 1.0 &&
	    opacity->b == 1.0 && opacity->a == 1.0)
		*result = *mi_eval_color(&paras->input);
	else {
		mi_trace_transparent(result, state);

		if (opacity->r != 0.0 || opacity->g != 0.0 ||
		    opacity->b != 0.0 || opacity->a != 0.0) {
			inp = *mi_eval_color(&paras->input);
			result->r = result->r * (1.0 - opacity->r) +
							inp.r * opacity->r;
			result->g = result->g * (1.0 - opacity->g) +
							inp.g * opacity->g;
			result->b = result->b * (1.0 - opacity->b) +
							inp.b * opacity->b;
			result->a = result->a * (1.0 - opacity->a) +
							inp.a * opacity->a;
		}
	}
	return(miTRUE);
}
