// special simple version of colorGen.inc using for 1st stage: CSND (fake lighting)

uniform	vec4	u_gen;
uniform	vec4	u_parm0;
uniform	vec4	u_parm1;
uniform	vec4	u_parm2;

#ifndef GLSL_CGEN_CONST
	#define	GLSL_CGEN_CONST				0.0
	#define	GLSL_CGEN_LIGHTING_DIFFUSE		1.0
	#define	GLSL_CGEN_VERTEX			2.0

	#define	GLSL_AGEN_CONST				0.0
	#define	GLSL_AGEN_VERTEX			2.0
	#define	GLSL_AGEN_ONE_MINUS_VERTEX		3.0
	#define	GLSL_AGEN_VERTEX_COLOR			4.0
	#define	GLSL_AGEN_ONE_MINUS_VERTEX_COLOR	5.0
#endif

vec4 colorGenLit (in vec3 glVertex, in vec4 gen, in vec4 parm0, in vec4 parm1, in vec4 parm2)
{
	vec4	col = parm0;	// default is GLSL_CGEN_CONST

	if (gen.x == GLSL_CGEN_LIGHTING_DIFFUSE)
	{	// parm0.xyz = lightDir
		// parm1 = ambientLight
		// parm2 = directedLight
		float incoming = dot(attr_Normal, parm0.xyz);
		col = vec4(parm1.rgb + max(incoming, 0.0) * parm2.rgb, 1.0);
	}
	if (gen.x == GLSL_CGEN_VERTEX)
		col = attr_Color;

	if (gen.y >= GLSL_AGEN_CONST)
	{
		if (gen.y == GLSL_AGEN_CONST)
			col.a = parm0.a;
		if (gen.y == GLSL_AGEN_VERTEX)
			col.a = attr_Color.a;
		if (gen.y == GLSL_AGEN_ONE_MINUS_VERTEX)
			col.a = 1.0 - attr_Color.a;
		if (gen.y == GLSL_AGEN_VERTEX_COLOR)
			col.a = (attr_Color.r + attr_Color.g + attr_Color.b) * 0.33333;
		if (gen.y == GLSL_AGEN_ONE_MINUS_VERTEX_COLOR)
			col.a = 1.0 - (attr_Color.r + attr_Color.g + attr_Color.b) * 0.33333;
	}

	// clamp for avoidance 1.0 (special case for portal/mirror marks)
	col = clamp(col, 0.0, 0.95);
	return col;
}
