// 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_FIRERISEENV_MAPPED	4.0
#define	TCGEN_VECTOR			6.0

#define	GLSL_TMOD_NONE			0.0
#define	GLSL_TMOD_TURBULENT		1.0
#define	GLSL_TMOD_MATRIX		2.0
#define	GLSL_TMOD_SWAP			3.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 (u_tgen.x == TCGEN_FIRERISEENV_MAPPED)
		{	// u_parmtc = normalize(-backEnd.currentEntity->e.fireRiseDir)
			float d = dot(attr_Normal, u_parmtc);
			vec3 reflected = attr_Normal * 2.0 * d - u_parmtc;
			st[b] = 0.5 + reflected.yz * vec2(0.5, -0.5);
		}
		if (tgen[b] == TCGEN_VECTOR)
			st[b] = vec2( dot(glVertex, u_tcGenVectorS[b]), dot(glVertex, u_tcGenVectorT[b]) );

		// texMod:
		// unroll texMod loop (#define TR_MAX_TEXMODS 4)
		// for stupid NVidia GLSL compiler (new Detonator + old cards like 9600GT/8500GT)
		float f = 999.0;	// initial f must be != GLSL_TMOD_NONE
		include tcMod_multitexture.inc
		include tcMod_multitexture.inc
		include tcMod_multitexture.inc
		include tcMod_multitexture.inc
	}

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