#version 120
uniform sampler2D img2;
uniform vec3 color;
varying vec3 color2;    //in
varying vec2 texcoord;

varying vec3 position;
varying vec3 normal;

uniform vec3 lightPos;

uniform vec3 mambient;
uniform vec3 mdiffuse;
uniform vec3 mspecular;
uniform float shininess;

uniform vec3 lambient;
uniform vec3 ldiffuse;
uniform vec3 lspecular;

uniform vec2 toffset;
uniform vec2 trepeat;
 
void main()
{
	//lightPos = lightPos * gl_ModelViewMatrix.xyz;
	vec3 ambient = mambient * lambient;
	vec3 surface2light = normalize(lightPos - position);
	vec3 norm = normalize(normal);
	float dcont = max(0.0,dot(normal, surface2light));
	vec3 diffuse = mdiffuse * ldiffuse * dcont;

	vec3 surface2view = normalize(-position);
	vec3 reflection = reflect(surface2light, norm);
	float scont = pow(max(0,dot(surface2view, reflection)),shininess);

	vec3 specular = mspecular * lspecular * scont;

	vec4 texcolor2=texture2D(img2,vec2((texcoord.x+toffset.x),(texcoord.y+toffset.y)));
	if (trepeat.x != 0)
	{
		texcolor2=texture2D(img2,vec2((texcoord.x+toffset.x)*trepeat.x,(texcoord.y+toffset.y)*trepeat.y));
	}
    //gl_FragColor=vec4(vec3(ambient + diffuse + specular),1);
	gl_FragColor=vec4(texcolor2.rgba);
}