// no fog version of tcGen_multitexture.inc

uniform	vec2	u_tgen;
uniform	vec3	u_parmtc;
uniform	vec3	u_tcGenVectorS[2];	// 2 = NUM_TEXTURE_BUNDLES
uniform	vec3	u_tcGenVectorT[2];	// 2 = NUM_TEXTURE_BUNDLES
uniform	float	u_tcModParms[53];	// NUM_TEXTURE_BUNDLES * TR_MAX_TEXMODS * 6 + TR_MAX_TEXMODS + 1

#define	TCGEN_TEXTURE			1.0
#define	TCGEN_LIGHTMAP			2.0
#define	TCGEN_ENVIRONMENT_MAPPED	3.0
#define	TCGEN_VECTOR			5.0

#define	GLSL_TMOD_NONE			0.0
#define	GLSL_TMOD_TURBULENT		1.0
#define	GLSL_TMOD_MATRIX		2.0

void tcGen(out vec2 st0, out vec2 st1, in vec3 glVertex)
{
	int	b;
	vec2	st[2];
	float	tgen[2];

	tgen[0] = u_tgen.x;
	tgen[1] = u_tgen.y;
	int tcOffs = 0;

	for ( b = 0; b < 2; b++ )	// 2 = NUM_TEXTURE_BUNDLES
	{
		// texGen
		st[b] = vec2(0.0);		// TCGEN_BAD (default) shut up compiler :(
		if (tgen[b] == TCGEN_TEXTURE)
			st[b] = attr_texCoords;
		if (tgen[b] == TCGEN_LIGHTMAP)
			st[b] = attr_lightCoords;
		if (tgen[b] == TCGEN_ENVIRONMENT_MAPPED)
		{	// u_parmtc = backEnd.or.viewOrigin
			vec3 viewer = normalize(u_parmtc - glVertex);
			float d = dot(attr_Normal, viewer);
			vec3 reflected = attr_Normal * 2.0 * d - viewer;
			st[b] = 0.5 + reflected.yz * 0.5;
		}
		if (tgen[b] == TCGEN_VECTOR)
			st[b] = vec2( dot(glVertex, u_tcGenVectorS[b]), dot(glVertex, u_tcGenVectorT[b]) );

		// texMod
		while (true)
		{     
			float f = u_tcModParms[tcOffs++];
			if (f == GLSL_TMOD_NONE)
				break;
			if (f == GLSL_TMOD_MATRIX)
			{	// stupid ATI HD2600 can't parse 'a*u[t++]+b*u[t++]+u[t++]' :((
				vec2 sm = st[b];
				float pa, pb, pc, pd, pe, pf;
				pa = u_tcModParms[tcOffs++];
				pb = u_tcModParms[tcOffs++];
				pc = u_tcModParms[tcOffs++];
				pd = u_tcModParms[tcOffs++];
				pe = u_tcModParms[tcOffs++];
				pf = u_tcModParms[tcOffs++];
				st[b].s = dot(vec3(sm, 1.0), vec3(pa, pb, pc));
				st[b].t = dot(vec3(sm, 1.0), vec3(pd, pe, pf));
				continue;	// stupid NVidia GLSL compiler (new Detonator + old cards like 9600GT/8500GT)
			}
			if (f == GLSL_TMOD_TURBULENT)
			{
				vec2 sm;
				float now = u_tcModParms[tcOffs++];
				float amplitude = u_tcModParms[tcOffs++];
				sm.x = sin(((glVertex.x + glVertex.z) * 0.125/128.0 + now) * 2.0 * 3.14159);
				sm.y = sin((glVertex.y * 0.125/128.0 + now) * 2.0 * 3.14159);
				st[b] += sm * amplitude;
				continue;	// stupid NVidia GLSL compiler (new Detonator + old cards like 9600GT/8500GT)
			}
		}
	}

	st0 = st[0];
	st1 = st[1];
}
