
float processTexCoord(float coord, int texGenMode, vec4 texGenParamsEye, vec4 texGenParamsObject)
{
    if (texGenMode == 9216) return dot(texGenParamsEye, eyePos);
    else if (texGenMode == 9217) return dot(texGenParamsObject, gl_Vertex);
    else return coord;
}

void main()
{
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    eyePos = gl_ModelViewMatrix * gl_Vertex;
    gl_FogFragCoord = abs(eyePos.z/eyePos.w);
	if (useTex0)
	{
		vec4 texCoord = gl_MultiTexCoord0;
		if (texGen0_s) texCoord.s = processTexCoord(texCoord.s, texGenMode0_s, gl_EyePlaneS[0], gl_ObjectPlaneS[0]);
		if (texGen0_t) texCoord.t = processTexCoord(texCoord.t, texGenMode0_t, gl_EyePlaneT[0], gl_ObjectPlaneT[0]);
		if (texGen0_p) texCoord.p = processTexCoord(texCoord.p, texGenMode0_p, gl_EyePlaneR[0], gl_ObjectPlaneR[0]);
		if (texGen0_q) texCoord.q = processTexCoord(texCoord.q, texGenMode0_q, gl_EyePlaneQ[0], gl_ObjectPlaneQ[0]);
		gl_TexCoord[0] = gl_TextureMatrix[0] * texCoord;
	}
	if (useTex1)
	{
		vec4 texCoord = gl_MultiTexCoord1;
		if (texGen1_s) texCoord.s = processTexCoord(texCoord.s, texGenMode1_s, gl_EyePlaneS[1], gl_ObjectPlaneS[1]);
		if (texGen1_t) texCoord.t = processTexCoord(texCoord.t, texGenMode1_t, gl_EyePlaneT[1], gl_ObjectPlaneT[1]);
		if (texGen1_p) texCoord.p = processTexCoord(texCoord.p, texGenMode1_p, gl_EyePlaneR[1], gl_ObjectPlaneR[1]);
		if (texGen1_q) texCoord.q = processTexCoord(texCoord.q, texGenMode1_q, gl_EyePlaneQ[1], gl_ObjectPlaneQ[1]);
		gl_TexCoord[1] = gl_TextureMatrix[1] * texCoord;
	}
	if (useTex2)
    {
        vec4 texCoord = gl_MultiTexCoord2;
        if (texGen2_s) texCoord.s = processTexCoord(texCoord.s, texGenMode2_s, gl_EyePlaneS[2], gl_ObjectPlaneS[2]);
        if (texGen2_t) texCoord.t = processTexCoord(texCoord.t, texGenMode2_t, gl_EyePlaneT[2], gl_ObjectPlaneT[2]);
        if (texGen2_p) texCoord.p = processTexCoord(texCoord.p, texGenMode2_p, gl_EyePlaneR[2], gl_ObjectPlaneR[2]);
        if (texGen2_q) texCoord.q = processTexCoord(texCoord.q, texGenMode2_q, gl_EyePlaneQ[2], gl_ObjectPlaneQ[2]);
        gl_TexCoord[2] = gl_TextureMatrix[2] * texCoord;
    }
    if (useTex3)
    {
        vec4 texCoord = gl_MultiTexCoord3;
        if (texGen3_s) texCoord.s = processTexCoord(texCoord.s, texGenMode3_s, gl_EyePlaneS[3], gl_ObjectPlaneS[3]);
        if (texGen3_t) texCoord.t = processTexCoord(texCoord.t, texGenMode3_t, gl_EyePlaneT[3], gl_ObjectPlaneT[3]);
        if (texGen3_p) texCoord.p = processTexCoord(texCoord.p, texGenMode3_p, gl_EyePlaneR[3], gl_ObjectPlaneR[3]);
        if (texGen3_q) texCoord.q = processTexCoord(texCoord.q, texGenMode3_q, gl_EyePlaneQ[3], gl_ObjectPlaneQ[3]);
        gl_TexCoord[3] = gl_TextureMatrix[3] * texCoord;
    }
    if (useTex4)
    {
        vec4 texCoord = gl_MultiTexCoord4;
        if (texGen4_s) texCoord.s = processTexCoord(texCoord.s, texGenMode4_s, gl_EyePlaneS[4], gl_ObjectPlaneS[4]);
        if (texGen4_t) texCoord.t = processTexCoord(texCoord.t, texGenMode4_t, gl_EyePlaneT[4], gl_ObjectPlaneT[4]);
        if (texGen4_p) texCoord.p = processTexCoord(texCoord.p, texGenMode4_p, gl_EyePlaneR[4], gl_ObjectPlaneR[4]);
        if (texGen4_q) texCoord.q = processTexCoord(texCoord.q, texGenMode4_q, gl_EyePlaneQ[4], gl_ObjectPlaneQ[4]);
        gl_TexCoord[4] = gl_TextureMatrix[4] * texCoord;
    }
    if (useTex5)
    {
        vec4 texCoord = gl_MultiTexCoord5;
        if (texGen5_s) texCoord.s = processTexCoord(texCoord.s, texGenMode5_s, gl_EyePlaneS[5], gl_ObjectPlaneS[5]);
        if (texGen5_t) texCoord.t = processTexCoord(texCoord.t, texGenMode5_t, gl_EyePlaneT[5], gl_ObjectPlaneT[5]);
        if (texGen5_p) texCoord.p = processTexCoord(texCoord.p, texGenMode5_p, gl_EyePlaneR[5], gl_ObjectPlaneR[5]);
        if (texGen5_q) texCoord.q = processTexCoord(texCoord.q, texGenMode5_q, gl_EyePlaneQ[5], gl_ObjectPlaneQ[5]);
        gl_TexCoord[5] = gl_TextureMatrix[5] * texCoord;
    }
    if (useTex6)
    {
        vec4 texCoord = gl_MultiTexCoord6;
        if (texGen6_s) texCoord.s = processTexCoord(texCoord.s, texGenMode6_s, gl_EyePlaneS[6], gl_ObjectPlaneS[6]);
        if (texGen6_t) texCoord.t = processTexCoord(texCoord.t, texGenMode6_t, gl_EyePlaneT[6], gl_ObjectPlaneT[6]);
        if (texGen6_p) texCoord.p = processTexCoord(texCoord.p, texGenMode6_p, gl_EyePlaneR[6], gl_ObjectPlaneR[6]);
        if (texGen6_q) texCoord.q = processTexCoord(texCoord.q, texGenMode6_q, gl_EyePlaneQ[6], gl_ObjectPlaneQ[6]);
        gl_TexCoord[6] = gl_TextureMatrix[6] * texCoord;
    }
    if (useTex7)
    {
        vec4 texCoord = gl_MultiTexCoord7;
        if (texGen7_s) texCoord.s = processTexCoord(texCoord.s, texGenMode7_s, gl_EyePlaneS[7], gl_ObjectPlaneS[7]);
        if (texGen7_t) texCoord.t = processTexCoord(texCoord.t, texGenMode7_t, gl_EyePlaneT[7], gl_ObjectPlaneT[7]);
        if (texGen7_p) texCoord.p = processTexCoord(texCoord.p, texGenMode7_p, gl_EyePlaneR[7], gl_ObjectPlaneR[7]);
        if (texGen7_q) texCoord.q = processTexCoord(texCoord.q, texGenMode7_q, gl_EyePlaneQ[7], gl_ObjectPlaneQ[7]);
        gl_TexCoord[7] = gl_TextureMatrix[7] * texCoord;
    }
    fragNorm = gl_NormalMatrix * gl_Normal;
    fragCol = gl_Color;
    if (useNormals)
    {
        vec3 color = vec3(0.0);
        vec3 Idiff = color;
        vec3 pos = gl_LightSource[0].position.xyz-eyePos.xyz;
        vec3 L = normalize(pos);
        float d = dot(fragNorm, L);
        if (d > 0.0)
        {
            Idiff = gl_LightSource[0].diffuse.rgb * max(d, 0.0);
            Idiff = clamp(Idiff, 0.0, 1.0);
            color += Idiff;
        }
        pos = gl_LightSource[1].position.xyz-eyePos.xyz;
        L = normalize(pos);
        d = dot(fragNorm, L);
        if (d > 0.0)
        {
            Idiff = gl_LightSource[1].diffuse.rgb * max(d, 0.0);
            Idiff = clamp(Idiff, 0.0, 1.0);
            color += Idiff;
        }
        color = vec3(0.4) + (color * 0.6);
        fragCol.rgb *= clamp(color, 0.0, 1.0);
    }