//Vertex Shader for shadow map and normal mapping - file "texture_shadownorm.vert"

#version 330 

precision highp float;

uniform mat4 perspective_matrix;
uniform mat4 modelview_matrix;
uniform mat4 lightview_matrix;
uniform mat4 model_matrix;
uniform mat4 model_inverse_matrix;
uniform vec3 light_position;
uniform vec3 light_position_odsm;

in vec4 in_position;
in vec3 in_normal;
in vec4 in_tangent;
in vec2 in_texcoord;

out vec2 fragment_texcoord;
out vec4 ex_lightspace_position;
out vec3 texture_light;
out vec3 texture_eye;
out vec3 texture_light_odsm;
out vec4 worldspace_pos;

void main(void)
{
	fragment_texcoord = in_texcoord;

	ex_lightspace_position = lightview_matrix * in_position;

	vec3 n = (model_matrix * vec4(in_normal, 0.0)).xyz;
	vec3 t = (model_matrix * vec4(in_tangent.x, in_tangent.y, in_tangent.z, 0.0)).xyz;//Korr?
	//vec3 t = (model_matrix * vec4(in_tangent.x, in_tangent.y, in_tangent.z, in_tangent.w)).xyz;
	vec3 b = cross(n, t) * in_tangent.w;

	//ACHTUNG INFO?????
	//Im Prinzip könnte aus n,t,b eine eigene Matrix erstellt werden, die mir dann das Licht 
	//in den passenden Raum transformiert, oder?
	//mat3 tmp = mat3(t.x, b.x, n.x, 
				    //t.y, b.y, n.y,
				    //t.z, b.z, n.z);
	mat3 tmp = mat3(t, b, n);

	//normalze the vectors
	//n = normalize(n);
	//t = normalize(t);
	//b = normalize(b);

	//vec3 eye = (modelview_matrix * in_position).xyz;
	//eye = normalize(-eye);

	//vec3 light_dir = vec3(light_position - eye);
	//FRAGE: stimmt die Lichtrichtung?
	
	//vec3 light_dir = vec3(light_position - in_position.xyz); 
	//vec3 light = normalize(light_dir);
	
	//vec3 light = normalize(ex_lightspace_position.xyz);    //könnte aber passen???? SIEHT OK AUS
	//vec3 light = normalize(tmp * light_position);

	//DAS LICHT IST FALSCH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DAS DÜRFTE DIE PROBLEME BEREITEN!!!!!!!!


	vec3 light = normalize(model_matrix * vec4(light_position,1.0)).xyz;
	//vec3 light = normalize(light_position);

	texture_light.x = dot(light, t);
	texture_light.y = dot(light, b);
	texture_light.z = dot(light, n);

	vec4 worldspace_pos = (model_matrix * in_position);    //KORREKT
	
	//vec3 eye = tmp * (model_matrix * in_position).xyz; 
	//vec3 eye = tmp * (model_matrix * model_inverse_matrix * in_position).xyz;
	vec3 eye = -normalize(worldspace_pos.xyz);

	texture_eye.x = dot(eye, t);
	texture_eye.y = dot(eye, b);
	texture_eye.z = dot(eye, n);

	//TODO: still might not be correct
	vec3 light_odsm = normalize(model_matrix * ( vec4(light_position_odsm,1.0) - in_position) ).xyz;
	//this may be correct, but produces errors :/
	//vec3 light_odsm = normalize(model_matrix * model_inverse_matrix * ( vec4(light_position_odsm,1.0) - in_position) ).xyz;
	//vec3 light = normalize(light_position);

	texture_light_odsm.x = dot(light_odsm, t);
	texture_light_odsm.y = dot(light_odsm, b);
	texture_light_odsm.z = dot(light_odsm, n);

	gl_Position = perspective_matrix * modelview_matrix * in_position; //KORR

}//end of main
