#version 130

#define lc 4
varying vec4 diffuse[lc], pos, spos;//, ambient;//, col;
varying vec3 lightDir[lc]/*halfVector[lc], */;
//varying vec3 lightDir[lc]/*halfVector[lc], */;
varying vec3 normal, srcn;
varying float fogCoord, alpha;

uniform int lightsCount;
uniform bool acc_fog, shadows;
uniform mat4 mat;

void prepareLight(in int index, in vec3 p) {
	vec4 lpos = gl_LightSource[index].position;
	lightDir[index] = normalize(lpos.xyz - (p * lpos.w));
	//halfVector[index] = normalize(lightDir[index] - normalize(p));
	diffuse[index] = gl_FrontMaterial.diffuse * gl_LightSource[index].diffuse;
}

void main(void) {
	float NdotL;
	//col = gl_Color;
	alpha = gl_Color.a;
	normal = normalize(gl_NormalMatrix * gl_Normal);
	srcn = gl_Normal;
	pos = gl_ModelViewMatrix * gl_Vertex;
	//v = normalize(-pos);
	if (acc_fog) {
		fogCoord = (gl_Fog.end - length(pos) * 0.85) / (gl_Fog.end - gl_Fog.start);
		fogCoord = 1. - clamp(fogCoord, 0., 1.);
	}
	vec3 ldir;
	if (lightsCount > 0) {
		prepareLight(0, pos.xyz);
		if (lightsCount > 1) {
			prepareLight(1, pos.xyz);
			if (lightsCount > 2) {
				prepareLight(2, pos.xyz);
				if (lightsCount > 3) {
					prepareLight(3, pos.xyz);
					diffuse[3] = gl_FrontMaterial.diffuse * gl_LightSource[3].diffuse;
					/*if (lightsCount > 4) {
						diffuse[4] = gl_FrontMaterial.diffuse * gl_LightSource[4].diffuse;
						if (lightsCount > 5) {
							diffuse[5] = gl_FrontMaterial.diffuse * gl_LightSource[5].diffuse;
							if (lightsCount > 6) {
								diffuse[6] = gl_FrontMaterial.diffuse * gl_LightSource[6].diffuse;
								if (lightsCount > 6) {
									diffuse[7] = gl_FrontMaterial.diffuse * gl_LightSource[7].diffuse;
								}
							}
						}
					}*/
				}
			}
		}
	}
	//ambient = gl_LightModel.ambient * gl_FrontMaterial.ambient;
	gl_TexCoord[0] = gl_MultiTexCoord0;
	gl_TexCoord[1] = gl_MultiTexCoord1;
	gl_TexCoord[2] = gl_MultiTexCoord2;
	gl_TexCoord[3] = gl_MultiTexCoord3;
	gl_Position = ftransform();
	spos = pos * mat;
}
