void processTexCoords(int tex, vec4 texCoord)
{
    int index = tex * 4;
    int index2 = tex * 8;
    if (texGen[index])
    {
        if (texGenMode[index] == 9216)
        {
            texCoord.s = dot(texGenParams[index2], eyePos);
        }
        else if (texGenMode[index] == 9217)
        {
            texCoord.s = dot(texGenParams[index2 + 1], gl_Vertex);
        }
    }
    if (texGen[index + 1])
    {
        if (texGenMode[index + 1] == 9216)
        {
            texCoord.t = dot(texGenParams[index2 + 2], eyePos);
        }
        else if (texGenMode[index + 1] == 9217)
        {
            texCoord.t = dot(texGenParams[index2 + 3], gl_Vertex);
        }
    }
    if (texGen[index + 2])
    {
        if (texGenMode[index + 2] == 9216)
        {
            texCoord.p = dot(texGenParams[index2 + 4], eyePos);
        }
        else if (texGenMode[index + 2] == 9217)
        {
            texCoord.p = dot(texGenParams[index2 + 5], gl_Vertex);
        }
    }
    if (texGen[index + 3])
    {
        if (texGenMode[index + 3] == 9216)
        {
            texCoord.q = dot(texGenParams[index2 + 6], eyePos);
        }
        else if (texGenMode[index + 3] == 9217)
        {
            texCoord.q = dot(texGenParams[index2 + 7], gl_Vertex);
        }
    }
    gl_TexCoord[tex] = gl_TextureMatrix[tex] * texCoord;
}

void main()
{
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    eyePos = gl_ModelViewMatrix * gl_Vertex;
    gl_FogFragCoord = abs(eyePos.z/eyePos.w);
    if (useTex[0]) processTexCoords(0, gl_MultiTexCoord0);
    if (useTex[1]) processTexCoords(1, gl_MultiTexCoord1);
    if (useTex[2]) processTexCoords(2, gl_MultiTexCoord2);
    if (useTex[3]) processTexCoords(3, gl_MultiTexCoord3);
    if (useTex[4]) processTexCoords(4, gl_MultiTexCoord4);
    if (useTex[5]) processTexCoords(5, gl_MultiTexCoord5);
    if (useTex[6]) processTexCoords(6, gl_MultiTexCoord6);
    if (useTex[7]) processTexCoords(7, gl_MultiTexCoord7);
    if (useTex[8]) processTexCoords(8, gl_MultiTexCoord8);
    if (useTex[9]) processTexCoords(9, gl_MultiTexCoord9);
    if (useTex[10]) processTexCoords(10, gl_MultiTexCoord10);
    if (useTex[11]) processTexCoords(11, gl_MultiTexCoord11);
    if (useTex[12]) processTexCoords(12, gl_MultiTexCoord12);
    if (useTex[13]) processTexCoords(13, gl_MultiTexCoord13);
    if (useTex[14]) processTexCoords(14, gl_MultiTexCoord14);
    if (useTex[15]) processTexCoords(15, gl_MultiTexCoord15);
    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);
    }